BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sheeba
Lapis Lazuli | Level 10

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.

 

Thanks in advance

 

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

From the online help:

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.

View solution in original post

4 REPLIES 4
ballardw
Super User

From the online help:

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.

Sheeba
Lapis Lazuli | Level 10

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

LinusH
Tourmaline | Level 20
Yes.
Even if you can, M-M merge in the data step is almost never a valid use case.
Data never sleeps
Sheeba
Lapis Lazuli | Level 10

Hi LinusH,

 

Thank you.

 

Regards,

Sheeba Swaminathan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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