SAS DATA Step Overview

A SAS DATA step is a group of SAS statements that reads, creates, or modifies data. It can read raw records, process observations from an existing SAS data set, calculate variables, filter rows, combine data sets, and write the results to one or more output data sets.

A DATA step normally begins with a DATA statement and ends when SAS encounters a RUN statement, another DATA statement, a PROC statement, or the end of the submitted program. The DATA statement names the data set that SAS will create.

What a SAS DATA Step Can Do

  • Read raw data entered in the program or stored in an external file.
  • Read observations from an existing SAS data set.
  • Create variables and calculate their values.
  • Select observations with IF or WHERE conditions.
  • Validate values and identify data errors in the SAS log.
  • Merge, concatenate, sort-dependent combine, or reshape data.
  • Apply variable attributes such as labels, formats, informats, and lengths.
  • Create one or more SAS data sets for later analysis or reporting.

SAS programs commonly contain DATA steps and procedure steps. A DATA step prepares or transforms data, while a PROC step analyzes, summarizes, or reports it. See the broader SAS tutorial and SAS program basics for the relationship between these steps.

Basic SAS DATA Step Syntax

</>
Copy
DATA output-data-set;
    input-or-creation-statements;
    processing-statements;
RUN;

The statements inside the step depend on the source of the data. Use an INPUT statement to describe raw data and a SET statement to read an existing SAS data set. SAS statements usually end with semicolons.

SAS DATA Step Example Using Raw Input

The following DATA step creates WORK.STUDENTS from data written directly in the program. The INPUT statement defines the variables, and DATALINES marks the beginning of the raw records.

</>
Copy
data students;
    input StudentID Name $ Score;
    datalines;
101 Anil 78
102 Meera 91
103 Ravi 84
;
run;

The dollar sign after Name tells SAS that the variable contains character values. Variables without a dollar sign in this simple input form are numeric.

SAS DATA Step Example Using the SET Statement

Use SET when the source is an existing SAS data set. This example reads each observation from STUDENTS, calculates a new variable, and writes the result to STUDENT_RESULTS.

</>
Copy
data student_results;
    set students;
    Percentage = Score;

    if Score >= 50 then Result = 'Pass';
    else Result = 'Fail';
run;

Unless another library is specified, these one-level data set names refer to the temporary WORK library. Data sets in WORK are removed when the SAS session ends. Use a two-level name such as MYLIB.STUDENTS to identify a data set in an assigned permanent library.

Filtering a SAS DATA Step with Multiple Conditions

A subsetting IF statement evaluates observations during DATA step execution. A WHERE statement requests qualifying observations from the input source. Conditions can be combined with operators such as AND, OR, and NOT.

</>
Copy
data high_scores;
    set students;
    where Score >= 80 and StudentID < 200;
run;

Use parentheses when a condition combines AND and OR. This makes the intended evaluation order explicit.

</>
Copy
data selected_students;
    set students;
    if (Score >= 80 and StudentID < 200) or Name = 'Meera';
run;

SAS DATA Step Compile and Execution Phases

When SAS receives a DATA step, it handles the step in two main phases: compilation and execution. Understanding these phases helps explain variable attributes, automatic variables, the Program Data Vector, and messages in the SAS log.

SAS DATA Step Compile Phase

During compilation, SAS checks the syntax, identifies variables, determines their attributes, creates the Program Data Vector, and prepares the descriptor portion of the output data set. Statements such as LENGTH, FORMAT, INFORMAT, LABEL, and ATTRIB help define variable attributes.

A syntax error can prevent the DATA step from executing. The SAS log should therefore be checked after every submitted step, even when an output table appears to have been created.

SAS DATA Step Execution Phase

During execution, SAS processes the DATA step statements in order. In a typical step that uses SET, SAS reads one observation into the Program Data Vector, performs calculations and conditional processing, writes an output observation, and begins the next iteration. This implicit loop continues until no more input observations are available or a statement explicitly stops processing.

Input Buffer for Raw Data Records

When a DATA step reads raw data with an INPUT statement, SAS places the current raw record in an input buffer. The INPUT statement then reads values from that record and assigns them to variables in the Program Data Vector.

The input buffer is associated with raw-data input. A DATA step that reads an existing SAS data set with SET loads variable values into the Program Data Vector without using the raw-data input buffer in the same way.

