我有两个数据集需要合并。
第一个是包含 studyid 和 discharg(患者出院日期)的大数据集。
第二个观测值比第一个观测值少。它们有两列:studyid 和 call_mad(护士在出院日期后给患者打电话的日期)。 并非所有出院都会接到护士的电话。
第一个表是
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
第二个表是
STUDYID CALL_MAD
10011 2009-10-19
10011 2010-1-25
我想要的决赛 table
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
希望一切都清楚了。提前致谢。
简
最佳答案
我还没有对此进行过多次测试,但我认为它应该可以正常工作。我不是 SAS 专家。
它假设您的两个数据集名为“first”和“second”:
/* set unique identifiers */
data first;
set first;
keyida = _N_;
run;
data second;
set second;
keyidb = _N_;
run;
中级看数据:
首先看起来像:
study discharg keyida
10011 10/29/2008 1
10011 11/07/2008 2
10011 11/18/2008 3
10011 10/17/2009 4
10011 01/02/2010 5
10011 01/22/2010 6
第二个看起来像:
study discharg keyidb
10011 10/19/2009 1
10011 01/25/2010 2
现在合并并清理:
/* merge the data on id and call made after discharge */
proc sql;
create table final as
select a.studyid,
a.discharg,
a.keyida,
b.studyid,
b.call_mad,
b.keyidb
from first a,
second b
where a.studyid = b.studyid and
b.call_mad > a.discharg
order by keyida;
quit;
/* remove duplicates of original dataset */
proc sort data=final nodupkey;
by keyida;
run;
/* sort by secondary id and date of calling */
proc sort data=final;
by call_mad
keyidb;
run;
/* remove duplicates of the secondary dataset */
data final (drop = keyida keyidb);
set final;
by call_mad
keyidb;
if not (last.call_mad and last.keyidb) then call_mad =.;
run;
最终数据集如下所示:
study discharg call_mad
10011 10/29/2008 .
10011 11/07/2008 .
10011 10/17/2009 10/19/2009
10011 11/18/2008 .
10011 01/02/2010 .
10011 01/22/2010 01/25/2010
关于sas - 少对多合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9777404/