SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 603|回复: 1
打印 上一主题 下一主题

few to many merge?

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2012-3-21 01:35:27 | 只看该作者

few to many merge?

I have two datasets need to merge.

The first one is a big dataset with[b:2iuotagz] studyid[/b:2iuotagz] and[b:2iuotagz] discharg[/b:2iuotagz](the date when the patients got their discharge).

The second one is the fewer observations than first one. They have two columns: [b:2iuotagz]studyid[/b:2iuotagz] and [b:2iuotagz]call_mad[/b:2iuotagz](the date when [u:2iuotagz][i:2iuotagz]nurse call the patient after discharge date[/i:2iuotagz][/u:2iuotagz]). Not all discharges get a call from nurse.

[u:2iuotagz][b:2iuotagz]The first table is
[/b:2iuotagz][/u:2iuotagz]
STUDYID   DISCHARG      

10011   2008-10-29         

10011   2008-11-7           

10011   2008-11-18         

10011   2009-10-17     

10011   2010-1-2      

10011   2010-1-22  

[u:2iuotagz][b:2iuotagz]The second table is
[/b:2iuotagz][/u:2iuotagz]
STUDYID        CALL_MAD

10011          2009-10-19
10011          2010-1-25  

[u:2iuotagz][b:2iuotagz]The final table I want[/b:2iuotagz][/u:2iuotagz]

STUDYID   DISCHARG      CALL_MAD

10011   2008-10-29         

10011   2008-11-7           

10011   2008-11-18         

10011   2009-10-17      2009-10-19

10011   2010-1-2      

10011   2010-1-22       2010-1-25  

Hopefully, it is clear. Thanks in advance.

Jane
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2012-3-28 04:03:11 | 只看该作者

Re: few to many merge?

[code:vjhnikr4]data a;
input studyid discharg yymmdd10.;
pk=_n_;
cards;
10011 2008-10-29
10011 2008-11-7
10011 2008-11-18
10011 2009-10-17
10011 2010-1-2
10011 2010-1-22
;
data b;
input studyid call_mad yymmdd10.;
cards;
10011 2009-10-19
10011 2010-1-25
;
run;
proc sql;
create table c as
   select d2.studyid, d2.discharg format=yymmdd10.,d1.call_mad format=yymmdd10.
     from (select *,min(abs(call_mad-discharg)) as diff
             from (select d1.*,call_mad
                     from a d1 left join b d2
                           on d1.studyid=d2.studyid)
                group by call_mad
           having diff>0 and call_mad-discharg=diff) d1 right join a d2
        on d1.discharg=d1.discharg and d1.pk=d2.pk;
quit;[/code:vjhnikr4]
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-6-4 18:49 , Processed in 0.067055 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表