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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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