Skip to content
AgileLogics

SAS Cheat Sheet

Learn SAS from the basics →

Program Structure

data out; set in; run;
DATA step: read, change, and write a data set
proc print data=out; run;
PROC step: run a ready-made procedure
* comment; /* comment */
Statement comment / block comment
libname mylib "/home/u123/data";
Point a library name (libref) at a folder
mylib.sales / work.sales / sales
Two-level name; one-level names mean WORK
options nodate nonumber;
Set system options

Reading Data

input name $ age score;
List input; $ marks a character variable
datalines; ... ;
Data typed inside the program
infile "f.csv" dsd firstobs=2;
Read a CSV file, skipping the header row
proc import datafile="f.csv" out=f dbms=csv replace; run;
Import a CSV or Excel file
length name $ 20;
Set a variable's type and length
d = '15MAR2024'd;
Date constant (days since 1 Jan 1960)

DATA Step Statements

if x > 10 then y = 1; else y = 0;
Conditional assignment
if sex = "F";
Subsetting IF: keep only matching rows
where age > 12;
Filter rows as they are read
keep a b; drop c; rename old=new;
Choose and rename variables
total + amount;
Sum statement: retained, missing treated as 0
retain count 0;
Keep a value across iterations
output; / put x=;
Write a row now / write to the log

Loops, Arrays, BY Groups

do i = 1 to 10; ... end;
Iterative DO loop
do while (x < 100); ... end;
Condition loop (also DO UNTIL)
array q{5} q1-q5;
Array over existing variables
set a b;
Stack (concatenate) data sets
merge a(in=ina) b; by id; if ina;
Match-merge, keep rows from a
by region; if first.region then n = 0;
BY-group flags FIRST. and LAST.

Functions

sum(a, b) / mean(a, b, c)
Statistics that ignore missing values
round(x, 0.01) / int(x) / mod(a, b)
Numeric functions
upcase(s) / propcase(s) / strip(s)
Case and blanks
substr(s, 1, 3) / scan(s, 2, ",")
Extract text / the nth word
catx(" ", first, last)
Join with a separator, trimming blanks
intck("month", d1, d2) / intnx("month", d, 1)
Count / move by date intervals
input(s, 8.) / put(x, 8.2)
Character to number / number to character

Procedures

proc sort data=a out=b; by descending x; run;
Sort
proc means data=a maxdec=2; class g; var x; run;
Summary statistics per group
proc freq data=a; tables g g*h / chisq; run;
Frequency and cross tables
proc sql; select g, avg(x) from a group by g; quit;
SQL queries
proc contents data=a; run;
List variables, types, and lengths
proc format; value $sx "F"="Female"; run;
Custom formats

Macros

%let year = 2024;
Create a macro variable
title "Sales for &year";
Resolve it (double quotes only)
%put &=year;
Write name and value to the log
call symputx('n', total);
Macro variable from a DATA step
%macro rpt(ds); ... %mend; %rpt(sales)
Define and call a macro

Output and Statistics

ods pdf file="r.pdf"; ... ods pdf close;
Send results to a PDF (also RTF, EXCEL, HTML)
ods output ParameterEstimates=pe;
Save a results table as a data set
proc sgplot data=a; scatter x=h y=w; run;
Scatter plot
proc reg data=a; model y = x; run;
Linear regression
proc logistic data=a; model y(event="1") = x; run;
Logistic regression