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"

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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