Hi I need to merge two datasets but not sure if I can do it with data step.
My datasets look like this;
Dataset 1 Dataset 2
ID Var1 ID var2
1 a 2 a
2 b 5 b
3 c 3 c
4 d 4 d
When I merge these two datasets, I only want to keep all IDs from dataset 1 and add from dataset 2 if IDs match.
So I want a final dataset to look like this;
Data_merged
ID var1 var2
1 a
2 b a
3 c c
4 d d
Thanks much!
Yes. Except you don't need your in=b in this case and you have to make sure that the two data sets are properly sorted.
Like this?
data Dataset_1;
input ID$ Var1$;
datalines;
1 a
2 b
3 c
4 d
;
data Dataset_2;
input ID$ var2$;
datalines;
2 a
5 b
3 c
4 d
;
/* Data Step Approach */
proc sort data=Dataset_1; by ID; run;
proc sort data=Dataset_2; by ID; run;
data merged;
merge Dataset_1(in=a) Dataset_2;
by ID;
if a;
run;
/* Proc SQL Approach */
proc sql;
create table joined as
select *
from Dataset_1 left join Dataset_2
on Dataset_1.ID = Dataset_2.ID ;
quit;
Hi,
I tried
data want;
merge data1 (in=a) data2 (in=b);
by id;
if a;
run;
This is the same as what you suggested?
Thanks
Yes. Except you don't need your in=b in this case and you have to make sure that the two data sets are properly sorted.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.