In SAS, many-to-many merges are handled very differently via Data Step and PROC SQL.
Let's take an example -
The DATA step does not handle many-to-many matching very well. When we perform many to many merges. the result should be a cartesian (cross) product. For example, if there are three records that match from one contributing data set to two records from the other, the resulting data set should have 3 × 2 = 6 records.
Suppose you have two data sets. You want to merge both the data sets but there are duplicate values in primary key.
![]() |
SAS - Many-to-many merge |
Data Step
MERGE does not create a cartesian product incase of a many-to-many relationship.It will create number of records for a duplicate value equal to maximum in both the table.SAS Code -
data dat1;input ID Info;cards ;1 31231 12342 74822 89123 1284;run;data dat2;input ID Info2;cards ;1 44441 55551 89892 90992 88883 8989;run;data combined;merge dat1 dat2 ;by ID;run;