A common belief is that the MERGE statement in a DATA step is the exact equivalent of a SQL join (LEFT JOIN or FULL JOIN). While this is true for simple relationships (1-to-1 or 1-to-N), it is completely false for "Many-to-Many" (N-to-N) relationships.
The Observation: Divergent Results
Let's take a simple example where the join key (ID) appears multiple times in both tables:
Table 1: ID 23456 appears 2 times.
Table 2: ID 23456 appears 2 times.
If we try to combine this data, we mathematically expect to get $2 \times 2 = 4$ rows (the Cartesian product for this ID).
Note :
The PROC SQL Approach (Cartesian Product)
The SQL works on set-based logic (relational algebra). It combines each row from table A with every corresponding row from table B.
PROC SQL;
SELECT * FROM dataset1 t1
LEFT JOIN dataset2 t2 ON t1.ID = t2.ID;
QUIT;
1
PROC SQL;
2
SELECT * FROM dataset1 t1
3
LEFT JOIN dataset2 t2 ON t1.ID = t2.ID;
4
QUIT;
Result: 4 rows for ID 23456. All possible combinations are created. This is the standard behavior of a relational database.
Note :
The DATA MERGE Approach (Sequential Juxtaposition)
The DATA step operates row by row sequentially. It places the read pointers "side by side".
Use PROC SQL if you need a Cartesian product (N-to-N relationship), meaning you want to cross every possible occurrence. This is often the expected result for cross-sectional analyses.
Use DATA MERGE for 1-to-1 or 1-to-N relationships (like a "Look-up" / Data Enrichment). It is much more efficient in terms of computation time on large volumes, as long as the keys are unique in at least one of the two tables.
Technical Note: It is technically possible to simulate a Cartesian product with a DATA step (using two SET statements and explicit loops), but the code becomes unnecessarily complex. In this specific case, the clarity of SQL is unbeatable.
Important Disclaimer
The codes and examples provided on WeAreCAS.eu are for educational purposes. It is imperative not to blindly copy-paste them into your production environments. The best approach is to understand the logic before applying it. We strongly recommend testing these scripts in a test environment (Sandbox/Dev). WeAreCAS accepts no responsibility for any impact or data loss on your systems.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.