3.2 Combining Data Sets: Concatenating & Interleaving
Key Takeaways
- Concatenating lists multiple data sets in a single SET statement (SET ds1 ds2;), reading all observations from ds1 sequentially followed by all observations from ds2.
- Interleaving combines datasets in sorted order using a single SET statement paired with a BY statement (SET ds1 ds2; BY variable;).
- Interleaving strictly requires all input data sets to be pre-sorted by the variable(s) listed in the BY statement.
- If variables exist in one input dataset but not another, missing values (. or blank) are populated for observations originating from datasets lacking those variables.
- If variable data types conflict across concatenated datasets (numeric vs character), SAS halts compilation with a syntax error.
3.2 Combining Data Sets: Concatenating & Interleaving
Quick Answer: Concatenating (
SET ds1 ds2;) appends datasets sequentially, reading all rows fromds1followed by all rows fromds2. Interleaving (SET ds1 ds2; BY var;) combines pre-sorted datasets while preserving ascending order of theBYvariable.
Combining data sets vertically is a fundamental task in SAS data management. SAS provides two distinct methods for vertical combination within a single SET statement: Concatenating and Interleaving. Understanding the mechanical differences in Program Data Vector (PDV) handling, attribute alignment, and sorting requirements is crucial for the SAS Base certification exam.
1. Concatenating Data Sets
Concatenation is the process of appending one dataset to the end of another. It occurs when two or more SAS data sets are listed after a single SET statement without an accompanying BY statement.
data work.q1_q2_combined;
set work.q1_sales work.q2_sales;
run;
Execution Order & Behavior
- SAS opens
work.q1_salesand reads all observations sequentially from observation 1 to the End-of-File (EOF). - SAS closes
work.q1_salesand immediately openswork.q2_sales, reading all observations sequentially until its EOF. - The total observation count of the output dataset equals the sum of the observations in
work.q1_salesandwork.q2_sales.
Variable Attributes & Missing Values During Concatenation
- Union of Variables: The output dataset contains all unique variables present in any of the input datasets.
- Missing Value Imputation: If a variable exists in
q1_salesbut not inq2_sales, observations originating fromq2_saleswill have a missing value (.for numeric, blank " " for character) for that variable. - Attribute Determination: Variable attributes (type, length, label, format) are established during the compilation phase based on the dataset in which the variable first appears.
/* Scenario: Length Conflict */
data work.ds1;
length City $10;
City = "Dallas";
run;
data work.ds2;
length City $20;
City = "San Francisco";
run;
data work.combined;
set work.ds1 work.ds2; /* City length is set to $10 based on ds1 */
run;
Exam Trap: In
work.combined, the length ofCityis determined bywork.ds1($10). When SAS readsSan Franciscofromwork.ds2, the string is truncated toSan Franci! To prevent truncation, define the maximum required length using aLENGTHstatement before theSETstatement.
2. Type Conflict Errors
If a variable is defined as numeric in one dataset and character in another, SAS cannot complete compilation.
/* Compilation Error Example */
data work.ds_num; ID = 101; run;
data work.ds_char; ID = "102"; run;
data work.fail;
set work.ds_num work.ds_char;
run;
SAS Log Error:
ERROR: Variable ID has been defined as both character and numeric.
Compilation stops immediately, and no output dataset is produced. You must explicitly convert variable types using INPUT() or PUT() functions prior to concatenating.
3. Interleaving Data Sets
Interleaving combines multiple SAS data sets into a single sorted dataset based on common key variables. Interleaving requires a SET statement listing all input datasets followed immediately by a BY statement.
data work.interleaved_sales;
set work.q1_sorted work.q2_sorted;
by Region StoreID;
run;
Mandatory Requirement for Interleaving
Rule: All input data sets listed in the
SETstatement MUST be pre-sorted by the variables listed in theBYstatement (or indexed accordingly). If any input dataset is not sorted, SAS halts execution with a runtime error.
How Interleaving Works in the PDV
- SAS sets up input buffers for each dataset listed in the
SETstatement and compares theBYvariable values of the current observation in each buffer. - SAS selects the observation with the lowest
BYvariable value (in ascending order), copies it into the PDV, and writes it to the output dataset. - The pointer for the dataset that supplied the observation advances to its next record.
- Tie-Breaking Rule: If matching observations across datasets have identical
BYvalues, SAS breaks the tie according to the order of datasets listed in theSETstatement (e.g.,q1_sortedfirst, thenq2_sorted).
4. Concatenation vs. Interleaving Comparison
| Feature | Concatenation (SET ds1 ds2;) | Interleaving (SET ds1 ds2; BY var;) |
|---|---|---|
| BY Statement | None | Required |
| Sorting Requirement | Datasets do NOT need to be sorted | Datasets MUST be sorted by BY variable(s) |
| Observation Order | Sequential (all of ds1, then all of ds2) | Sorted by BY variable values across datasets |
| PDV Reading Logic | Reads single stream to EOF, then switches | Compares buffer pointers across streams per row |
| Tie-Breaker Logic | Not applicable | Input dataset order in SET statement |
| Output Row Count | Sum of all input rows | Sum of all input rows |
5. Best Practices & Code Examples
Handling Length and Name Mismatches Correctly
data work.all_customers;
/* 1. Explicitly set attribute length to prevent truncation */
length CustomerName $35 AccountID 8;
/* 2. Concatenate datasets */
set work.retail_cust work.online_cust;
run;
When interleaving data sets with SET ds1 ds2; BY Region;, how does SAS determine observation order when two observations from different datasets have identical Region values?
What occurs during compilation if ds1 defines variable Code as numeric and ds2 defines Code as character in data combined; set ds1 ds2; run;?
Which condition MUST be satisfied before executing a DATA step that interleaves data sets using a BY statement?
Dataset A contains variables ID and Score. Dataset B contains variables ID and Grade. What happens when executing data C; set A B; run;?