BookmarkSubscribeRSS Feed
AmitKB
Fluorite | Level 6
Hi all,
I want to merge 2 datasets where one of the by variables could be equal to any of the 2 variables in the other dataset

Eg

Dataset A variables: ID ,Amt, Serial1
Dataset B variables: ID ,Amt, SerialA, SerialB

The by variable Serial1 in datasetA could by same as SerialA or SerialB. ID is also a by variable.

Thanks,

Amit
2 REPLIES 2
DanielSantos
Barite | Level 11
You could easily do this with a single SQL statement (where SERIAL1 = SERIALA or SERIAL1 = SERIALB).

For a datastep merge solution, you should do a 3 table merge, being the third table a copy of dataset B. Assuming that dataset A is sorted by SerialA, it would look like this;

data BB; /* copy B to BB */
set B;
run;

proc sort; /* sort BB by SerialB */
by SerialB;
run;

data RESULT; /* do the 3 table merge */
merge A (in = a)
B (in = b rename = (SerialA=Serial1) drop = SerialB)
BB (in = bb rename = (SerialB=Serial1) drop = SerialA);
by Serial1;
if a and (b or bb); /* match by Serial1=SerialA or Serial1=SerialB */
run;

Greetings from Portugal.

Daniel Santos at www.cgd.pt
AmitKB
Fluorite | Level 6
Hi Daniel,
Thanks a lot for the solution. I will implement it both ways, to gain more confidence in proc sql ( trying to learning it)

Thanks.

Amit

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
  • 2 replies
  • 1050 views
  • 0 likes
  • 2 in conversation