3.1 Reading SAS Data Sets with the SET Statement
Key Takeaways
- The SET statement reads observations from one or more existing SAS data sets sequentially into the Program Data Vector (PDV).
- During compilation, SAS establishes variable attributes (name, type, length, format) based on the first occurrence in the input data set(s).
- Variables read from an input data set via SET are automatically retained in the PDV across iterations rather than re-initialized to missing.
- The automatic variable _N_ tracks DATA step iterations while _ERROR_ signals data errors; neither variable is written to output data sets.
- The END= option creates a temporary numeric indicator variable (0 or 1) that flags when SAS reaches the last observation of the input data set.
3.1 Reading SAS Data Sets with the SET Statement
Quick Answer: The
SETstatement reads observations sequentially from one or more existing SAS data sets into the Program Data Vector (PDV). Unlike raw data read withINPUT, variables read viaSETautomatically retain their values across DATA step iterations until replaced by subsequent observations.
Understanding how SAS reads existing SAS data sets using the SET statement is a cornerstone of SAS Base programming and a primary focus of the SAS Certified Specialist exam (A00-231). The SET statement operates inside a DATA step to read data sequentially from an input data set, copy variable descriptor information, and populate the Program Data Vector (PDV).
1. Compilation Phase vs. Execution Phase Mechanics
SAS processes a DATA step containing a SET statement in two distinct phases: Compilation and Execution.
data work.employee_copy;
set sashelp.class;
run;
Compilation Phase
During compilation, SAS scans the code to establish data structures before reading any observations:
- Descriptor Creation: SAS opens the header descriptor of the input dataset (
sashelp.class). It copies the variable attributes—including variable names, types (character or numeric), lengths, formats, informats, and labels—to the descriptor portion of the new output dataset (work.employee_copy). - PDV Construction: SAS creates the Program Data Vector (PDV) layout based on the discovered variable attributes.
- Automatic Variables: SAS adds two hidden automatic variables to the PDV:
_N_: A numeric iteration counter initialized to 1._ERROR_: A numeric binary flag initialized to 0, which signals data or processing errors during execution.
Execution Phase
During execution, SAS iterates through the input dataset line by line:
- PDV Initialization: At the beginning of the FIRST iteration (
_N_=1), all variables in the PDV are set to missing (.for numeric, blank " " for character). - SET Execution: The
SETstatement reads the first observation fromsashelp.classdirectly into the matching PDV variable slots. - Program Logic: SAS executes any subsequent programming statements in the DATA step.
- Implicit OUTPUT: Upon reaching the
run;statement (or implicitRETURN), SAS executes an implicitOUTPUTstatement, copying the contents of the PDV as a row in the output dataset. - Iteration & Retention: SAS returns to the top of the DATA step (
RETURN), increments_N_by 1, and executesSETto read the next observation.
| Phase | Activity | PDV Variable Attributes | Variable Values in PDV |
|---|---|---|---|
| Compilation | Reads dataset header | Set from input dataset attributes | Unpopulated (logical structure only) |
| Execution Step 1 | Top of DATA step (_N_=1) | Frozen from compilation | Initialized to missing (., " ") |
| Execution Step 2 | SET statement executes | Unchanged | Overwritten with values from input row |
| Execution Step 3 | Bottom of step (RETURN) | Unchanged | Written to output dataset via implicit OUTPUT |
| Execution Step 4 | Start of next iteration (_N_=2) | Unchanged | Retained from previous row until SET reads next row |
2. Variable Retention Behavior in the PDV
A critical exam rule distinguishes variables created by assignment statements from variables read from SAS data sets via SET:
Exam Rule: Variables read from an existing SAS data set using the
SETstatement are automatically retained across iterations. SAS does NOT re-initialize input dataset variables to missing at the beginning of subsequent DATA step iterations.
In contrast, newly calculated variables created via assignment statements (e.g., temp_calc = age * 10;) are re-initialized to missing at the start of every iteration unless explicitly preserved using a RETAIN statement or a sum statement (+).
data work.calc_example;
set sashelp.class;
/* Variable created by assignment: reset to missing each iteration */
temp_calc = age * 10;
/* Variable created by sum statement: automatically retained */
cumulative_age + age;
run;
3. Dataset Options Used with the SET Statement
SAS provides specialized dataset options on the SET statement to control data stream reading and last-observation logic.
The END= Option
The END= option defines a temporary, numeric indicator variable that flags when SAS reaches the final observation of the input dataset.
data work.summary_stat;
set sashelp.class end=last_rec;
total_weight + weight;
/* Output only the single summary row when last_rec equals 1 */
if last_rec = 1 then output;
run;
last_recholds a value of0for all observations except the final observation, where it equals1.END=variables exist only in the PDV during DATA step execution; they are never written to the output dataset.
Direct Access with POINT= and NOBS=
The POINT= option specifies a numeric variable containing the observation number to read directly (random access). The NOBS= option creates a temporary variable containing the total count of observations in the dataset.
data work.sample_middle;
/* NOBS assigns total record count to total_obs at compilation */
set sashelp.class nobs=total_obs;
/* Fetch observation #5 directly */
pick_obs = 5;
set sashelp.class point=pick_obs;
output;
stop; /* MANDATORY: Prevents infinite loop */
run;
Warning / Exam Trap: When using
POINT=, SAS does not encounter an End-Of-File (EOF) marker. Without an explicitSTOPstatement, the DATA step will enter an infinite loop, repeatedly executing the step without terminating!
4. Common Exam Pitfalls & Traps
- Length Mismatches Across Data Steps: If a variable is assigned a length in a preceding statement before
SET, SAS retains that initial length. If the input dataset has a longer length, character string truncation occurs without warning. - Conditional SET Statements: Placing
SETinside anIF-THENblock can cause unexpected execution behavior, as observations are only read when the condition evaluates to true. - Misinterpreting
_N_:_N_records the number of times the DATA step has executed, not necessarily the observation number of the output dataset (especially if conditionalOUTPUTor subsettingWHEREstatements are present).
Which statement correctly describes the behavior of the temporary variable created by the END= option on a SET statement?
When using the POINT= option on a SET statement to read observations directly, what is required to prevent an infinite loop?
How does SAS handle variable values in the Program Data Vector (PDV) for variables read from an existing SAS data set using a SET statement?
Consider the following code: data combined; set ds1 ds2; run;. In ds1, character variable City has length $10. In ds2, City has length $20. What is the length of City in combined?