BookmarkSubscribeRSS Feed
echoli
Obsidian | Level 7
 

Hi All,

 

I have two datasets (A and B) with the same variable names ID, Test 1, Test2.

I want to merge A and B but if A and B have the same ID, than use records in A, not in B.

 

I tried to use left join, seems not correct. Any cues?

 

Thanks,

 

 

data a;
	input ID test1 test2;
	datalines;
123 33 55
372 23 48
763 13 85
;

data b;
	input ID test1 test2;
	datalines;
568 41 36
587 23 48
763 25 69
;


 

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Please post sample data in the form of a data step

Astounding
PROC Star

You're leaving out many important details.

 

Does an ID ever appear more than once in the same data set?

 

If an ID appears in B but not in A, do you still want to keep that ID?

 

If an ID appears in both data sets A and B, but TEST1 and TEST2 have missing values in A, do you want to use the values in B?

 

Do your actual data sets really contain just 3 variables, or do they contain many more than 3?

 

In all likelihood, the programming will be simple.  The difficult part is figuring out what the results should be.

echoli
Obsidian | Level 7

Thanks for the answer, I've provide a sample data. 

Reeza
Super User

@echoli wrote:

Thanks for the answer, I've provide a sample data. 


There's no data in the post?

echoli
Obsidian | Level 7
data a;
	input ID test1 test2;
	datalines;
123 33 55
372 23 48
763 13 85
;

data b;
	input ID test1 test2;
	datalines;
568 41 36
587 23 48
763 25 69
;


can you see it now?Smiley Happy

Spoiler
 
ballardw
Super User

If the ID is not duplicated in either set then this may do what you want;

/* data step merge by requires sorted data*/
proc sort data=a;
  by id;
run;
proc sort data=b;
by id;
run;

data want;
   merge b a;
   by id;
run;

Sorts included even though example data already sorted just in case.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2055 views
  • 0 likes
  • 5 in conversation