Sas Cheat Sheet
Sas Cheat Sheet
2 Selecting vriables
• w.d standard numeric DATA dsname;
• $n character SET dsname;
SAS Cheat Sheet for Stat-342 • COMMAw.d numeric values with commas ...
• DATEw.d,YYMMDDw. - use the ISO standard with 4 digit KEEP var1 var2 ...;
Carl James Schwarz years! DROP var1 var2 ....;
run;
April 9, 2016 1.4 Format modifiers
3.3 Merging datasets
Used typically with list input to modify the format attached to a
variable. Proc SORT data=DS1; by bvar1 bvar2 ...; run;
1 Reading data files into SAS dataset • (colon) : typically used for numeric variables for Proc SORT data=DS2; by bvar1 bvar2 ...; run;
dates/times/commas etc. DATA both;
DATA dsname1(dsoptions) • (ampersand) & used for character values with embedded MERGE ds1 ds2 ....;
dsname2(dsoptions) ....; blanks BY bvar1 bvar2 ....;
INFILE filelocation infileoptions; run;
LENGTH cvar1 $length1 What happens if records are in one dataset but not the other?
cvar2 $length2 ....; 2 Importing data from database systems Refer to manuals for use of IN= variables to keep track of which
INPUT var1 cvar2 var3 cvar4 ....; dataset is active in the merge.
ATTRIB var1 LABEL= FORMAT= ; Proc IMPORT FILE=filename
... processing statements .... OUT=dsname DBMS=dbms REPLACE; 3.4 Stacking datasets
run; GUESSINGROWS=nnnn;
GETNAMES=yes; Proc SORT data=DS1; by bvar1 bvar2 ...; run;
run; Proc SORT data=DS2; by bvar1 bvar2 ...; run;
1.1 Important INFILE options Common dbms are csv. DATA both;
SET ds1 ds2 ....;
• MISSOVER BY bvar1 bvar2 ....;
• DLM= DSD
• FIRSTOBS=
3 Modifying existing SAS datasets run;
1
• substr(test, begin, length) - differs from C 6 Analysis Procedures OUTPUT OUT=
• upcase(), lowcase() - change case of text statistic(var)=name ....;
• word(string, n) 6.1 FREQ run;
Proc Freq data= ; Popular statistics are n, mean, stddev, stderr, lclm, uclm. See
table v1 * v2 / chisq nocol nopercent; also SUMMARY.
4 Graphical Procedures run;
2
Always use the Welch version of the t-test. Paired statement set wide; 10 Generating random numbers
used for paired t-test or use Proc Univariate on difference. length test $10.;
grade = t1; test="Test1’; output; SAS has a complete set of function to generate pseudo-random
grade = t2; test="Test2’; output; numbers. Some examples are:
6.10 UNIVARIATE
.... • ranuni(seed);
p rand(’uniform’); a U[0,1] with E=.5 and
Proc UNIVARIATE data=.... cibasic robustscale;drop t1 t2 ....; SD= 1/12.
var var1 var2 ....; run; • rannor(seed); rand(”normal”); a N(0,1) with E=0, and
output out= statistic(variable)= ; SD=1; p
run; proc transpose data=long out=wide; • rand(”lognormal”) with E=e.5 , and SD= (e − 1)e.
Generate lots of output! Can also generate histograms etc, but I by studentnumber;
prefer SGplot. var grade;
id test; 11 Bootstrapping
run;
7 Split-Apply-Combine General procedures for CRD/SRS and statistics related to the
mean.
The BY statement can be used with any procedure along with
• Resample K times with replacement from the original
ODS tablename=dsname to send selected output to a SAS
dataset.
9 Sending SAS output to other destina- dataset using Proc SurveySelect and the method=urs,
sampfrac=1 , outhits and reps= options.
Proc SORT data=dsname; by ....; tions • Compute estimate for each bootstrap sample.
Proc BLAH data=dsname;
• Look at bootstrap sampling distribution to compute rele-
BY bvar1 bvar2; Refer to manual for extensive help on using ODS. Common us- vant quantities of interest.
... statements... age to create PDF or MSWord document.
ODS tablename=newds;
ODS PDF FILE=’filename.pdf’ style=.....;
run;
.... procedures that generate output ... 12 Macro Variables
ODS PDF CLOSE;
8 Wide-Long and Long-Wide ODS RTF FILE=’filename.rtf’ style=.....; %LET mvar= ...; /* sets the macro variable */
.... procedures that generate output ... Use &mvar where you want to replace the macro variable by the
data long; ODS RTF CLOSE; assigned text. Careful of quotes, i.e. “&mvar” vs ‘&mvar’.