思路:计算需要重复的记录的重复次数,然后为每一个记录分配到某一个组中。代码看起来简短一些。
- data test;
- input subjn dt Rx $ x;
- cards;
- 1 1 A 11
- 1 2 B 12
- 1 3 C 13
- 2 3 A 21
- 2 7 C 22
- 2 7 B 23
- 2 12 D 24
- 3 1 B 31
- 3 1 C 32
- 3 5 A 33
- 3 8 B 34
- 3 11 B 35
- 3 11 D 36
- 3 11 E 37
- ;
- proc sql;
- create table test_sorted as
- select *, count(*) as rx_types
- from test
- group by subjn, dt
- order by subjn, dt;
- quit;
- data test_howmany_group;
- set test_sorted;
- retain howmany_group;
- if first.subjn then howmany_group=1;
- if first.dt then howmany_group=howmany_group*rx_types;
- if last.subjn then output;
- by subjn dt;
- run;
- proc sql;
- create table test_howmany_group2 as
- select a.*,b.howmany_group
- from test_sorted a , test_howmany_group b
- where a.subjn=b.subjn
- order by subjn,dt;
- quit;
- data test_goup;
- set test_howmany_group2;
- retain group_interval;
- if first.dt then group_interval=0;
- do i=1 to howmany_group/rx_types;
- if i=1 then group_interval=group_interval+1;
- group=group_interval+rx_types*(i-1); /*将记录输出复制howmany_group/rx_types次,为每次复制赋值group标志*/
- output;
- end;
- by subjn dt;
- drop i;
- run;
- proc sort data=test_goup(drop=rx_types group_interval howmany_group) out=test_goup_final;
- by subjn group dt;
- run;
复制代码