楼主: Imasasor
8733 31

怎么利用hash或sql将不同观测按要求重新组合? [推广有奖]

31
semenljw 在职认证  发表于 2013-3-17 10:43:10
经典,呵呵。

32
hongxx 发表于 2013-3-22 13:05:50
思路:计算需要重复的记录的重复次数,然后为每一个记录分配到某一个组中。代码看起来简短一些。
  1. data test;
  2. input subjn dt Rx $ x;
  3. cards;
  4. 1 1 A 11
  5. 1 2 B 12
  6. 1 3 C 13
  7. 2 3 A 21
  8. 2 7 C 22
  9. 2 7 B 23
  10. 2 12 D 24
  11. 3 1 B 31
  12. 3 1 C 32
  13. 3 5 A 33
  14. 3 8 B 34
  15. 3 11 B 35
  16. 3 11 D 36
  17. 3 11 E 37
  18. ;
  19. proc sql;
  20.         create table  test_sorted as
  21.         select *, count(*) as rx_types
  22.         from test
  23.         group by subjn, dt
  24.         order by subjn, dt;
  25. quit;
  26. data test_howmany_group;
  27.         set test_sorted;
  28.         retain howmany_group;
  29.         if first.subjn then howmany_group=1;
  30.                 if first.dt then howmany_group=howmany_group*rx_types;
  31.         if last.subjn then output;
  32.         by subjn dt;
  33. run;
  34. proc sql;
  35.         create table test_howmany_group2 as
  36.         select a.*,b.howmany_group
  37.         from test_sorted a , test_howmany_group b
  38.         where a.subjn=b.subjn
  39.         order by subjn,dt;
  40. quit;

  41. data test_goup;
  42.         set test_howmany_group2;
  43.         retain group_interval;
  44.         if first.dt then group_interval=0;
  45.         do i=1 to howmany_group/rx_types;
  46.                 if i=1 then group_interval=group_interval+1;
  47.                 group=group_interval+rx_types*(i-1);                /*将记录输出复制howmany_group/rx_types次,为每次复制赋值group标志*/
  48.                 output;
  49.         end;
  50.         by subjn dt;
  51.         drop i;
  52. run;
  53. proc sort data=test_goup(drop=rx_types group_interval howmany_group) out=test_goup_final;
  54.         by subjn group dt;
  55. run;
复制代码
已有 1 人评分经验 论坛币 学术水平 收起 理由
Imasasor + 100 + 100 + 4 对论坛有贡献

总评分: 经验 + 100  论坛币 + 100  学术水平 + 4   查看全部评分

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

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