Write and run SAS programs in your web browser

Merging datasets with repeated variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Merging datasets with repeated variables

Hello, I'm using SAS University edition / SAS studio.

I have two datasets with three variables item ID; quantity and price. The first data set has item ID and a unique quantity for that item. The second data set has item ID and price for that item - but the item ID and (same) price appear several repeated times.  

I just want a merged data set that has: item ID, quantity and one (same) price as the columns. How do I do this?

For example, see the code below:

 

/* Create sample data */
data one;
 input id $ quantity $;
 datalines;
Item_a 4
Item_b 3
Item_c 2;

 

data two;
 input id $ price $;
 datalines;
Item_a 1.10
Item_a 1.10
Item_b 2.35
Item_b 2.35
Item_c 5.50
Item_c 5.50
Item_c 5.50;

 

data both;
 merge one (in=in1) two (in=in2);
 by id;

 if in1=1 and in2=1;
run;

proc print data=both;
run;

  


Accepted Solutions
Solution
‎07-19-2016 07:24 PM
Super User
Posts: 19,064

Re: Merging datasets with repeated variables

Why does the second data set have duplicates? I would remove the duplicates and then merge. Your data step code will work. You can remove duplicates with proc sort and noduprecs or nodupkey. 

View solution in original post


All Replies
Solution
‎07-19-2016 07:24 PM
Super User
Posts: 19,064

Re: Merging datasets with repeated variables

Why does the second data set have duplicates? I would remove the duplicates and then merge. Your data step code will work. You can remove duplicates with proc sort and noduprecs or nodupkey. 

Occasional Contributor
Posts: 10

Re: Merging datasets with repeated variables

Thanks - that worked well.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 259 views
  • 1 like
  • 2 in conversation