楼主: fyfzhdsfdx
7517 24

SAS根据多个列筛选有效数据 [推广有奖]

11
boe 发表于 2013-6-7 22:55:22
fyfzhdsfdx 发表于 2013-6-7 19:59
这些是原始数据,没有转置。
sas的数组我不太会用,能不能麻烦您写一下代码?不过这些列没有之间没有规律 ...
  1. data t1;
  2.     input c1-c8;
  3. datalines;
  4. 1 1 1 1 2 3 4 5
  5. 2 2 2 2 1 2 3 4
  6. 1 3 3 3 2 1 2 3
  7. 2 1 4 4 1 3 1 2
  8. 1 2 1 5 2 2 4 1
  9. ;
  10. run;

  11. /*111111111111111111111111111*/
  12. proc transpose data=t1 out=t2;
  13. run;
  14. data t02 t03 t04 t05;
  15.      set t2;
  16.          array cc c:;
  17.          do over cc;
  18.              if cc=max(of col:) then max=cc;
  19.          end;
  20.          if max=2 then output t02;
  21.      else if max=3 then output t03;
  22.          else if max=4 then output t04;
  23.          else output t05;
  24.          keep _NAME_;
  25. run;

  26. /*222222222222222222222222222*/
  27. proc means data=t1 noprint;
  28.     output out=t3(drop=_type_ _freq_) max=/keeplen;
  29. run;
  30. proc transpose data=t3 out=t4;
  31. run;
  32. data t002 t003 t004 t005;
  33.      set t4;
  34.          if col1=2 then output t002;
  35.      else if col1=3 then output t003;
  36.          else if col1=4 then output t004;
  37.          else output t005;
  38.          drop col1;
  39. run;
复制代码
Gorgeous girl , I love !

12
fyfzhdsfdx 发表于 2013-6-7 23:37:30
boe 发表于 2013-6-6 23:41
这是转置过的数据?
在转置之前,用数组可能也能操作。。。
下面的代码是我想实现的,求有没有简单的代码来实现那?
%macro valid(r);
%do i=1 %to &r;
data b&i;set a&i;
if (max(_col2, _col4, _col6, _col7, of _col18-_col25)in (1,2) and min(_col2, _col4, _col6, _col7, of _col18-_col25) in (1,2)) and
   (_col5 in(1,2,3)) and
   (max(_col3, _col8)in (1,2,3,4) and min(_col3, _col8)in (1,2,3,4)) and
   (max(of _col10-_col17)in (1,2,3,4,5) and min(of _col10-_col17) in (1,2,3,4,5)) and
   (_col9 in(1,2,3,4,5,6,7,8,9,10,11,12)) and
   (max(of A1-A24)in (1,2,3,4,5) and min(of A1-A24) in (1,2,3,4,5)) and
   (max(of B1-B30)in (1,2,3,4,5) and min(of B1-B30) in (1,2,3,4,5)) and
   (max(of C1-C68)in (1,2,3,4,5) and min(of C1-C68) in (1,2,3,4,5)) then output b&i;
run;
%end;
%mend valid;
%valid(20);
但是这个代码的最后结果会包含缺失值,我想把缺失值也给删除,怎么实现那?下面这个代码对吗?
%macro nonmiss(r);
%do i=1 %to &r;
data b&i;set b&i;
%do j=0 %to 25;if missing(_col&j) then delete;%end;
%do j=1 %to 24;if missing(A&j) then delete;%end;
%do j=1 %to 30;if missing(B&j) then delete;%end;
%do j=1 %to 68;if missing(C&j) then delete;%end;
run;
%mend nonmiss;
%nonmiss(20);
如果我想筛选出有效的数据(条件见上面的代码)并删除有缺失值得行,有没有代码可以同时实现?不用编2个宏了。
十分感谢!

13
fyfzhdsfdx 发表于 2013-6-7 23:43:00
boe 发表于 2013-6-7 22:52
楼主的意思是:在一个数据集中,把满足条件的变量找出来???不知道理解得对不对。
方法1先转置,数据量大 ...
是想把满足条件的记录全部输出,谢谢!

