s as tutorial
TRANSCRIPT
-
8/11/2019 s as Tutorial
1/10
Using SAS PC with Windows
USING SAS PC WITH WINDOWSStatistics 511
Professor Naomi Altman
revised from previos editions !" #cS$ane and Altman%
and Ns$in"a!a&o!e'e and Altman
TA()* O+ CONT*NTS
A, O-*.-I*W O+ TH* SAS S/ST*# 0
(, TWO ST*PS N**D*D IN TH* SAS P.OG.A##ING )ANGUAG* 0
C, S/NTA 2
D, CHA.ACT*.ISTICS O+ A SAS DATA S*T 3
D, C.*ATING A SAS DATA S*T 5
*, C.*ATING A SAS P.OG.A# +O. D*SC.IPTI-* STATISTICS 4
F. PRODUCING A REPORT FROM A SAS OUTPUT 10
G. H*)P+U) HINTS 1
1
-
8/11/2019 s as Tutorial
2/10
Using SAS PC with Windows
A, O-*.-I*W O+ TH* SAS S/ST*#
SAS (Statistical Analysis System) is a software system/package for data analysis. SAS proides
tools for! information storage and file handling" data modification and management" statistical
analysis" and report writing.
#he SAS system is a powerf$l programming lang$age pl$s a collection of ready%to%$se programs
called proced$res or P&'Cs which can perform a large ariety of applications.
We will $se primarily the *asic and Statistical tools + a small fraction of the capa,ilities of SAS.
'n%line doc$mentation is aaila,le at www.sas.ps$.ed$. #here is also on%line help when yo$ r$n
SAS PC ,$t this is diffic$lt to $se.
(, TWO ST*PS N**D*D IN TH* SAS P.OG.A##ING )ANGUAG*
#he SAS lang$age has its own oca,$lary and synta- % words and the r$les for p$tting them
together.
A SAS statement is a string of SAS keywords SAS names and special characters and operators
ending in a semicolon that instr$cts SAS to perform an operation or gies SAS information.
A se$ence of SAS statements is called a SAS program.
A SAS program consists of two kinds of steps! A#A steps and P&'C steps. A#A and P&'C
steps can appear in any order and any n$m,er of A#A and P&'C steps can ,e $sed in a SAS
program.
Us$ally A#A steps create SAS data sets and P&'C steps do analysis of SAS data sets. A P&'C
may also create aria,les s$ch as resid$als and fitted al$es which can ,e placed in a new data set
or appended to an e-isting data set.
A A#A step is a gro$p of SAS statements that ,egins with a A#A statement. 0-ample!
A#A '0" Creates a data set named "ONE".
23240 5A!6204.#7#5" Reads the data from the file A:YIELD.TXT
2PU# #&0A# &0P 6204" The file has !ariales named TREAT# RE$ and YIELD.4'8694'8 (6204)" A ne% !ariale LO&Y is created and added to "ONE".
#he A#A step ,egins with a A#A statement and can incl$de any n$m,er of program statements.
6o$ can $se the A#A step for these p$rposes!
: retrieal! getting inp$t data from a file
: editing! checking for errors in the data and correcting them" comp$ting new aria,les"
;
http://www.sas.psu.edu/http://www.sas.psu.edu/http://www.sas.psu.edu/ -
8/11/2019 s as Tutorial
3/10
Using SAS PC with Windows
: o$tp$tting! write data sets to disk"
: creating! prod$cing new SAS data sets from e-isting ones ,y s$,setting merging and $pdating.
0ery SAS data set has a name. *y defa$lt SAS $ses the c$rrently actie data set which is the
one most recently called as in'(tto a A#A or P&'C statement. 2f yo$r program $ses seeral data
sets it is ,est to call the re$ired data set $sing A#A9datasetname when yo$ need to $se it. #hat
will aoid pro,lems as yo$ change yo$r program.
#he A#A step can incl$de statements telling SAS to create one or more new SAS data sets and
programming statements that perform the manip$lations necessary to ,$ild the data sets. Creating a
new data set does not change the c$rrently actie data set.
A P&'C is a gro$p of SAS statements that ,egins with a P&'C statement. 0-ample!
P&'C &08 A#A9
-
8/11/2019 s as Tutorial
4/10
Using SAS PC with Windows
#he SAS system reads data (letters or n$m,ers) in ario$s forms and organiEes them into a
SAS data set which is similar to a spreadsheet. 'nce the data hae ,een organiEed into a SAS data set
yo$ can access analyEe reise and display the data. 6o$ can also store datasets + howeer for small
datasets it is most conenient to store them as te-t files.
#he data consist of the following components! data al$e aria,le and o,seration.
ata al$e is a single $nit of information (a single cell)
Baria,le is a set of data al$es descri,ing a characteristic (a single col$mn)
',seration is a set of data al$es for the same item (a single row)
#he data set following contains F aria,les 1 o,serations and GH data al$es one of which is
missing.
aria,les
A=0 S07 A80
-
8/11/2019 s as Tutorial
5/10
Using SAS PC with Windows
(2n the SAS man$als yo$ will occasionally see data im,edded in a A#A step.
-
8/11/2019 s as Tutorial
6/10
Using SAS PC with Windows
After opening SAS one is prompted to the following screen with two windows (see ,elow). #he $pper
window is a 4og window showing the SAS statements which hae already ,een processed along with
comments. #he ,ottom window is the Program editor window. 6o$ will enter and edit yo$r SAS
program in the Program editor window
ow create the following SAS program in the Program 0ditor window. Use $pper or lower case letters
as yo$ choose.
/*
J
-
8/11/2019 s as Tutorial
7/10
Using SAS PC with Windows
THIS PROGRAM IS USED TO CREATE A SMALL SAS PROGRAM
WRITTEN BY: LAST NAME, FIRST NAME OF STUDENT.
DATE: MONTH/DAY/YEAR
*/
#he te-t a,oe which is delimited ,y /: :/ is a comment and is ignored ,y SAS. 2t is helpf$l to $secomments as a way to doc$ment yo$r data.
OPTIONS LS=79 NOCENTER; OPTIONS picks options for the output. LS=selects the number of characters per line.
TITLE 'SUMMARY STATISTICS'; TITLE provides a title thatappears on each page of the output.
DATA MARY; e no! create the data set named "#$%&".INFILE 'A:STEERS.TT'; e read the data from $'STEE%S.T(TINPUT LI!EWT DRESSWT; There are t!o variables named LI)ET and
*%ESST. The variable names are separated b+blanks. &ou need to name all variables inthe data set, even if +ou do not !ant to usethem all.
TITLE" 'PRINTING LI!EWT'; TITLE- provides a subtitle. It can be usede.g. if several anal+ses are performed in the
same program.PROC PRINT DATA=MARY; e no! run our rst P%O/. It prints some or
all of the data in data set #$%&.!AR LI!EWT; Tells S$S to print LI)ET onl+. Other!ise it
prints all of the data.RUN; The %0N command can be used to terminate a
P%O/ or *$T$ step. /ommands +ou submit !illnot run until a %0N command is added.
#ry to r$n the SAS program now ,y clicking the SU*=2# icon (the r$nning fig$re) or ,y pressing the
key f$nction 3D. 4ook at yo$r SAS o$tp$t. 6o$ sho$ld see a list of the data. 2f yo$ do not yo$ hae
made an error.
-
8/11/2019 s as Tutorial
8/10
Using SAS PC with Windows
(written in red) in the 4'8 window. ERROR: File WORK.MARIAM.DATA does o! e"is!.
'nce a SAS program has ,een s$,mitted for processing error messages are written in the 4og window.
#hey can ,e accessed as follows! Windo; 6 )o7,ow open the 4og window and scroll down to read
the error message. As mentioned preio$sly it is ass$med that yo$ hae cleared the 4og window of its
preio$s contents. 2f not clear this window and r$n SAS program again.
&eopen yo$r SAS program file as follows! Windo; 6 Pro7ram *ditor 6 )ocals 6 .ecall Te:t,8o to
the statement PROC PRINT DATA=MARIAM; and changeMARIAM,acktoMARY.
Add t$e remainin7 statements !elo; to "or SAS pro7ram,
PROC UNI#ARIATE DATA=MARY; $ROC +NI/ARIATE 'rints s(mmar0 statistics. It is 'artof ,A,
1A,IC# rather than ,A, ,TATI,TIC,.#AR $I#EWT DRESSWT; e %ill otain s(mmar0 statistics for oth !ariales.
%%P$OT; e re2(est a Normal $roailit0 $lot for oth !ariales.
RUN;
Sae the SAS program file on A89drie as follows! +ile 6 Save 6 A89steers,sas 6 Save
#o r$n the SAS program click on the SU*=2# icon or simply press the 3D key. 'ther key
f$nctions are defined $nder! Help 6 >e",
*y selecting the Window men$ yo$ can open the '$tp$t Program 0ditor and 4og windows
wheneer necessary. #he '$tp$t window can ,e selected and opened the same way the other two
windows are opened.
6o$ can sae the '$tp$t windows contents as follows! +ile 6 Save 6 A89steers,lst 6 Save2 $s$ally
c$t and paste the entire window into a te-t editor as descri,ed ,elow.
-
8/11/2019 s as Tutorial
9/10
Using SAS PC with Windows
+, P.ODUCING A .*PO.T +.O# A SAS OUTPUT
#here are many different ways to prod$ce a report $sing SAS o$tp$t. We will go thro$gh one way
which ass$mes that yo$ hae a te-t editor s$ch as Word on yo$r comp$ter and that yo$r comp$ter is
powerf$l eno$gh to r$n the editor and SAS at the same time.
Start t$e Te:t *ditor,
2n the editor write yo$r report. 3or e-ample type the heading and introd$ctory material descri,ing the
pro,lem yo$ analyEed. isc$ss yo$r analysis of the data. S$ppose yo$ wo$ld like to incl$de a SAS
analysis o$tp$t in yo$r report.
Cop" anal"sis otpt to t$e Clip!oard8
'pen the SAS o$tp$t! Windo; 6 otpt,
*dit6 Select all? *dit6 Cop"? ote that copying only a portion of the o$tp$t ,y highlighting does
not always work. 2 copy the entire o$tp$t to a te-t editor and edit there.
'pen the te-t editor and paste yo$r SAS o$tp$t! *dit 6 Paste,
6o$ will realiEe that the SAS o$tp$t has the SAS monospace font siEe 1H ,y defa$lt, 6o$ needto
modify the font siEe for a ,etter o$tp$t. Proceed as follows to modify the font siEe of yo$r pasted o$tp$t.
e.g. 2n =icrosoft Word choose *dit 6 Select All and change the SAS monospacefont to size to 8.
ow yo$ can edit yo$r word doc$ment ,y adding te-t and/or remoing parts of the o$tp$t yo$ L$dge
$nimportant.
6o$ can also copy and paste graph sheets directly into yo$r doc$ment.
Sae yo$r report as follows! +ile 6 Save 6 A89report,doc6 Save
ow yo$ can print yo$r report in =icrosoft Word as follows! +ile 6 Print 6 Clic& O>
G
-
8/11/2019 s as Tutorial
10/10
Using SAS PC with Windows
G, H*)P+U) HINTS
0ery SAS statement ends with a semicolon ". 6o$ may contin$e statements on two or more lines.
3orgetting the semicolon (") leads to error messages which are hard to decipher. #his is always the
first thing to check if yo$r program does not r$n.
#he ne-t most common error is misspelling a SAS command or aria,le name. SAS aria,le
names can ,e no more than characters long.
#he third most common error is trying to $se a aria,le that is not aaila,le. 3or e-ample this error
can occ$r if yo$ try to draw resid$al plot ,$t forgot to store the resid$als from the regression or if
yo$ are in the wrong data set.
SAS ignores e-tra ,lanks incl$ding ,lank lines so yo$ can space yo$r program so that it is neat
and easy to read.
6o$ can p$t more than one statement on a line ,$t this $s$ally makes yo$r program hard to read.
SAS program files can get $ite long so it is $sef$l to keep them reada,le.
Statements added in the SAS editor are not saed in yo$r SAS program file. 2f yo$ want to hae
them aaila,le for f$t$re $se yo$ m$st e-plicitly sae them $sing file6save
=ost P&'Cs can $se only one data set. 2f aria,les from m$ltiple data sets are needed the data
sets can ,e merged in a A#A step.
#he online SAS help can ,e diffic$lt to $se d$e to statements with the same name in different
P&'Cs. When seeking help for a P&'C try searching on P&'C. #his gies a list of all the
P&'Cs. 6o$ can then click on the P&'C yo$ want which gies a list of the statements alid for
that P&'C. 6o$ will likely find the online man$al more $sef$l.
1H