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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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