14
boe 发表于 2013-6-8 00:16:04
试试
  1. %macro valid(r);
  2. %do i=1 %to &r;
  3. data b&i;
  4.     set a&i;
  5.     where    1 le _col5 le 3    and    1 le _col9 le 12    and    not missing(_col0)    and    not missing(_col1);

  6.     array array1 _col2 _col4 _col6 _col7 _col18-_col25;
  7.     do over array1;
  8.          if 1 le array1 le 2;
  9.     end;

  10.     array array2 _col10-_col17;
  11.     do over array2;
  12.          if 1 le array2 le 4;
  13.     end;
  14.    
  15.     array array3 _col10-_col17 A1-A24 B1-B30 C1-C68;
  16.     do over array3;
  17.          if 1 le array3 le 5;
  18.     end;
  19. run;
  20. %end;
  21. %mend valid;
  22. %valid(20);
复制代码
Gorgeous girl , I love !

15
boe 发表于 2013-6-8 00:32:57
不知能不能用,能用的话效率会高些
  1. %macro valid(r);
  2. %do i=1 %to &r;
  3. data b&i;
  4.     set a&i;

  5.     where    1 le _col5 le 3    and    1 le _col9 le 12    and    not missing(_col0)    and    not missing(_col1);

  6.     array array1 _col2 _col4 _col6 _col7 _col18-_col25;
  7.     array array2 _col10-_col17;
  8.     array array3 _col10-_col17 A1-A24 B1-B30 C1-C68;
  9.           do over array1;
  10.                  if 1 le array1 le 2 then do;
  11.                         do over array2;
  12.                                 if 1 le array2 le 4 then do;
  13.                                         do over array3;
  14.                                                if 1 le array3 le 5;
  15.                                         end;
  16.                                 end;
  17.                         end;
  18.                 end;
  19.         end;
  20. run;
  21. %end;
  22. %mend valid;
  23. %valid(20);
复制代码
Gorgeous girl , I love !

16
fyfzhdsfdx 发表于 2013-6-9 23:53:23
boe 发表于 2013-6-8 00:32
不知能不能用,能用的话效率会高些
十分感谢您的及时和耐心回复!
您发表于00::6的那个和我代码跑出的结果相同,但下面那个00:32的,您说效率高的那个结果与我的不相同,结果相对于前面的偏多,但是没用error,为什么那?还有为什么下面的比上面运行效率高那?
对了sas里max(of _col18-_col25 _col2 _col4 _col6 _col7) 和max(of _col18-_col25 ,_col2, _col4, _col6 _col7)只是写法不同,结果是相同的,对吗?
谢谢!

17
boe 发表于 2013-6-10 00:32:02
fyfzhdsfdx 发表于 2013-6-9 23:53
十分感谢您的及时和耐心回复!
您发表于00::6的那个和我代码跑出的结果相同,但下面那个00:32的,您说效 ...
麻烦上传一个数据集。。。。
Gorgeous girl , I love !

18
fyfzhdsfdx 发表于 2013-6-10 11:12:10
boe 发表于 2013-6-10 00:32
麻烦上传一个数据集。。。。
20.xls (357 KB)
这是其中的一个数据集,十分感谢!

19
fyfzhdsfdx 发表于 2013-6-10 11:20:08
boe 发表于 2013-6-10 00:32
麻烦上传一个数据集。。。。
顺便提醒一下,您的条件有一部分写错了,应该是
where 1 le _col5 le 3 and 1 le _col9 le 12 and not missing(_col0) and not missing(_col1);
array array1 _col2 _col4 _col6 _col7 _col18-_col25;
do over array1;if 1 le array1 le 2;end;
array array2 _col3 _col8;
do over array2;if 1 le array2 le 4;end;
array array3 _col10-_col17 A1-A24 B1-B30 C1-C68;
do over array3;if 1 le array3 le 5;end;
十分感谢!

20
boe 发表于 2013-6-10 13:30:38
前面那个靠谱些,后面那个_col6 还有缺失值,具体原因未知。
还请大牛们给指点一下子。。。。。。
Gorgeous girl , I love !

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 08:25