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 Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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