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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5928 views
  • 0 likes
  • 2 in conversation