DATA Steps and PROC Steps
Prerequisites: Your first program
Every SAS program is a sequence of steps. A step starts with DATA
or PROC and ends at RUN; (some procedures, like PROC SQL, end with
QUIT;). SAS runs the steps one after another.
How a DATA step runs
A DATA step runs in two phases:
- Compilation — SAS checks the syntax and builds the program data
vector (PDV): a one-row workspace holding every variable the step
uses, plus two automatic variables,
_N_(the iteration number) and_ERROR_. - Execution — SAS runs the statements once for each row of
input. At the end of each pass (the
RUNstatement), it writes the PDV to the output data set, resets new variables to missing, and goes back to the top for the next row.
That built-in loop is the key to reading SAS code: you write what should happen to one row, and SAS repeats it for every row.
data demo;
input x;
y = x * 2;
put _n_= x= y=;
datalines;
3
5
8
;
run;
Log (abridged):
_N_=1 x=3 y=6
_N_=2 x=5 y=10
_N_=3 x=8 y=16
NOTE: The data set WORK.DEMO has 3 observations and 2 variables.
The step ran three times, once per data line. _N_ isn't saved in the
data set — automatic variables are dropped — which is why the note says
2 variables.
How a PROC step runs
A procedure reads a whole data set and does its job — you don't write a
loop. Statements inside a PROC step, like var or by, are options for
that procedure:
proc print data=demo;
var y;
run;
Obs y
1 6
2 10
3 16
Errors and warnings
SAS tries hard to keep going. Misspell a keyword it recognizes and it fixes it with a warning:
data demo2;
imput x;
datalines;
1
;
run;
WARNING 1-322: Assuming the symbol INPUT was misspelled as imput.
A misspelled procedure name stops that step:
proc prnt data=demo;
run;
ERROR: Procedure PRNT not found.
Common mistakes
- A missing semicolon — SAS joins two statements into one and reports a confusing error on the next line. Check the line above the error.
- A missing
run;at the end — in SAS Studio the last step still runs, but in other environments it may wait for more code. Always end each step withrun;. - Unbalanced quotes — an unclosed
'makes SAS treat the rest of the program as text. Fix the quote and re-run.