SAS中文论坛

标题: SAS计数 [打印本页]

作者: shiyiming    时间: 2010-5-29 14:12
标题: SAS计数
弱问:如果有下面类似的数据集,如何分别计算I有多少个,2有多少个?以及它们分别对应的ID号?多谢各位指导菜鸟~!


data a;
id sex
1   1  
2   2
3   1
4   2
5   2
6   1
7   1
8   2
9   1

自己想的有个比较笨的方法,能分别求出1,2个数但没有指出对应的ID,还请LS及各位多指点~!
data a(drop = i);
array x[*] sex;
do i = 1 to dim(x);
  if x[i] = 1 then count1 + 1;
  if x[i] = 2 then count2 + 1;
end;
proc print;run;
作者: shiyiming    时间: 2010-5-29 14:52
标题: Re: SAS计数
[quote="naivearies":14071slk]以及它们分别对应的ID号?[/quote:14071slk]
把ID号拼接在一起?
[code:14071slk]data a(index=(sex));
        input id sex;
datalines;
1 1
2 2
3 1
4 2
5 2
6 1
7 1
8 2
9 1
;
data b(drop=id);
        length id_list $200;
        call missing(id_list,count);
        do _n_=1 by 1 until(last.sex);
                set a;
                by sex;
                id_list=ifc(_n_=1,put(id,best.),catx('/',id_list,put(id,best.)));
                count+1;
        end;
run;[/code:14071slk]
作者: shiyiming    时间: 2010-5-29 15:27
标题: Re: SAS计数
感谢LS的程序,自己还要多多加油啊~!再次多谢~!
作者: shiyiming    时间: 2010-5-30 10:48
标题: Re: SAS计数
data a;
input id sex @;
cards;
1 1
2 2
3 1
4 2
5 2
6 1
7 1
8 2
9 1
;
run;
data a1;
set a;
where sex=1;
proc sql noprint;
select id into:id1 separated by " " from a1;
quit;
data a2;
set a;
where sex=2;
proc sql noprint;
select id into:id2 separated by " " from a2;
quit;
%put "sex=1" id=&id1.;
%put "sex=2" id=&id2.;




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2