楼主: crackman
15195 47

跟crackman读SAS程序(15)--如何将宏变量按照字母顺序排列 [推广有奖]

11
pobel 在职认证  发表于 2010-8-24 17:24:23
data one;
  input var1 $ var2;
datalines;
A 10
B 22
C 5
D 41
E 33
;
run;


data two;
   set one;
   call missing(next_var2); /*set next_var2 to missing because variable value are automatically retained*/
   if not last then do;   
       set one(firstobs=2 keep=var2 rename=(var2=next_var2)) end=last; /*reread dataset one from the second observation*/
       dif=var2-next_var2;
        end;
run;
和谐拯救危机

12
小马过河abc 发表于 2010-8-24 21:35:45
不好意思 不懂。。。。。。。。。。。。。
我思故我在

13
crackman 发表于 2010-8-24 22:04:58

跟crackman读SAS程序(20)----SQL中的内嵌查询结果

proc sql outobs=20;
title ’Job Groups with Average Salary’;
title2 ’> Company Average’;
select jobcode,
avg(salary) as AvgSalary format=dollar11.2,
count(*) as Count
from sasuser.payrollmaster
group by jobcode
having avg(salary) >
(select avg(salary)
from sasuser.payrollmaster)
order by avgsalary desc;

这个里面主要是注意一点:
就是HAVING语句中的AVG(salary)比较是全部数据中的薪水的平均值,而AVG(SALARY)是按照分组计算出来的薪水的平均值,这里将select avg(salary)
from sasuser.payrollmaster的结果做比较的对象。
已有 1 人评分学术水平 热心指数 收起 理由
pobel + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

14
soporaeternus 发表于 2010-8-24 22:19:04
楼主的代码一个data步输出到多个datasets,原表仅遍历一次
pobel的代码可能需要多个data步遍历原表多次来完成,效率上可能略逊一筹
楼上的方法本质和楼主的方法一致,只是当分组不那么明显的时候,楼主的方法更具一般性

一个小建议,使用if-else语句可能效率较之if-if语句更好。
已有 1 人评分学术水平 热心指数 收起 理由
pobel + 1 + 1 好的意见建议

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

Let them be hard, but never unjust

15
pobel 在职认证  发表于 2010-8-25 07:12:23
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 ...; 最基本的提高效率的方法,受教了。
和谐拯救危机

16
crackman 发表于 2010-8-25 08:58:08
你起得好早哦
兄弟

17
soporaeternus 发表于 2010-8-25 09:10:05
5# pobel
对的,你的输出也是遍历全表一次,我看错代码了......,和crackman的一样的......
Let them be hard, but never unjust

18
pobel 在职认证  发表于 2010-8-25 09:27:40
crackman 发表于 2010-8-25 08:58
你起得好早哦
兄弟
[biggrin]
和谐拯救危机

19
pobel 在职认证  发表于 2010-8-25 09:30:52
soporaeternus 发表于 2010-8-25 09:10
5# pobel
对的,你的输出也是遍历全表一次,我看错代码了......,和crackman的一样的......
[biggrin]
和谐拯救危机

20
viterbi 发表于 2010-8-25 11:35:03
内嵌查询,看起来很完美,但实际上应用起来非常困惑,实在慢的不行,特别当你运行千万数量级的数据的时候!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-25 05:26