;
.proc
statement. proc
steps may have additional required or optional statements. run;
statement tells SAS to execute a block of code./* comment */
.infile
statement in a data
stepproc import
.proc print
and proc contents
for inspecting data. example0.sas
we: data
step with an infile
statement, input
statement,proc print
and proc contents
.example0.sas
we alsoproc import
to import a comma delimited copy of the "2020 RECS data".proc import
. %let [macro] = [text];
&
.
-- use it always. "¯o."
allow for macro expansion. example1.sas
we read a fixed width file using a data step with
infile
and input
statements.example1.sas
, we create a handle for a SAS library using a libname
statement. libname [handle] [loc]
.=
in this statement. .sas7bdat
WORK
.WORK
library is usually created in a temporary directory that gets
erased when you log off / close SAS.example2.sas
we:recs
out_lib
library using a
set
statementproc contents
. example3.sas
we: proc import
or a data
step with
infile
and input
statements. .sas7bdat
libname
statement to create a handle for a SAS library - a directory
in which to work with .sas7bdat
filesWORK
created in a temporary folderproc import
and proc contents
are useful for inspectionwhere
statement to filter to rows meeting a condition._n_
.if [cond] then delete;
construction. keep
only a subset of variabledrop
one or more variablesrename
a variable=
example4.sas
we:DOEID
,DOEID
in each dataset. tidyr::pivot_longer()
andtidyr::pivot_wider()
data.table::melt()
and data.table::dcast()
reshape long
and reshape wide
proc transpose;
by
statement for variables to be retained from the original wide
format data, e.g., case identifiersvar
statement:_NAME_
_LABELS_
var
example5.sas
we pivot replicate weights used for variance calculations
to a longer format.by
statement to specify variables that identify rows in the new
longer format data. var
statement.id
statement.example5.sas
we pivot the replicate weights back to wide as a
demonstration. proc summary;
class
statement to specify groups.proc summary;
proc sort
with the same by
statement to sort. output
statement to specify the name of the new summary dataset
and the summary statistics.output out=[new data] [f1(x1)] = y1;
example6.sas
we use aggregation by group to compute an effective
sample size for each census region in the RECS 2020 data:merge
and by
statements in a data step.merge [left data] [right data];
by
statement specifies which variables are used to match rows. keep
options after each data set to control which
columns to merge.by
. example7.sas
we use aggregation by group to compute replicate
estimates of tje effective sample size for each census region.We then use these replicates to estimate the variance:
We use several merges in the process.
proc sql;
proc sql;
and quit;
statementsproc sql
example8.sas
, we repeat example 7 using proc sql;
%let
%macro
%macro
statement %macro [macro name]([par1], [par2]);
%mend [macro name];
%name(1, 2)
; example9.sas
we define a macro to export a SAS dataset to csv using
proc export
.%csvexport2()
is a modification that allows the output filename
to be changed.%
prefix.%if [cond] %then [what]
to
set the output filename to be the name of the dataset by default. proc tabulate
proc freq
proc means
title
statements to label results appearing in the results (listing)
window (file)proc means
¶var
statement.class
statement or a by
statement for means by group.example10.sas
class
and by
statement:by
statementby
applies the procedure group by groupproc freq
¶proc means
but for frequencies instead of meanstables
statement.proc tabulate
¶var
statementclass
statementtable
statement:[variable]*[statistic]
or [variable]*([stat]='label', ...)
data
and proc
steps.libname
statement.%let
work by text substitution, use ¯o.
. proc sql
for complex data manipulations. proc means
, proc freq
.