BookmarkSubscribeRSS Feed
Feefee
Fluorite | Level 6

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?

 

3 REPLIES 3
Kurt_Bremser
Super User

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.

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
  • 729 views
  • 1 like
  • 2 in conversation