## Merging - many to many relationship using data step

Solved
Regular Contributor
Posts: 181

# Merging - many to many relationship using data step

Hi,

I have two datasets A and B which I am trying to join and the scenario is a many to many join.

I am confused on the join. Does each record of dataset A gets the matching last record from B? Is it possible to have a control here so that each record in dataset A is combined with all the matching records from B

Any help will be greatly appreciated.

data A;

input code \$ x y;

datalines;

AA 1 2

AA 3 4

AA 5 6

BB 7 8

CC 9 10

;

run;

data B;

input code \$ z k;

datalines;

AA 10 20

AA 20 40

BB 70 80

BB 99 77

CC 90 100

;run;

Proc sort data = A;

By code;

Run;

Proc sort data = B;

By code;

Run;

Data c;

Merge A(in =ina) B;

If ina;

Run;

The ooutput i get is

The Actual  output which I get is

AA          1              2              10           20

AA          3              4              20           40

AA          5              6              20           40

BB           7              8              70           80

BB           7              8              99           77

CC           9              10           90           100

Regards,

Sheeba Swaminathan

Accepted Solutions
Solution
‎09-07-2016 11:43 AM
Super User
Posts: 13,583

## Re: Merging - many to many relationship using data step

Note:   The MERGE statement does not produce a Cartesian product on a many-to-many match-merge. Instead it performs a one-to-one merge while there are observations in the BY group in at least one data set. When all observations in the BY group have been read from one data set and there are still more observations in another data set, SAS performs a one-to-many merge until all observations have been read for the BY group.

So in your case for the Code=AA. First record of set a matched first of B, then second of A with second of B. B ran out of AA codes so remaing AA in set A matched against the last AA in dataset B.

Many to many joins are usually better left to Proc Sql.

All Replies
Solution
‎09-07-2016 11:43 AM
Super User
Posts: 13,583

## Re: Merging - many to many relationship using data step

Note:   The MERGE statement does not produce a Cartesian product on a many-to-many match-merge. Instead it performs a one-to-one merge while there are observations in the BY group in at least one data set. When all observations in the BY group have been read from one data set and there are still more observations in another data set, SAS performs a one-to-many merge until all observations have been read for the BY group.

So in your case for the Code=AA. First record of set a matched first of B, then second of A with second of B. B ran out of AA codes so remaing AA in set A matched against the last AA in dataset B.

Many to many joins are usually better left to Proc Sql.

Regular Contributor
Posts: 181

## Re: Merging - many to many relationship using data step

HI Ballardw,

Thanks a  lot for the details.

Also i assume many to many merge differ from proc sql left join? is that correct.

Thanks again

Regards,

Sheeba Swaminathan

Super User
Posts: 5,890

## Re: Merging - many to many relationship using data step

Yes.
Even if you can, M-M merge in the data step is almost never a valid use case.
Data never sleeps
Regular Contributor
Posts: 181

## Re: Merging - many to many relationship using data step

Hi LinusH,

Thank you.

Regards,

Sheeba Swaminathan

☑ This topic is solved.