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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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