|
我觉得比较好理解的思路就是计算出每个subjn下面要有几个group,然后根据group的数量将相应的记录复制,就像10楼bobguy的思路。不过比较tricky的地方是要保证同一个subjn下每个group都是不同的,所以记录复制后有些记录的顺序要稍微调整。bobguy似乎没有考虑到这个,它的结果应该会出现同一个subjn下有重复的group。
data test1 (drop=total) test2 (keep=subjn dt id ) test3 (keep=subjn total);
set test;
by subjn dt;
retain total;
if first.dt then id=1 ;
else id+1;
if last.dt then output test2;
if first.subjn then do;
total=id;
end;
else if last.dt then total=total*id;
if last.subjn then output test3;
output test1;
run;
proc sql;
create table test4 as
select t1.*,
t3.total,
t3.total/t2.id as signal
from test1 as t1,
test2 as t2,
test3 as t3
where t1.subjn=t2.subjn=t3.subjn and
t1.dt=t2.dt;
quit;
data test5;
set test4;
by subjn dt;
retain times;
if first.subjn then times=0;
if first.dt and signal^=total then times+1;
if not first.dt and times=1 and signal>1 then do;
start=signal*(id-1)+1;
stop=signal*id;
end;
if start=. then do i=1 to signal;
output;
end;
else do i= start to stop;
output;
end;
run;
proc sort data=test5;
by subjn dt i;
run;
data test6;
set test5 (keep= subjn dt Rx x i);
by subjn dt;
if first.dt then id=1;
else id+1;
drop i;
run;
proc sort data=test6 out=final;
by subjn id dt;
run;
proc print;
proc datasets lib=work;
delete test1-test6;
run;
|