请选择 进入手机版 | 继续访问电脑版
楼主: whymath
2639 19

[问答] SAS数据处理题目 [推广有奖]

yuan_wang 发表于 2018-8-19 00:07:40 |显示全部楼层 |坛友微信交流群
whymath 发表于 2018-8-18 11:01
明白了!
whymath 水平高,又热心肠,为你点赞

使用道具

yuan_wang 发表于 2018-8-19 00:15:03 |显示全部楼层 |坛友微信交流群
  1. %put Hi, whymath.;
  2. %put Advanced mode at bbs.pinggu;
复制代码


使用道具

whymath 发表于 2019-5-28 00:02:00 |显示全部楼层 |坛友微信交流群
十个多月后,我已经可以解决此问题:
  1. data test;
  2.     input Group$;
  3.     cards;
  4.     A
  5.     A
  6.     A
  7.     A
  8.     B
  9.     B
  10.     B
  11.     B
  12.     B
  13.     B
  14.     C
  15.     C
  16.     C
  17.     ;
  18. run;

  19. data test;
  20.     set test nobs=nobs;

  21.     Count = .;
  22.     do i = 1 to nobs;
  23.         set test(rename=Group=GroupTmp) point=i;
  24.         if Group = GroupTmp then Count + 1;
  25.     end;
  26.     drop GroupTmp;
  27. run;
复制代码
另从Overflow上见到他山之玉,并记于此:
  1. proc summary data = test nway order=freq missing;
  2.     class group;
  3.     output out=test1(drop=_type_ index=(group)) / levels;
  4. run;

  5. data test2;
  6.     merge test test1;
  7.     by group;
  8. run;
复制代码


SASUSER

使用道具

whymath 发表于 2022-4-18 18:47:47 |显示全部楼层 |坛友微信交流群
Method2:
  1. data test;
  2.   do until(last.group);
  3.     set test;
  4.     by group;
  5.     count+1;
  6.   end;

  7.   do until(last.group);
  8.     set test;
  9.     by group;
  10.     output;
  11.   end;
  12. run;
复制代码

使用道具

whymath 发表于 2022-4-18 18:50:42 |显示全部楼层 |坛友微信交流群
Method1.1:
  1. data test(drop=tmpgroup);
  2.   set test;

  3.   do i=1to rec;
  4.     set test(rename=group=tmpgroup) nobs=rec point=i;
  5.     count=sum(count,group=tmpgroup);
  6.   end;
  7. run;
复制代码

使用道具

whymath 发表于 2022-4-18 19:18:18 |显示全部楼层 |坛友微信交流群
Method3:
  1. data test;
  2.   input Group$;
  3.   cards;
  4.   A
  5.   A
  6.   A
  7.   A
  8.   B
  9.   B
  10.   B
  11.   B
  12.   B
  13.   B
  14.   C
  15.   C
  16.   C
  17.   ;
  18. run;

  19. data want;
  20.   set test;

  21.   array _par_[&sysnobs.]$_temporary_;
  22.   array _cnt_[&sysnobs.]_temporary_;
  23.   if _n_=1 then do until(eof);
  24.     set test(rename=group=tmpgroup) end=eof;
  25.     do i=1 to dim(_par_) until(_par_[i]=tmpgroup);
  26.       if _par_[i]='' then _par_[i]=tmpgroup;
  27.       _cnt_[i]+(_par_[i]=tmpgroup);
  28.     end;
  29.   end;

  30.   do i=1 to dim(_par_) until(group=_par_[i]);
  31.     count=_cnt_[i];
  32.   end;
  33. run;
复制代码

使用道具

whymath 发表于 2022-4-18 19:56:46 |显示全部楼层 |坛友微信交流群
Method4:
  1. data want(drop=rc);
  2.   set test;

  3.   rc=open("test(where=(group='"||trim(group)||"'))");
  4.   if rc then do;
  5.     count=attrn(rc,'nlobsf');
  6.     rc=close(rc);
  7.   end;
  8. run;
复制代码

使用道具

whymath 发表于 2022-4-18 20:06:33 |显示全部楼层 |坛友微信交流群
Method5:
  1. data want(drop=rc);
  2.   set test;

  3.   rc=dosubl("
  4.     data _null_;
  5.       set test(where=(group='"||trim(group)||"') keep=group) end=eof;
  6.       call symputx('n_"||cats(_n_)||"',_n_);
  7.     run;
  8.   ");
  9.   count=symget('n_'||cats(_n_));
  10. run;
复制代码

使用道具

whymath 发表于 2022-4-18 20:39:02 |显示全部楼层 |坛友微信交流群
Method6:
  1. data want;
  2.   set test;

  3.   dcl hash h(dataset:"test(where=(group='"||strip(group)||"'))", multidata:'yes');
  4.   h.definekey('group');
  5.   h.definedone();
  6.   count=h.num_items;
  7. run;
复制代码

使用道具

whymath 发表于 2022-4-18 20:50:07 |显示全部楼层 |坛友微信交流群
Method6.1:
  1. data want(drop=rc);
  2.   set test;

  3.   if _n_=1 then do;
  4.     dcl hash h(dataset:'test',multidata:'yes');
  5.     h.definekey('group');
  6.     h.definedone();
  7.   end;
  8.   rc=h.find();
  9.   do while(rc=0);
  10.     count=sum(count,1);
  11.     rc=h.find_next();
  12.   end;
  13. run;
复制代码


使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 08:07