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

I am trying to merge two data sets. Dataset1 has multiple observations for each id, while dataset2 has a single row of observations for each id. I want to merge the data sets so that the observations in dataset2 are duplicated into each id in dataset1. 

 

For example if this is the data set I have:

 

dataset1;
input id $ weight age;
cards;
1 108 23
1 114 24
1 118 25
2 100 45
2 108 46
2 110 47
;
run;

dataset2;
input id $ income married $ smoking $
cards;
1 40,000 y y
2 58,000 y n
;
run;

 

 

I want to combine so that the final data set looks like this:

 

datafinal;
input id $ weight age income married $ smoking $;
cards;
1 108 23 40,000 y y 
1 114 24 40,000 y y 
1 118 25 40,000 y y 
2 100 45 58,000 y n
2 108 46 58,000 y n
2 110 47 58,000 y n
;
run;

 

 

I tried doing

 

data combined;
     merge dataone datatwo;
run;

 

but what I ended up was with 

datafinal;
input id $ weight age income married $ smoking $;
1 108 23 40,000 y y 
1 114 24 
1 118 25 
2 100 45 58,000 y n
2 108 46 
2 110 47 

because the data from dataset2 was not copied into each id in dataset1. Is there a way that I can duplicate data while merging two sets? 

 

Thanks! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data set1;
input id $ weight age;
cards;
1 108 23
1 114 24
1 118 25
2 100 45
2 108 46
2 110 47
;
run;

data set2;
input id $ income :comma10. married $ smoking $;
cards;
1 40,000 y y
2 58,000 y n
;
run;

data want;
 merge set1 set2;
 by id;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data set1;
input id $ weight age;
cards;
1 108 23
1 114 24
1 118 25
2 100 45
2 108 46
2 110 47
;
run;

data set2;
input id $ income :comma10. married $ smoking $;
cards;
1 40,000 y y
2 58,000 y n
;
run;

data want;
 merge set1 set2;
 by id;
run;
yabwon
Onyx | Level 15

use BY:

data set1;
input id $ weight age;
cards;
1 108 23
1 114 24
1 118 25
2 100 45
2 108 46
2 110 47
;
run;

data set2;
input id $ income married $ smoking $;
cards;
1 40000 y y
2 58000 y n
;
run;

data combined;
     merge set1 set2;
     by id;
run;
proc print;
run;

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



KaueMAlmeida
Obsidian | Level 7

Hi,

 

Try doing a LEFT JOIN instead of MERGE. Use the column ID for doing the JOIN function.

 

 

Tom
Super User Tom
Super User

Many to one merges work fine with data step MERGE statement.

But you have to tell it what variable(s) to use to match them.

That is done with the BY statement.

You should have seen a note in the log warning that you forgot to include the BY statement.

Note that both datasets need to be sorted by the BY variable(s).

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
  • 4 replies
  • 854 views
  • 0 likes
  • 5 in conversation