《SAS高效编程.pdf》由会员分享,可在线阅读,更多相关《SAS高效编程.pdf(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 1Paper PO-088 T.I.P.S.(Techniques and Information for Programming in SAS)Kathy Harkins,Carolyn Maass,Mary Anne Rutkowski Merck Research Laboratories,Upper Gwynedd,PA ABSTRACT:This paper provides a collection of basic programming tips and techniques that SAS users can implement in their daily progra
2、mming.This collection of tips should be useful immediately and will improve the efficiency of the programs.There are several examples organized by the following categories(Keywords:BASE STAT FUNCTIONS):Read and write data selectively Concise coding techniques Effective use of sorting techniques Data
3、 manipulation Macros(tips for the beginner)READ&WRITE DATA SELECTIVELY:Example 1:Use the KEEP=or DROP=on the SET or MERGE statement to keep unneeded variables out of the Program Data Vector(PDV)(the storage area for variables,values,and attributes).Acceptable:data small;set large;keep a b c prod lrg
4、st;prod=a*b;lrgst=max(a,b,c);More SAS statements;run;More Efficient:data small;set large(keep=a b c);prod=a*b;lrgst=max(a,b,c);More SAS statements;run;Example 2:Produce all subsets you require for further processing in one step to minimize the#of times a large dataset is read in.SESUG Proceedings(c)
5、SESUG,Inc (http:/www.sesug.org)The papers contained in the SESUG proceedings are the property of their authors,unless otherwise stated.Do not reprint without permission.SESUG papers are distributed freely as a courtesy of the Institute for Advanced Analytics(http:/analytics.ncsu.edu).2 Acceptable:da
6、ta one;set large;if a 10;run;data two;set large;if 9 a 90;run;(similar for data three;)More Efficient:data one two three;set large;if a 10 then output one;else if 9 a 89 then output three;run;Example 3:Read an existing SAS dataset and subset it based on values of one(or more)of the variables.Use a w
7、here instead of an if.Acceptable:data new;set old;If age 65 and gndr=F;More SAS statements.;run;More Efficient:data new;set old(where=(age 65 and gndr=F);More SAS statements.;run;CONCISE CODING TECHNIQUES:Example 1:Execute only the necessary statements move a“subsetting”if before statements you only
8、 want to execute after the condition is met.Acceptable:data elders;set demog;age=date2 date1;relday=date3 date2;tot_rslt=sum(rslt1,rslt2,rslt3);More SAS stmnts.;if age 65;More Efficient:data elders;set demog;age=date2 date1;if age 65;relday=date3 date2;tot_rslt=sum(rslt1,rslt2,rslt3);More SAS stmnts
9、.;3run;run;Example 2:Execute only the necessary statements when“if”conditions are mutually exclusive,use if-then-else instead AND move most likely condition to top of logic.Acceptable:data rsn_dscn;set pat_stat;if upcase(resn_dsc)in(Reason 1,Reason 2)then code_dsc=1;if upcase(resn_dsc)in(Reason 3,Re
10、ason 4)then code_dsc=2;if upcase(resn_dsc)in(Reason 5,Reason 6)then code_dsc=3;run;More Efficient:data rsn_dscn;set pat_stat;if upcase(resn_dsc)in(Reason 5,Reason 6)then code_dsc=3;else if upcase(resn_dsc)in(Reason 3,Reason 4)then code_dsc=2;else if upcase(resn_dsc)in(Reason 1,Reason 2)then code_dsc
11、=1;run;Example 3:Take advantage of boolean logic expressions evaluate to true(0)or false(1)to assign values instead of using if-then-else logic.Acceptable:data survey;set survey;if age=25 then agegr=1;else if age=40 then agegr=2;else agegr=3;run;More Compact:data survey;set survey;agegr=(age 25)and
12、(age 41);run;Example 4:Use select statements instead of if-then-else when many mutually exclusive conditions exist.4 Method 1(no select expression):data new;set old;select;when(temp 32)code=1;when(temp 80)code=2;when(temp=20 then do;N+1;output;end;if final then call symput(number,n);run;%mend create
13、;Footnote“&number Observations”;9Footnote“26 Observations”;Example 2:Concatenating Several datasets using a macro.Acceptable without macro:data x1;x=1;run;data x2;x=2;run;data x3;x=3;run;data final;set x1 x2 x3;run;Alternate Method:data x1;x=1;run;data x2;x=2;run;data x3;x=3;run;%macro test;data fin
14、al;set%do i=1%to 3;x&i%end;run;%mend test;%test Example 3:Generating a series of DATA steps Invoke macro CREATE:%macro create;%do i=1%to 3;data month&I;infile in&I;input product cost date;run;%mend create;%create;Produces:data month1;infile in1;input product cost date;run;data month2;infile in2;inpu
15、t product cost date;run;data month3;infile in3;input product cost date;run;10 Example 4:Comment out code using a macro or use“HOT”key:“HOT”Key Method:Use the“HOT”key cntl+/to comment selected code./*data small.subset;*/*set big.dataset;/*if mod(_n_,50)=28;*/*select every 50th record*/*run;*/Macro Me
16、thod:%macro skipstep;data small.subset;set big.dataset;if mod(_n_,50)=28;/*select every 50th record*/run;%mend skipstep;11 REFERENCES:1.In the KnowSAS Tips&Techniques From Around the Globe,Phil Mason 2.SAS Guide to Macro Processing,SAS Institute,Inc.3.SAS Users Guide:Basics,SAS Institute,Inc.SAS and
17、 all other SAS Institute Inc.product or service names are registered trademarks or trademarks of SAS Institute Inc.in the USA and other countries.indicates USA registration.Other brand and product names are trademarks of their respective companies.CONTACT INFORMATION:Kathy Harkins Merck&Co.Inc.PO Box 1000 North Wales,PA 19454-1099 267-305-5533 kathy_ Carolyn Maass Merck&Co.Inc.PO Box 1000 North Wales,PA 19454-1099 267-305-7145 carolyn_ Mary Anne Rutkowski Merck&Co.Inc.PO Box 1000 North Wales,PA 19454-1099 267-305-6925 mary_anne_
限制150内