7.3 Debugging Techniques with _ERROR_, _N_, & PUT Statements
Key Takeaways
- The automatic variables _ERROR_ (binary error flag) and _N_ (iteration count) are maintained in the Program Data Vector during execution but are not written to the output dataset.
- The PUT statement writes custom messages, formatted variable values, or complete PDV contents directly to the SAS log during execution.
- Specifying PUT _ALL_; dumps every variable in the Program Data Vector—including automatic variables _N_ and _ERROR_—to the SAS log for the current execution iteration.
- Named output syntax PUT variable=; writes the variable name, an equals sign, and its current value to the log, providing rapid debugging feedback.
- Programmers can perform conditional debugging by combining IF-THEN logic with PUT statements (e.g., IF _ERROR_ = 1 THEN PUT _ALL_;) to inspect only problematic records.
7.3 Debugging Techniques with ERROR, N, & PUT Statements
When developing SAS programs, understanding what happens inside the Program Data Vector (PDV) during the execution phase is vital for troubleshooting complex transformations. SAS provides two built-in automatic variables—_ERROR_ and _N_—alongside the versatile PUT statement to give programmers direct visibility into PDV execution states.
1. Automatic Variables _ERROR_ and _N_
During the compilation phase, SAS automatically adds two numeric variables to the PDV: _ERROR_ and _N_.
+-------------------------------------------------------------------------+
| PROGRAM DATA VECTOR (PDV) |
| User Variables: [ ID ] [ Department ] [ Sales ] |
| Automatic Vars: [ _N_ ] [ _ERROR_ ] |
+-------------------------------------------------------------------------+
Characteristics of _ERROR_ and _N_
_N_(Iteration Counter): Keeps track of the number of times the DATA step has iterated. It starts at1for the first record and increments by 1 at the top of each execution loop._ERROR_(Binary Error Flag): Acts as an execution error indicator. Its default value is0(no error). If a runtime error occurs (such as invalid raw data input, data conversion failure, or division by zero), SAS automatically resets_ERROR_ = 1for that observation.- Non-Output Behavior: Neither
_ERROR_nor_N_is written to the output dataset when anOUTPUTstatement executes. They exist strictly in memory within the PDV for processing control and debugging.
/* Using _N_ and _ERROR_ in programming logic */
data work.audit;
set work.transactions;
/* Flag observations processed during the first 10 iterations */
if _N_ <= 10 then sample_group = 1;
/* Perform custom actions if an execution error occurred */
if _ERROR_ = 1 then do;
put "Data error detected at observation number " _N_;
end;
run;
2. The PUT Statement vs. OUTPUT Statement
A common source of confusion for Base SAS exam candidates is the fundamental operational difference between the PUT statement and the OUTPUT statement.
| Statement | Destination Target | Operational Purpose |
|---|---|---|
OUTPUT; | SAS Dataset (Disk/Work Library) | Writes the current contents of the PDV as a row in the target SAS dataset. |
PUT; | SAS Log (or external text file) | Writes text literals, variable values, or PDV diagnostics directly to the SAS log. |
3. Variations of the PUT Statement for Debugging
The PUT statement offers several formatting modes to inspect PDV variable values during execution.
A. Named Output (PUT variable=;)
Adding an equals sign (=) immediately after a variable name instructs SAS to print the variable's name, an equals sign, and its current value to the log.
data work.test;
set work.employees;
if salary > 100000 then do;
put emp_id= dept= salary=;
end;
run;
SAS Log Output:
emp_id=10452 dept=Executive salary=125000
emp_id=10899 dept=Legal salary=140000
B. Complete PDV Dump (PUT _ALL_;)
Executing PUT _ALL_; dumps every variable currently residing in the PDV to the SAS log, including user-defined variables, calculated variables, drop/keep status flags, and automatic variables _N_ and _ERROR_.
data work.debug_demo;
set work.sales_raw;
bonus = sales * 0.05;
if _N_ = 1 then do;
put "=== DUMPING PDV FOR FIRST OBSERVATION ===";
put _ALL_;
end;
run;
SAS Log Output:
=== DUMPING PDV FOR FIRST OBSERVATION ===
sales_raw_id=101 Region=East sales=5000 bonus=250 _ERROR_=0 _N_=1
C. Text Literals and Formatted Output
Programmers can combine descriptive text literals (in quotes) with specific SAS formats in a PUT statement:
data _null_;
set work.quarterly_results;
put "Obs #" _N_ " Region: " region $10. " Total Sales: " sales dollar12.2;
run;
Note on
DATA _NULL_;: SpecifyingDATA _NULL_;creates a DATA step that compiles and executes without writing an output dataset to disk. This is widely used for producing custom log reports, writing raw text files viaFILEandPUTstatements, and rapid debugging.
4. Advanced Debugging Strategies
A. Conditional Log Reporting
To avoid cluttering the SAS log when processing datasets with millions of rows, place PUT statements inside conditional IF-THEN blocks:
data work.processed_claims;
set work.medical_claims;
claim_ratio = payout / claim_amount;
/* Trigger log alert ONLY when an anomaly or runtime error occurs */
if _ERROR_ = 1 or claim_ratio > 1.0 then do;
put "WARNING: Invalid claim record at _N_=" _N_;
put claim_id= claim_amount= payout= claim_ratio= _ERROR_=;
end;
run;
B. Resetting _ERROR_ to Suppress Log Buffer Dumps
When SAS sets _ERROR_ = 1 due to invalid data, it automatically writes a multi-line input buffer and PDV dump to the log. If a programmer writes custom error-handling logic and wishes to suppress the default SAS multi-line dump, they can reset _ERROR_ = 0; after handling the issue:
data work.clean_input;
set work.raw_data;
if _ERROR_ = 1 then do;
put "Custom Audit: Invalid row encountered at record " _N_;
_ERROR_ = 0; /* Prevents SAS from executing its automatic default log dump */
end;
run;
Which statement correctly describes the behavior of automatic variables ERROR and N in a SAS DATA step?
A SAS programmer executes the following statement inside a DATA step when Region = 'North' and Revenue = 45000:
PUT Region= Revenue=;
What text is written to the SAS log?
What is the primary effect of executing 'PUT ALL;' within a SAS DATA step?
How do the PUT statement and OUTPUT statement differ in their destination target?