sas高效编程

上传人:xzh****18 文档编号:46592409 上传时间:2018-06-27 格式:PDF 页数:11 大小:390.82KB
返回 下载 相关 举报
sas高效编程_第1页
第1页 / 共11页
sas高效编程_第2页
第2页 / 共11页
sas高效编程_第3页
第3页 / 共11页
sas高效编程_第4页
第4页 / 共11页
sas高效编程_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《sas高效编程》由会员分享,可在线阅读,更多相关《sas高效编程(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

2、 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 tec

3、hniques 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 re

4、quire 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 pa

5、pers 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

6、 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 statement

7、s 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 = s

8、um(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

9、 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,

10、 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 (a

11、ge 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

12、 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 =

13、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

14、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

15、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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号