You are on page 1of 48

Introduction to SAS Essentials

Mastering SAS for Data Analytics

Alan Elliott and Wayne Woodward


1 SAS ESSENTIALS -- Elliott & Woodward
Chapter 5
Preparing to Use SAS Procedures

2 SAS ESSENTIALS -- Elliott & Woodward


LEARNING OBJECTIVES
 To be able to use SAS® Support Statements
 To be able to use TITLE and FOOTNOTE
 To be able to include comments in your code
 To be able to use RUN and QUIT correctly
 To understand SAS PROC statement syntax
 To be able to use VAR statements
 To be able to use BY statements
 To be able to use ID statements
 To be able to use LABEL statements in a SAS procedure
 To be able to use WHERE statements
 To be able to use PROC PRINT
 Going Deeper: To be able to use common System Options
 Going Deeper: To be able to split column titles

3 SAS ESSENTIALS -- Elliott & Woodward


5.1 UNDERSTANDING SAS SUPPORT STATEMENTS
Using TITLE and FOOTNOTES Statements
 Specify up to 10 titles or footnotes

First line of either Title or


TITLE ‘title text’; Footnote
FOOTNOTE ‘footnote text’;
or
Define line 2 to 9 of Titles or
TITLEn ‘title text’; Footnotes
FOOTNOTEn ‘footnote text

4 SAS ESSENTIALS -- Elliott & Woodward


TITLE and FOOTNOTES Examples

TITLE 'The first line of the title';


TITLE2 'The second line of the title';
TITLE5 'Several lines skipped, then this
title on the fifth line';
FOOTNOTE 'This is a footnote';
FOOTNOTE3 'This is a footnote, line 3';

 Cancel all TITLE and FOOTNOTE lines with the statement

TITLE; FOOTNOTE;
 Do Hands On Exercise P 114

5 SAS ESSENTIALS -- Elliott & Woodward


HANDS ON EXAMPLE P 114 (DTITLE1.SAS)
Creates titles on lines 1, 2 and 4,
and a Footnote on line 1 of
footnotes.

The title for line 1 is retained,


TITLE4 is erased, FOOTNOTE 1
remains.

All titles and footnotes are erased.

6
CUSTOMIZING TITLES AND FOOTNOTES
 There are a number of options that can be used with the
TITLE or FOOTNOTE statements to customize the look of
your title.
 Some of these options include specifying color with a C=
or COLOR= option. For example:

TITLE C=BLUE H=5 "This is a title";

 Title appears in (C= or COLOR=) blue with a height (H=


or HEIGHT= ) larger than normal. (H=1 is default)
 See Appendix A for colors, fonts, and other options.
7 SAS ESSENTIALS -- Elliott & Woodward
Customizing Titles and Footnotes
 There are a number of options that can be used with the
TITLE or FOOTNOTE statements to customize the look of
your title.
 Some of these options include specifying color with a C=
or COLOR= option. For example:

TITLE C=BLUE H=5 "This is a title";

 This title appears in the color (C= or COLOR=) blue with a


height (H= or HEIGHT= ) larger than normal.
 Do Hands on Example p116.
8 SAS ESSENTIALS -- Elliott & Woodward
9 SAS ESSENTIALS -- Elliott & Woodward
Including Comments in Your SAS Code
 It is a good programming practice to include explanatory comments
in your code. There are two options for putting comments in your
code
 Method 1 - Begin with an asterisk (*), and end with a semi-colon (;).

*This is a message
It can be several lines long
But it always ends with an ;

*************************************************************
* Boxed messages stand out more, still end in a semicolon *
*************************************************************;
DATA MYDATA; * You can put a comment on a line of code;

10 SAS ESSENTIALS -- Elliott & Woodward


 Comments Method 2 – Begin with /* and end with */

/*This is a SAS comment*/


The code from /* to */ Is ignored
by SAS

/* Use this comment technique to comment out lines of code


PROC PRINT; These semi-colons are ignored.
PROC MEANS;
End of comment – the PROCS were ignored*/

11 SAS ESSENTIALS -- Elliott & Woodward


Using RUN and QUIT Statements
 The RUN statement causes previously entered SAS statements
to be executed. It is called a boundary statement. For
example:

PROC PRINT;
PROC MEANS;
RUN;
 Another boundary statement is the QUIT statement. It is
sometimes used in conjunction with a RUN statement to cease
an active procedure. For example:

PROC REG;
RUN;
QUIT;
12 SAS ESSENTIALS -- Elliott & Woodward
5.2 UNDERSTANDING PROC STATEMENT SYNTAX
 Although there are scores of SAS PROCs (procedures), the
