资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 programming. 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 manipulation Macros (tips for the beginner) READ set large; keep a b c prod lrgst; 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) 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). 2Acceptable: data one; set large; if 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 where 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 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.; 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, Reason 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 = 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) and (age 41); run; Example 4: Use select statements instead of if-then-else when many mutually exclusive conditions exist. 4Method 1 (no select expression): data new; set old; select; when (temp =20 then do; N+1; output; end; if final then call symput(number,n); run; %mend create; Footnote “ 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 final; set %do i = 1 %to 3; x run; %mend test; %test Example 3: Generating a series of DATA steps Invoke macro CREATE: %macro create; %do i=1 %to 3; data month infile in input product cost date; run; %mend create; %create; Produces: data month1; infile in1; input product cost date; run; data month2; infile in2; input product cost date; run; data month3; infile in3; input product cost date; run; 10Example 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 Method : %macro skipstep; data small.subset; set big.dataset; if mod(_n_, 50) = 28; /* select every 50th record*/ run; %mend skipstep; 11REFERENCES: 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 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 trad
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号