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.
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!
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.