syntax is consistent across all of them. The general syntax
of the SAS PROC statement is:

PROC name options;


Statements/statementoptions;
. . .etc. . .
Statements/statementoptions;
RUN;

13 SAS ESSENTIALS -- Elliott & Woodward


Four parts of a PROC Statement

PROC name options; statements/statementoptions;

The name of
the SAS OPTIONS appear STATEMENTS Statements may
procedure BEFORE the appear as a have their own
such as semicolon. Typical separate “phrase” options following
MEANS or options are DATA=, (with its on a slash (/).
PRINT. NOPRINT, and others semicolon.) These
– typically deal with usually specify
the data set or options within the
output. procedure. There
may be multiple
Statements.

14 SAS ESSENTIALS -- Elliott & Woodward


Example PROC Syntax (Options)
 The most commonly used option within the PROC
statement is the DATA= option. For example:

PROC PRINT DATA=MYDATA;


RUN;
Note that options appear in
the PROC statement BEFORE
the semicolon.
 The DATA= option tells SAS which data set to use in the
analysis.

15 SAS ESSENTIALS -- Elliott & Woodward


Example PROC Syntax (Statements)
 Procedure statements are often required to indicate
information about how an analysis is to be performed.
For example:

PROC PRINT DATA=MYDATA;


VAR ID GROUP TIMEl TIME2;
RUN;
STATEMENTS appear AFTER the first PROC
semicolon

16 SAS ESSENTIALS -- Elliott & Woodward


Example PROC Syntax (Statement Options)
 Statements can themselves have options. For example:

PROC FREQ DATA=MYDATA;


TABLES GROUP*SOCIO/CHISQ;
RUN;

This is a statement option –


note that it follows a slach
(/) within the Statement

17 SAS ESSENTIALS -- Elliott & Woodward


Summary of typical PROC Syntax

PROC name PROC option

PROC FREQ DATA=MYDATA;


TABLES GROUP*SOCIO/CHISQ;
RUN;
PROC Statement PROC Statement Option

18 SAS ESSENTIALS -- Elliott & Woodward


Common PROC options

 Some typical options that are COMMON to most


PROCS include:

 DATA= Specify data set to use in the analysis


 NOPRINT Do not display certain output
 OUT= Send results to an output data set

19 SAS ESSENTIALS -- Elliott & Woodward


COMMON PROC STATEMENTS

20 SAS ESSENTIALS -- Elliott & Woodward


Common PROC Statements
 VAR variable(s); Instructs SAS to use only the variables
in the list for the analysis.
 BY variable(s); Repeats the procedure for each
different value of the named variable(s). (The data set
must first be sorted by the variables listed in the BY
statement.)
 ID variable(s); Instructs SAS to use the specified
variable as an observation identifier in a listing of the
data.
 LABEL var='label'; Assigns a descriptive label to a variable.
 WHERE (expression); Instructs SAS to select only
those observations for which the expression is true.

21 SAS ESSENTIALS -- Elliott & Woodward


OPTIONS Specific to a PROC
 In PROC MEANS, for example, the MAXDEC option
specified how many decimal places to report.
 The options specific for PROC will be covered as the
PROCS are introduced

PROC MEANS
DATA=“C:\SASDATA\SOMEDATA”
MAXDEC=2;
RUN; Notice that BOTH the DATA= and
the MAXDEC= options are within
the first semicolon.

22 SAS ESSENTIALS -- Elliott & Woodward


Using the VAR Statement in a SAS Procedure
 The VAR Statement is often used to specify a list of
variables to use in an analysis

VAR varlist;
 An example is as follows:

PROC MEANS;
VAR HEIGHT WEIGHT AGE;
RUN;

23 SAS ESSENTIALS -- Elliott & Woodward


Listing a range of Variables

 List a range of variables with consecutive numeric


suffixes such as Q1 , Q2, Q3, etc. to Q50 using a single
dash between the first and last: Q1-Q50:

VAR Q1-Q50;

 List a range of variables without consecutive suffixes


with two dashes. Example:
USE two dashes to indicate
all variables between the
VAR ID - - TIME4; indicated names

24 SAS ESSENTIALS -- Elliott & Woodward


Using the BY Statement in a SAS Procedure
 The BY statement allows you to quickly analyze subsets of
your data. Repeat an analysis for each value in BY (data
must be in sorted order by BY variable.) Example:

Sort
PROC SORT DATA="C:\SASDATA\SOMEDATA"
OUT=SORTED;
Note OUT= in OPTIONS
BY GP;RUN;
PROC MEANS DATA=SORTED MAXDEC=2;
BY GP;
BY is used first to SORT,
RUN; then to request analysis
by group.

