BookmarkSubscribeRSS Feed
Dritan007
Fluorite | Level 6

Hello guys.
I need to merge these 2 sets to get the below result:

Dritan007_0-1607955921770.png

Dritan007_1-1607955948512.png

Dritan007_2-1607955997013.png

The result I want is in third table but I can not get it with merge statement?

Thank you.

 

 

3 REPLIES 3
hhinohar
Quartz | Level 8

Try hash.

Please send data not in a picture but as a sas code.

 

data one;
  infile datalines dlm="09"x;
	input x y;
datalines;
1	10
1	11
1	15
2	34
2	30
3	40
;
run;

data two;
  infile datalines dlm="09"x;
	input x z;
datalines;
1	100
2	150
3	300
;
run;

data three;
	if _N_=1 then do;
		if 0 then set one;
		dcl hash h(dataset:"one",multidata:"y");
		h.definekey("x");
		h.definedata("y");
		h.definedone();
	end;
	set two;
	do while(h.do_over()=0);
		output;
	end;
run;
Astounding
PROC Star

MERGE is exactly the right thing to do.  You are probably forgetting to add a BY statement:

data want;
   merge top_dataset bottom_dataset;
   by x;
run;
ballardw
Super User

Merge works fine for me

data one;
	input x y;
datalines;
1	10
1	11
1	15
2	34
2	30
3	40
;
run;

data two;
	input x z;
datalines;
1	100
2	150
3	300
;
run;

data want;
  merge one two;
  by x;
run;

However, if your actual data has multiple records with the same By value in both data sets the log will say so and the merge is likely not what you want.

 

When you say something like "but I can not get it with merge statement" you should include the code you tried. Sometimes you may be close, sometimes using the wrong approach entirely. Making us guess what you may have tried is like pulling hen's teeth.

Best is to show us the code used by copying from your log the entire procedure or data step code will all the associated notes, warnings or other messages and then pasting into a text box opened on the forum with the </> icon to preserve formatting.

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