BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
Ksharp
Super User
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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 6351 views
  • 0 likes
  • 2 in conversation