Relational Joins ⏱️ 6 min read

Merging Datasets: DATA Step MERGE vs. PROC SQL Joins

Compare match-merging using IN= variables with relational SQL inner, left, and full joins.

Made2Stick
Made2Stick Editorial Team Reviewed by Senior SAS & Data Systems Faculty

📌 Direct Answer / Executive Definition

Merging in SAS combines observations from two or more datasets based on common key variables. In the DATA step, match-merging requires pre-sorted data and uses the MERGE and BY statements alongside IN= flags. In PROC SQL, tables are joined dynamically without requiring pre-sorting using standard SQL JOIN syntax.

🎬 Classroom Video Blueprint: Merging Datasets: DATA Step MERGE vs. PROC SQL Joins
Masterclass Frame
Merging Datasets: DATA Step MERGE vs. PROC SQL Joins Video Slide ▶ Watch in Video Player →

Why Match-Merging vs. Relational Joins Matter for Large Data Lakes

Choosing between DATA step merge and PROC SQL joins impacts query performance, memory consumption, and data integrity in large enterprise data lakes.

Standard Syntax, Derivation & Framework Pattern

/* Method 1: DATA Step Match-Merge */
PROC SORT DATA=work.demog; BY Subject_ID; RUN;
PROC SORT DATA=work.labs;  BY Subject_ID; RUN;

DATA work.merged_patient;
    MERGE work.demog(IN=a) work.labs(IN=b);
    BY Subject_ID;
    IF a AND b; /* Inner Join Equivalent */
RUN;

/* Method 2: PROC SQL Join (No pre-sort required) */
PROC SQL;
    CREATE TABLE work.sql_merged AS
    SELECT d.Subject_ID, d.Age, d.Sex, l.Lab_Result
    FROM work.demog AS d
    INNER JOIN work.labs AS l
    ON d.Subject_ID = l.Subject_ID;
QUIT;

Core Rules & Certification Takeaways

  • DATA step MERGE requires all input datasets to be sorted by the BY variables beforehand.
  • IN= options create temporary flags (0 or 1) indicating whether an input dataset contributed to the current PDV row.
  • PROC SQL does not require pre-sorted inputs and handles Cartesian products and many-to-many joins cleanly.
  • For 1-to-many merges, DATA step MERGE retains non-overwritten values from earlier tables, which can cause unexpected data propagation.

🔗 Related Architectural Concepts & Next Steps

Deepen your mastery with connected topics across our curriculum and knowledge hubs.

SQL Querying SAS
PROC SQL Aggregations & Subqueries Read Guide →
Data Hygiene SAS
PROC SORT Pre-requisite Rules Read Guide →
Safety Analysis Clinical
ADAE & ADSL Safety Merging Read Guide →

Frequently Asked Questions (FAQ)

Does PROC SQL require tables to be sorted before joining?

No. Unlike DATA step MERGE which mandates pre-sorted datasets by the BY variables, PROC SQL dynamically joins tables on any matching predicate.

How does the IN= dataset option work during a MERGE?

The IN= option creates a temporary boolean variable (1 if the dataset contributed data to the current observation, 0 otherwise), enabling precise filtering for left, right, and inner joins.

Want complete video breakdowns & exercises?

Explore full step-by-step masterclass training in The Simplest Guide to SAS Programming.

Enroll in SAS Masterclass →
← Back to SAS Programming Knowledge Hub Overview