Program Data Vector in a SAS DATA Step

The Program Data Vector, usually abbreviated as PDV, is the logical area of memory in which SAS builds one observation at a time. It contains variables read from the input source, variables created by DATA step statements, and automatic variables used internally during processing.

At the beginning of an iteration, values of many variables created in the DATA step are reset to missing. Variables read with statements such as SET have different retention behavior because their values are replaced when the next observation is read. Use a RETAIN statement or a sum statement when a newly created value must carry across iterations.

Automatic Variables _N_ and _ERROR_

  • _N_: Counts DATA step iterations. Its value begins at 1 and increases by one each time SAS starts another iteration. It is not necessarily the same as the number of observations written, because filtering or explicit OUTPUT statements can change the output count.
  • _ERROR_: Has an initial value of 0 and is set to 1 when SAS detects certain data-related errors during the current iteration, such as invalid input data. It is reset before the next iteration.

Both automatic variables are available during DATA step processing but are not written to the output data set. Their names must include the leading and trailing underscores.

[alert-note] Note :- Syntax errors are program errors and logical errors are data errors. [/alert-note]

A value of _ERROR_=0 does not prove that the program logic is correct. A DATA step can run without a syntax or data error and still produce an unintended result. Review the output and the notes, warnings, and errors in the SAS log.

Controlling SAS DATA Step Output

Most DATA steps have implicit output behavior: after the statements for an iteration finish, SAS writes the current PDV values as an observation. An explicit OUTPUT statement provides more control and can direct observations to different data sets.

</>
Copy
data passing failing;
    set students;

    if Score >= 50 then output passing;
    else output failing;
run;

Once an explicit OUTPUT statement is used, SAS does not also perform the usual implicit output for that iteration. Statement order matters: calculations required in the saved observation should occur before the corresponding OUTPUT statement.

SAS Data Set Descriptor Information

A SAS data set contains a data portion and a descriptor portion. The data portion stores observation values. The descriptor portion records information such as the data set name, number of observations and variables, variable names, data types, lengths, labels, formats, and informats.

Use the CONTENTS procedure to inspect descriptor information. This is useful when checking whether variable types, lengths, formats, and creation order match the intended DATA step design.

PROC CONTENTS DATA=DATASET_NAME;
RUN;

SAS DATA Step Troubleshooting Checklist

  • Confirm that the DATA, SET, INPUT, and RUN statements end with semicolons.
  • Check that character variables are defined with an appropriate type and length before values are assigned.
  • Verify that the input library and data set names are correct.
  • Use parentheses to confirm the intended logic in conditions containing both AND and OR.
  • Check whether an explicit OUTPUT statement changes the number or timing of observations written.
  • Do not use _N_ as an output-row count when observations can be filtered or written more than once.
  • Review every SAS log NOTE, WARNING, and ERROR, including messages about invalid data, uninitialized variables, and automatic character-to-numeric conversion.
  • Run PROC CONTENTS to confirm variable types, lengths, labels, formats, and informats.

Frequently Asked Questions About the SAS DATA Step

What is the difference between a DATA step and a PROC step in SAS?

A DATA step reads, creates, or transforms data. A PROC step invokes a SAS procedure to analyze, summarize, display, or report data. A program can contain multiple DATA and PROC steps in any sequence required by the task.

What is the difference between SET and INPUT in a SAS DATA step?

SET reads observations from a SAS data set. INPUT describes how SAS should read fields from raw data, including data supplied through DATALINES or an external file referenced by an INFILE statement.

Does every SAS DATA step require a RUN statement?

A following DATA or PROC statement can mark the end of the current DATA step, but writing an explicit RUN; statement is clearer and helps separate program steps. Some specialized SAS processing boundaries may use other statements, so the appropriate terminator depends on the code being executed.

Are _N_ and _ERROR_ saved in the SAS output data set?

No. _N_ and _ERROR_ are automatic variables available in the PDV during execution, but SAS does not include them in the output data set. Assign their values to regular variables if they need to be saved.

How can I inspect variable attributes created by a SAS DATA step?

Run PROC CONTENTS for the output data set. It reports descriptor information such as variable names, types, lengths, labels, formats, and informats. The SAS log should also be reviewed for compilation and execution messages.