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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 647 views
  • 0 likes
  • 2 in conversation