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.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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