BookmarkSubscribeRSS Feed
newsas007
Quartz | Level 8

Hi all,

I have several data sets i am trying to merge using a standard dataset that contains IDs i need for the final merge. Here is an example: dataset A and dataset B need to be merged. Dataset C is the 'standard' dataset that has just ID #s that i need in the final merge.  Also, is it better to use SQL or just data step? Thanks

Dataset A

IDvar1var2
2Cat12
3cat24
4Cat16
6cat28
7Cat10
9cat210
10cat112

 

Dataset B

IDVar3
11.5
23.6
35.3
57.9
79.2
92.5
102.9

 

Dataset C

ID
2
3
4
7
9
10

 

Data want

IDvar1var2Var3
2Cat123.6
3cat245.3
4Cat16 
7Cat109.2
9cat2102.5
10cat1122.9
6 REPLIES 6
Kurt_Bremser
Super User

Since the datasets are already sorted, and have a 1:1 relationship throughout, the data step is the method of choice:

data want;
merge
  a
  b
  c (in=in_c)
;
by id;
if in_c;
run;
newsas007
Quartz | Level 8
Thank you!
novinosrin
Tourmaline | Level 20

data a;
input ID	var1 $	var2;
cards;
2	Cat1	2
3	cat2	4
4	Cat1	6
6	cat2	8
7	Cat1	0
9	cat2	10
10	cat1	12
;


 

data B;
input ID	Var3;
cards;
1	1.5
2	3.6
3	5.3
5	7.9
7	9.2
9	2.5
10	2.9
;
 

data C;
input ID;
cards;
2
3
4
7
9
10
;

data want;
merge c(in=c) a b;
by id;
if c;
run;
newsas007
Quartz | Level 8
This is great! Thanks a lot!
Just in case i had to select variables, would it be better to use SQL?
novinosrin
Tourmaline | Level 20

Yes, you could use SQL Select or datastep keep/drop dataset options. The choice is yours.

 

Sir @Kurt_Bremser / @Tom  or @Reeza  can offer better slick advice if your question is related to "performance"

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