soporaeternus 发表于 2010-8-24 22:19 
楼主的代码一个data步输出到多个datasets,原表仅遍历一次
pobel的代码可能需要多个data步遍历原表多次来完成,效率上可能略逊一筹
楼上的方法本质和楼主的方法一致,只是当分组不那么明显的时候,楼主的方法更具一般性
一个小建议,使用if-else语句可能效率较之if-if语句更好。
proc sql;
15 select distinct color into : dslist separated by " "
16 from test;
17 run;
NOTE: PROC SQL statements are executed immediately; The RUN statement has no effect.
18
NOTE: PROCEDURE SQL used (Total process time):
real time 0.36 seconds
cpu time 0.12 seconds
19 data _null_;
20 if _n_=1 then call execute("data &dslist; set test;");
21 set test end=last;
22 by color;
23 if first.color then call execute("if color="||quote(strip(color))||" then output "||color||";");
24 if last then call execute("run;");
25 run;
NOTE: There were 8 observations read from the data set WORK.TEST.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.01 seconds
NOTE: CALL EXECUTE generated line.
1 + data blue green red; set test;
2 + if color="blue" then output blue ;
3 + if color="green" then output green ;
4 + if color="red" then output red ;
5 + run;
NOTE: There were 8 observations read from the data set WORK.TEST.
NOTE: The data set WORK.BLUE has 3 observations and 2 variables.
NOTE: The data set WORK.GREEN has 2 observations and 2 variables.
NOTE: The data set WORK.RED has 3 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
遍历三次就可以了,第一次产生宏变量,第二次产生call execute的代码,第三次输出到不同的数据集。
楼主的方法是两次遍历,第一次产生宏变量,第二次分组输出到不同数据集。
If ... then ...; else if... then ...; 最基本的提高效率的方法,受教了。