BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
leahcho
Obsidian | Level 7

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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.

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;
leahcho
Obsidian | Level 7

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

PeterClemmensen
Tourmaline | Level 20

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

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
  • 3 replies
  • 915 views
  • 2 likes
  • 2 in conversation