Saturday, September 22, 2007

不要总是用宏来解决问题

发信人: raze (calm+down), 信区: Statistics
标 题: 请问谁能帮我简化下sas的code?多谢。
发信站: BBS 未名空间站 (Fri Sep 21 20:38:41 2007), 转信

我有一组数据如下,data one。我已经算出了class1=1的所有id相同的体重和身高的和
,并且存入了名字为class1_sum的文件中。然后我用同样的方法算了class2=1,class3=
1,class4=1的所有id相同的人的体重和身高的和,分别生成了另外的三个文件。
怎么样能有用一次sql或者其他命令,达到以上目的呢?多谢。
data one;
input id obs weight height class1 class2 class3 class4;
datalines;
1 1 2 3 1 0 1 1
1 2 3 4 1 1 0 0
2 1 4 5 0 1 1 1
2 2 5 6 1 1 1 1
3 1 1 2 1 1 0 1
4 1 3 5 1 0 1 0
;run;

我的解决方案见图,自认为比用宏的方法好,效率更高。


single-step


SQL solution
--------------------------
data data1;
input id1 $ id2 $ month year ;
datalines;
1 a 3 1978
2 a 4 1977
3 a 2 1978
4 b 3 1977
5 b 2 1977
6 c 3 1977
7 c 9 1991
8 b 7 1974
9 a 6 1980
;


proc sql;
create table new as
select a.id2, a.id1, a.month, a.year,
sum((b.id2=a.id2)&
((b.month<=a.month & b.year=a.year)|(b.month>a.month & b.year=a.year-1)))-1
as Freq
from data1 as a, data1 as b
group by a.id2, a.id1, a.month, a.year;
quit;

0 comments: