BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ramanuj_Khatri
Obsidian | Level 7

Can we use data steps for many to many merge.

1 ACCEPTED SOLUTION
2 REPLIES 2
Quentin
Super User

My guesss is @Reezacorrectly interpretted your questions to be "Can I use the MERGE statement to get a cartesian product from a many to many merge."  And for that, the answer is no.

 

Unfortunately, the merge statement will do a many-to-many merge, but the results are NOT a cartesian product.  In fact, I've never seen an example where somebody actually wanted the result of a many-to-many merge.  But it's good to know that merge will not error in this setting, and that the note about more than one dataset with repeats of BY values should be treated as an error.  (Unfortunately I don't know a system option to make it an error).

 

524  data a;
525    do id=1,1,2;
526      a=cats('a',id);
527      output;
528    end;
529  run;

NOTE: The data set WORK.A has 3 observations and 2 variables.

530
531  data b;
532    do id=1,1,1,2;
533      b=cats('b',id);
534      output;
535    end;
536  run;

NOTE: The data set WORK.B has 4 observations and 2 variables.

537
538  data c;
539    merge a b;
540    by id;
541    put (id a b)(=);
542  run;

id=1 a=a1 b=b1
id=1 a=a1 b=b1
id=1 a=a1 b=b1
id=2 a=a2 b=b2
NOTE: MERGE statement has more than one data set with repeats of BY values.
NOTE: There were 3 observations read from the data set WORK.A.
NOTE: There were 4 observations read from the data set WORK.B.
NOTE: The data set WORK.C has 4 observations and 3 variables.

 

 

As a side note, there are ways to use the DATA step to create a cartesian product, just not with the MERGE statement.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1365 views
  • 4 likes
  • 3 in conversation