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

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