楼主: Imasasor
8734 31

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

21
数据分析师3K 发表于 2012-12-25 15:32:46
唉  本版的声誉几乎都在这里了
已有 1 人评分经验 收起 理由
guo.bailing + 60 我很赞同

总评分: 经验 + 60   查看全部评分

22
清水边看水 发表于 2012-12-27 16:10:25

23
清水边看水 发表于 2012-12-28 18:51:15

24
hgswz 发表于 2012-12-29 21:46:41
学习了学习了学习了

25
nkplpdelei 发表于 2012-12-30 16:36:27
proc sql;
   drop table t2;
   drop table t2_only;
   drop table t2_else;
   drop table t3_1;
   drop table t3_2;
   drop table t4;
   drop table t5_only;
   drop table t5_else;
   drop table t6;
quit;
%end;
%mend;
%test();

26
denver 发表于 2013-1-20 23:25:48
这个帖子得收藏啊
Denver大家一起读Paper系列索引贴:
https://bbs.pinggu.org/thread-1430892-1-1.html

27
ohno小米 发表于 2013-1-21 17:26:19

28
GGOD 发表于 2013-2-1 04:27:59

29
erzi_yhj 发表于 2013-2-2 12:07:06
我觉得比较好理解的思路就是计算出每个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;

30
seajack 在职认证  发表于 2013-2-22 11:13:06
不错

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

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