Help using Base SAS procedures

Merging 2 SAS Datasets by 2 Variables

Reply
N/A
Posts: 0

Merging 2 SAS Datasets by 2 Variables

Hello all,

I want to merge 2 datasets by 2 variables.
Here is my example:

Dataset 1
ID SubID
10 1
10 3
10 5
20 2
20 4

Dataset 2
ID SubID Emp
10 1 10
10 2 20
10 3 30
10 4 40
10 5 50
20 1 10
20 2 20
20 3 30
20 4 40
20 5 50

I want my output to look like this:

ID SubID Emp
10 1 10
10 3 30
10 5 50
20 2 20
20 4 40

Thanks in advance for helping me.

Andy
Super User
Posts: 9,671

Re: Merging 2 SAS Datasets by 2 Variables

There are usually two ways to get it.
One way: using data step

[pre]
data dataset1;
input id subid ;
datalines;
10 1
10 3
10 5
20 2
20 4
;

data dataset2;
input id subid emp ;
datalines;
10 1 10
10 2 20
10 3 30
10 4 40
10 5 50
20 1 10
20 2 20
20 3 30
20 4 40
20 5 50
;

data new;
merge dataset2(in=two) dataset1(in=one) ;
by id subid;
if one and two;
run;
proc print data=new noobs;
run;
[/pre]


Another way: using proc sql

[pre]
proc sql;
create table another as
select one.id,one.subid,two.emp
from dataset1 as one,dataset2 as two
where one.id=two.id and one.subid=two.subid
;
quit;
proc print data=another noobs;
run;
[/pre]


The code above are working.
N/A
Posts: 0

Re: Merging 2 SAS Datasets by 2 Variables

Thanks!
Ask a Question
Discussion stats
  • 2 replies
  • 175 views
  • 0 likes
  • 2 in conversation