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.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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