Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

SAS : Many to Many Merge

$
0
0
In SAS, many-to-many merges are handled very differently via Data Step and PROC SQL.

Let's take an example -

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

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.
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 3123
1 1234
2 7482
2 8912
3 1284
;
run;


data dat2;
input ID Info2;
cards ;
1 4444
1 5555
1 8989
2 9099
2 8888
3 8989
;
run;

data combined;
merge dat1 dat2 ;
by ID;
run;
Output : Merge

PROC SQL
PROC SQL creates a cartesian product incase of a many-to-many relationship. 
proc sql noprint;
create table combined2 as
select * from dat1 a
left join dat2 b
on a.ID = b.ID;
quit;
The output would be same as shown in the first image - "Cross Product". 

Viewing all articles
Browse latest Browse all 425

Trending Articles