SAS Base Programming cheat sheet
SAS Base Programming · Keep the key ideas within reach.
Access and create data structures
A LIBNAME statement assigns a libref to a physical location, for example LIBNAME mylib '/path'; librefs are 1-8 characters, begin with a letter or underscore, and contain only letters, digits, and underscores.
The DATA step compiles first, building the program data vector (PDV), then executes; the PDV holds all variables for the current row and is reset each iteration except for retained variables.
SET reads observations from existing SAS data sets and stacks them vertically; MERGE combines them horizontally by matching values of variables listed in a BY statement, which requires sorted input.
INFILE options: DLM= sets the delimiter, DSD treats consecutive delimiters as missing and handles quoted values, FIRSTOBS= starts reading at a record, and OBS= limits the number of records read.
MISSOVER assigns missing values to remaining variables when a record ends early; TRUNCOVER allows the last variable to be truncated instead of set to missing.
PROC IMPORT DATAFILE='file' DBMS=XLSX|CSV|DLM OUT=data REPLACE; reads external files; PROC CONTENTS DATA=data; describes data set metadata.
Character variable length in a DATA step is set by the first assignment or a LENGTH statement; the default length is 8 bytes if the variable was not previously defined.
ARRAY statement syntax: ARRAY q[3] var1-var3; DIM(q) returns the number of elements; array references use q[i] or q{i}.
Manage data
PROC SORT DATA=in OUT=out; BY key; NODUPKEY removes rows with duplicate BY values; NODUP removes duplicate entire rows.
WHERE filters observations before variables are loaded into the PDV; IF filters during execution after the PDV is populated and can use computed variables.
RETAIN var ; prevents a variable from being reset to missing at the start of each DATA step iteration and can assign an initial value.
The SUM statement var+expr; retains var across iterations, ignores missing values in the expression, and treats them as zero.
LAG(n) returns the value from n iterations earlier; DIF(n) returns the current value minus the lagged value; both use an internal queue.
BY-group processing with FIRST.var and LAST.var flags requires the data to be sorted by the BY variables.
PROC TRANSPOSE converts rows to columns or columns to rows using ID, VAR, BY, and PREFIX= options.
PROC APPEND appends one data set to another without reading and rewriting the base; PROC DATASETS manages library members and metadata.
Functions: SUBSTR extracts or replaces text, SCAN returns the nth word, CATX joins with a delimiter, INPUT converts character to numeric, and PUT converts numeric to character.
Error handling
ERROR is an automatic DATA step variable set to 1 when a data error occurs; it is reset to 0 at the start of each iteration.
Compile-time errors are syntax errors detected before execution; execution-time errors include invalid numeric conversion and invalid array subscripts.
OPTIONS ERRORS=n limits the number of observations for which error messages are printed; the default is 20.
OPTIONS ERRORABEND makes SAS terminate when an error occurs; NOERRORABEND is the default and allows processing to continue.
PUT ALL; in a DATA step writes all current PDV variables and their values to the SAS log, which is useful for debugging.
The ABORT statement stops DATA step execution; ABORT CANCEL cancels submitted statements, and ABORT RETURN returns to the calling environment.
&SYSERR contains the return code from the most recent SAS procedure or DATA step; a nonzero value indicates an error.
A 'Missing values were generated' note indicates numeric operations produced missing values, often from character-to-numeric conversion.
LOSTCARD occurs when an INPUT statement reads past the end of a record; check line holds, record format, and the number of values per record.
Generate reports and output
PROC PRINT DATA=... NOOBS; prints a data set without observation numbers; LABEL uses variable labels, SPLIT= defines label split characters, and BY requires sorted data.
PROC FREQ TABLES var / MISSING; includes missing values as a category; NOCUM suppresses cumulative statistics and NOPERCENT suppresses percentages.
PROC MEANS DATA=... CLASS var; produces statistics by class levels; NWAY outputs only the highest-level class combination.
PROC REPORT COLUMN ...; DEFINE var / DISPLAY|GROUP|ORDER|ANALYSIS; COMPUTE blocks create computed columns or report items.
ODS HTML FILE='x.html'; ... ODS HTML CLOSE; routes procedure output to HTML; ODS PDF and ODS RTF follow the same open and close pattern.
TITLE 'text'; and FOOTNOTE 'text'; add report titles and footnotes; OPTIONS NODATE NONUMBER removes the date and page numbers.
PROC TABULATE TABLE rowvar*colvar; uses dimensional table syntax; CLASS variables define categories and VAR variables provide analysis values.
DATA NULL with PUT writes custom report lines to the log or to a file specified by FILE; @ and @@ control line hold behavior.
PROC FORMAT VALUE name range='label'; defines a user format applied with FORMAT var name.;