25 SAS ESSENTIALS -- Elliott & Woodward


HANDS-ON EXERCISE P 121
 Open the File DSORTMEANS.SAS

SORT the data set by


GP, then use the BY
statement in a PROC
Statement to do
analyses by the
indexed values (GP)

26 SAS ESSENTIALS -- Elliott & Woodward


RESULTS
 Multiple results displayed

GP is the BY variable…
thus multiple analyses
BY GP

EXERCISE – Change the BY value to STATUS instead of GP (Sort first). Rerun the analysis.
PAUSE. Continue once you have completed this exercise

27 SAS ESSENTIALS -- Elliott & Woodward


RESULTS

Now the output contains


analyses by the values of
STATUS.

28
5.3 USING THE ID STATEMENT IN A SAS PROCEDURE
 The ID statement provides you with a way to increase the
readability of your output. It instructs SAS to use the
specified variable as an observation identifier in a listing
of the data (Instead of the OBS column.)
* FIRST VERSION;
PROC PRINT DATA=MYSASLIB.SOMEDATA;RUN;
* SECOND VERSION;
PROC PRINT DATA=MYSASLIB.SOMEDATA;
ID RAT_ID;
RUN; RAT_ID is the ID variable

29 SAS ESSENTIALS -- Elliott & Woodward


EXAMPLE OF ID STATEMENT
Observe Results, first
without ID Statement

Second time with ID


Statement

Notice how the Obs statement


was replaced with the RAT_ID
column

30 SAS ESSENTIALS -- Elliott & Woodward


5.4 USING THE LABEL STATEMENT IN A SAS
PROCEDURE
 Aversion of the LABEL statement allows you to create
labels for variable names within a procedure.
LABEL var='label';
 Assigns a descriptive label to a variable. Example:

NOTE: This assignment


PROC PRINT LABEL; of a LABEL only works
during this PROC,
ID RAT_ID; unlike the LABEL
statement in a DATA
LABEL TRT='Treatment';
Step that is saved
RUN; within the data set.

31 SAS ESSENTIALS -- Elliott & Woodward


Hands-On Exercise p 124 (D_ID.SAS)
 Example of the LABEL Statement in a PROC
 This code:
PROC MEANS DATA=WEIGHT;
LABEL WT_GRAMS="Treatment"
MDATE="MEDOBS Date";
RUN;
 Produces this output

32 SAS ESSENTIALS -- Elliott & Woodward


5.5 USING THE WHERE STATEMENT IN A SAS
PROCEDURE
 The WHERE statement allows you to specify a conditional
criterion for which output will be included in an analysis.
Example

WHERE TRT="A";

within a PROC statement causes the procedure to only use


records in the dataset that match the criteria TRT=“A”.

33 SAS ESSENTIALS -- Elliott & Woodward


HANDS ON EXERCISE P 125
Using the code from the previous Hands-On Example
(D_ID.SAS), modify the PROC MEANS statement:
PROC MEANS DATA=SORTED;
VAR TIME1 TIME2;
BY STATUS;
RUN;

EXERCISE - Add the following statement after the BY Statement,


and before the RUN statement

WHERE STATUS LT 4;

Run the edited program and observe the results.


PAUSE
34 – Continue once you’ve completed
SAS ESSENTIALS this exercise.
-- Elliott & Woodward
RESULTS

Only the first 3 STATUS


value analyses appear in
the output.

35
Do Hands On Example p 125
 Example of the WHERE statement

PROC PRINT LABEL DATA=WEIGHT;


ID RAT_ID; This code produces this
LABEL TRT='Treatment ' ; output… for only TRT=“A”

WHERE TRT="A";
RUN;

36 SAS ESSENTIALS -- Elliott & Woodward


5.6 USING PROC PRINT
Although several
previous examples
have used a simple
version of the PROC
PRINT procedure, a
number of options
for this procedure
have not been
discussed. Here are
common options:

37 SAS ESSENTIALS -- Elliott & Woodward


Common Statements for PROC PRINT

 For example, the SUM statement specifies that a sum


of the values for the variables listed is to be reported.

SUM COST;

38 SAS ESSENTIALS -- Elliott & Woodward


Do Hands On Example p 127
 Using APRINT1.SAS

PROC PRINT DATA="C:\SASDATA\SOMEDATA"


N = 'Number of Subjects is: '
Obs='Subjects';
SUM TIME1 TIME2 TIME3 TIME4;
TITLE 'PROC PRINT Example';
RUN;

39 SAS ESSENTIALS -- Elliott & Woodward


