DATA Step, Macro, Functions and more

dataset merge

Reply
Frequent Contributor
Posts: 84

dataset merge

[ Edited ]
 

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
;


 

PROC Star
Posts: 764

Re: dataset merge

Please post sample data in the form of a data step

Super User
Posts: 5,516

Re: dataset merge

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.

Frequent Contributor
Posts: 84

Re: dataset merge

Posted in reply to Astounding

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

Super User
Posts: 19,860

Re: dataset merge


echoli wrote:

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


There's no data in the post?

Frequent Contributor
Posts: 84

Re: dataset merge

[ Edited ]
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
 
Super User
Posts: 11,343

Re: dataset merge

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.

 

Ask a Question
Discussion stats
  • 6 replies
  • 152 views
  • 0 likes
  • 5 in conversation