Solved
Contributor
Posts: 22

Can we use data steps for many to many merge.

Can we use data steps for many to many merge.

Accepted Solutions
Solution
‎04-25-2016 03:49 PM
Super User
Posts: 23,778

Re: Can we use data steps for many to many merge.

All Replies
Solution
‎04-25-2016 03:49 PM
Super User
Posts: 23,778

PROC Star
Posts: 1,460

Re: Can we use data steps for many to many merge.

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.

🔒 This topic is solved and locked.