Output from example showing PROC PRINT options
and statement results

40 SAS ESSENTIALS -- Elliott & Woodward


5.7 GOING DEEPER: SPLITTING COLUMN TITLES IN
PROC PRINT
 Normally, SAS splits titles at blanks when needed to conserve
space in a report.
 If you want a different look, you can tell SAS where you want
the labels to be split using the SPLIT= option. For example:
PROC PRINT DATA=SOMEDATASET;
SPLIT='*‘
LABEL INC_KEY='Subject*ID*============'
AGE='Age in*2014*============'
GENDER='Gender* *============‘;

 In this code, SAS splits the labels where it sees an asterisk. Do the
Hands On Example p 129 (APRINT3.SAS).

41 SAS ESSENTIALS -- Elliott & Woodward


Results of using the Split Option
Note how the splits for
labels occur – according to
where the asterisks were in
the code.

42 SAS ESSENTIALS -- Elliott & Woodward


5.8 GOING DEEPER: COMMON SYSTEM OPTIONS
 Although not a part of a PROC statement, System Options
can be used to customize the way output is displayed or
how data in a data set is used.
 This section introduces some commonly used options.
System Options are specified using the OPTIONS
statement. The syntax for the OPTIONS statement is

OPTIONS option1 option2 ... ;


 For example
OPTIONS ORIENTATION=LANDSCAPE;

43 SAS ESSENTIALS -- Elliott & Woodward


Common System Options (See Table 5.14)
Common System Options Meaning
FIRSTOBS=n and OBS=n; Specifies the first observation to be used in a
data set (FIRSTOBS=) and the last observation
to be used (OBS= ). For example
OPTIONS FIRSTOBS=2 OBS=21;
causes SAS to use data records 2 through 21 in
any subsequent analysis. When this option is
set, it is usually a good idea to reset the values
to
OPTIONS FIRSTOBS=1; OBS=MAX;
at the end of the program so subsequent
analyses are not limited by the same options.
YEAR CUTOFF= year Specifies the cutoff year for two digit dates in a
100 year span starting with the specified date.
For example if YEARCUTOFF=1920 then the
data 01115119 would be considered 2019
while 01/15/21 would be seen as 1921.
The default YEARCUTOFF is 1926. (For SAS
versions 9 through 9.3, the cutoff year was
1920.)
44 SAS ESSENTIALS -- Elliott & Woodward
Common System Options (continued)
System Options Meaning
PROBSIG=n Specifies the number of decimals used when
reporting p-values. For example PROBSIG=3
would cause p-values to be reported to three
decimal places.

LINESIZE= n and PAGESIZE= n Controls number of characters in an output line


(LINESIZE) or number of lines on a page
(PAGESIZE) for RTF and PDF output.

NONUMBER Specifies no page numbers will be included in


RTF or PDF output.

NODATE Specifies no date will be included in RTF or PDF


output.

ORIENTATION=option Specified paper orientation. Options are


PORTRAIT or LANDSCAPE for RTF or PDF
output.
NOCENTER
45
Left justifies output (default is centered)
SAS ESSENTIALS -- Elliott & Woodward
Do Hands On Exercise p 132
 (SYSOBS.SAS) Sets system option so only records 11 to
20 are used in any future data sets.

OPTIONS FIRSTOBS=11 OBS=20;


PROC PRINT LABEL
DATA="C:\SASDATA\SOMEDATA";
RUN;
OPTIONS FIRSTOBS=1 OBS=MAX;

It is important to reset the options to the


defaults to avoid an error in future data
sets

46 SAS ESSENTIALS -- Elliott & Woodward


5.9 SUMMARY
 This chapter introduced you to the syntax of SAS
procedures in preparation for using specific PROCs
discussed in the remainder of the book. It also introduced
PROC PRINT and illustrated some of the common options
used for this procedure.
 Continue to Chapter 6: SAS® ADVANCED PROGRAMMING
TOPICS PART 1

47 SAS ESSENTIALS -- Elliott & Woodward


These slides are based on the book:

Introduction to SAS Essentials


Mastering SAS for Data Analytics, 2nd Edition

By Alan C, Elliott and Wayne A. Woodward

Paperback: 512 pages


Publisher: Wiley; 2 edition (August 3, 2015)
Language: English
ISBN-10: 111904216X
ISBN-13: 978-1119042167

These slides are provided for you to use to teach SAS using this book. Feel free to
modify them for your own needs. Please send comments about errors in the slides
(or suggestions for improvements) to acelliott@smu.edu. Thanks.

48 SAS ESSENTIALS -- Elliott & Woodward

You might also like