Hi Guys!
Quick question on merging and joining datasets:
If I have two datasets and if they both have a matching obs (by a certain key), how can i output both observations from each dataset into my output dataset please?
Do those datasets have the same structure (same variables)?
See these simple example tables:
data a;
input x y;
datalines;
1 2
2 3
3 4
;
data b;
input x y;
datalines;
1 3
2 4
4 6
;
You can either merge them and use a RENAME= option:
data want1;
merge
a (in=a)
b (in=b rename=(y=_y))
;
by x;
if a and b;
run;
Or you can stack them:
data want2;
set
a (in=a)
b (in=b)
;
by x;
if first.x ne last.x;
run;
Mind that this works only if there's maximally 1 observation for a key in each dataset.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.