连接2个sas表,并重复第二个值,直到它们对应于第一个表中的公共值为止



我有两个SAS数据集,在第一个表中,例如(表_1(:

Period IPC
201801 24
201801 45
201801 10
201801 20
201802 17
201802 18
201802 32

在第二个表中,我们有例如(表_2(:

Period CaR
201801 11
201802 49

目标是拥有一个类似(表3(的数据集:

Period IPC CaR
201801  24  11
201801  45  11
201801  10  11
201801  20  11
201802  17  49
201802  18  49
201802  32  49

我试过:

data table_3;
merge table_1 (in=a) 
table_2(in=b);
by Periode;
if a then output;
run;

但它没有起作用。

错误是:

ERROR: By variable are not properly sorted on data set table_1
warning: the data set table_3 may be incomplete. when this step was stopped there were 95 observations and 114 variables.

任何帮助都是非常值得的:(

为了避免排序,您也可以使用proc-sql来加入:

proc sql;
create table_3 as
select    a.*
,  b.*
from table_1 as a
left join table_2 as b
on a.period = b.period
;
quit;

在尝试合并数据集之前,请确保数据集已排序。

proc sort data=table_1 ;
by Periode;
run;
proc sort data=table_2 ;
by Periode;
run;
data table_3;
merge table_1 (in=a) 
table_2(in=b);
by Periode;
...

最新更新