如何合并两个不同长度的数据集?

时间:2017-09-24 03:25:57

标签: sas

我想合并2个不同维度的数据集。

TABLE1: people
gender name
M raa
F chico
M july
F sergio


TABLE2: serial_numbers
gender serial
M 4
F 5

我希望结果是

result
gender name serial
M raa 4
F chico 5
M july 4
F sergio 5

1 个答案:

答案 0 :(得分:2)

我在这里创建数据集来说明如何合并两个数据集:

data people;
infile cards;
length gender $1
       name   $10;
input gender name;
cards;
M raa
F chico
M july
F sergio
;
run;


data serial_numbers;
length gender $1
       serial  8;
infile cards;
input gender serial;
cards;
M 4
F 5
;
run;

解决方案1:使用proc sql执行连接。

proc sql;
create table result as
select a.gender, a.name, b.serial
from people a LEFT JOIN serial_numbers b
on a.gender=b.gender;
quit;

proc print data=result;
run;

解决方案2:使用数据步骤合并两个数据集。这需要对数据集进行排序:

proc sort data=people;
by gender;
run;

proc sort data=serial_numbers;
by gender;
run;

data result;
merge people serial_numbers;
by gender;
run;

proc print data=result;
run;