Getting Started with the SAS - PowerPoint PPT Presentation

1 / 147
About This Presentation
Title:

Getting Started with the SAS

Description:

Chapter 2 Getting Started with the SAS System Section 2.1 Introduction to SAS Programs Objectives State the components of a SAS program. State the modes in which ... – PowerPoint PPT presentation

Number of Views:282
Avg rating:3.0/5.0
Slides: 148
Provided by: KathyK91
Category:

less

Transcript and Presenter's Notes

Title: Getting Started with the SAS


1
Chapter 2
  • Getting Started with the SAS System

2
Section 2.1
  • Introduction to SAS Programs

3
Objectives
  • State the components of a SAS program.
  • State the modes in which you can run a SAS
    program.

4
SAS Vocabulary
  • SAS program
  • DATA step
  • PROC step

5
Turning Data into information
6
SAS Programs
  • A SAS program is a sequence of steps that the
    user submits for execution.

Raw Data
DATA steps are typically used to create SAS data
sets.
129986542,Fred,Barnes,54,654543681,Alicia,Burnett,
23,241453384,Connor,Coats
SAS Data Set
DATAStep
LastName FirstName Age TORRES
JAN 23LANGKAMM SARAH 46SMITH
MICHAEL 71WAGSCHAL NADJA 37TOERMOEN
JOCHEN 16
SAS Data Set
Output
PROCStep
LastName FirstName Age TORRES
JAN 23LANGKAMM SARAH 46SMITH
MICHAEL 71WAGSCHAL NADJA 37TOERMOEN
JOCHEN 16
PROC (procedure) steps are used to process SAS
data sets (generate reports and sort data).
7
SAS Programs
data work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means
datawork.staff class JobTitle var
Salary run
DATAStep
PROCSteps
8
SAS Programs
  • SAS executes one step at a time, statement by
    statement, independently of one another.
  • Other programming languages compile the entire
    program, and then execute the entire program at
    once.

9
Step Boundaries
  • SAS steps begin with DATA statements or PROC
    statements.
  • SAS detects the end of a step when it encounters
  • a RUN statement (for most steps)
  • a QUIT statement (for some run-group procedures)
  • the beginning of another step (DATA or PROC
    statement).

data work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means
datawork.staff class JobTitle var
Salary run
DATAStep
PROCSteps
10
Step Boundaries
data work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff proc means datawork.staff
class JobTitle var Salary run
11
Running a SAS Program
  • Versions of SAS exist for several different
    operating systems.
  • Windows
  • SAS Windowing Environment
  • SAS Enterprise Guide (point-and-click interface)
  • UNIX
  • SAS Windowing Environment only
  • Mainframe
  • SAS Windowing Environment only

12
SAS Windowing Environment
  • Interactive windows enable you to interface with
    SAS.
  • SAS Windowing Environment Interface

13
Section 2.2
  • Running SAS Programs

14
Objectives
  • Start SAS and include a SAS program in your
    session.
  • Submit a program and browse the results.
  • Navigate the SAS windowing environment.

15
SAS Vocabulary
SAS Output CLEAR LOG
INCLUDE OUTPUT
Editor window WED
SUBMIT Enhanced Editor
SAS log Program Editor
LOG KEYS
16
Submitting a SAS Program
When you execute a SAS program, the output
generated by SAS is divided into two major parts
SAS log contains information about the processing of the SAS program, including any warning and error messages.
SAS output contains reports generated by SAS procedures and DATA steps.
17
SAS Log
1 data work.staff 2 infile
'raw-data-file' 3 input LastName 1-20
FirstName 21-30 4 JobTitle 36-43
Salary 54-59 5 run NOTE The infile
'raw-data-file' is File Name
'raw-data-file', RECFMV,LRECL256 NOTE 18
records were read from the infile
'raw-data-file'. The minimum record length
was 59. The maximum record length was
59. NOTE The data set WORK.STAFF has 18
observations and 4 variables. 6 proc print
datawork.staff 7 run NOTE There were 18
observations read from the dataset WORK.STAFF. 8
proc means datawork.staff 9 class
JobTitle 10 var Salary 11 run NOTE
There were 18 observations read from the dataset
WORK.STAFF.
18
PRINT Procedure Output
The SAS System
First Obs LastName
Name JobTitle Salary 1
TORRES JAN Pilot 50000
2 LANGKAMM SARAH Mechanic
80000 3 SMITH MICHAEL
Mechanic 40000 4 LEISTNER
COLIN Mechanic 36000 5 WADE
KIRSTEN Pilot 85000 6
TOMAS HARALD Pilot 105000
7 WAUGH TIM Pilot
70000 8 LEHMANN DAGMAR
Mechanic 64000 9 TRETTHAHN
MICHAEL Pilot 100000 10 TIETZ
OTTO Pilot 45000 11
O'DONOGHUE ART Mechanic 52000
12 WALKER THOMAS Pilot
95000 13 NOROVIITA JOACHIM
Mechanic 78000 14 OESTERBERG
ANJA Mechanic 80000 15
LAUFFER CRAIG Mechanic 40000
16 TORR JUGDISH Pilot
45000 17 WAGSCHAL NADJA
Pilot 77500 18 TOERMOEN
JOCHEN Pilot 65000
19
MEANS Procedure Output
The SAS System
The MEANS Procedure
Analysis Variable Salary
N JobTitle Obs N Mean Std
Dev Minimum ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Mechanic 8
8 58750.00 19151.65
36000.00 Pilot 10 10 73750.00
22523.14 45000.00 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Analysis Variable Salary
N JobTitle
Obs Maximum
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Mechanic 8 80000.00
Pilot 10 105000.00
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
20
Running a SAS Program
  • This demonstration illustrates how to invoke SAS
    and open and run a SAS program.

21
Demonstration Invoking SAS
  • There are two ways to open SAS
  • Menu-driven
  • Select Start ? Programs ? SAS ? SAS 9.1.
  • Desktop shortcut
  • SAS toolbar button

22
Toolbar
Command Box
Menus
Output Window (behind)
Current Working Directory
23
Demonstration Running a SAS Program
  • There are three ways to open a SAS program
  • Command Box
  • Type the INCLUDE command in the Command Box.
  • Menu-driven
  • Select File ? Open.
  • Toolbar Shortcut
  • Open button

24
Demonstration Running a SAS Program
  • Issue the INCLUDE command in the command box to
    open or include a program in your SAS session.
  • Type include and the name of the file containing
    the program in quotation marks. Then press ENTER.
  • The program is included in the Editor window.

25
Demonstration Running a SAS Program
  • You can use the SAS Enhanced Editor to do the
    following
  • access and edit existing SAS programs
  • write new SAS programs
  • submit SAS programs
  • save SAS programs

26
Demonstration Running a SAS Program
  • In the SAS Enhanced Editor, the syntax in your
    SAS program is color-coded to distinguish between
    the following
  • step boundaries
  • SAS keywords
  • data set names
  • variable names

27
Demonstration Running a SAS Program
  • There are three ways to submit a SAS program
  • Menu-driven
  • Select File ? Submit.
  • Command Box
  • Type the SUBMIT command in the Command Box.
  • Toolbar Shortcut
  • Submit button

28
Demonstration Running a SAS Program
  • If your code generates output, the output is
    displayed in the SAS Output window.
  • When the Output window moves to the front to
    become the active window, you should select and
    view the SAS log first.

29
Demonstration Running a SAS Program
  • Always check the SAS log when you submit a
    program.
  • Browse the messages that the program generated.

30
Demonstration Running a SAS Program
  • There are three ways to view the SAS log
  • Menu-driven
  • Select View ? Log.
  • Command Box
  • Type the LOG command in the Command Box.
  • Tabs
  • Select the Log tab.

31
Demonstration Running a SAS Program
  • Examining Your Program Results
  • The Log window has the following characteristics
  • is one of the primary windows and is open by
    default
  • acts as an audit trail for your SAS session
  • has messages that are written to the log in the
    order in which they are generated
  • As you continue to submit your programs, SAS
    writes messages to the bottom of the log.

32
Demonstration Running a SAS Program
  • Navigating in the Log
  • Use the PAGE UP and PAGE DOWN keys.
  • Use the scroll bar.
  • Make certain that you review the complete log
    entry for your submission.
  • Look in the log for the beginning of your current
    code submission.

33
Demonstration Running a SAS Program
  • The Log window contains the programming
    statements that are submitted, as well as notes
    about the following
  • any files that were read
  • the records that were read
  • the program execution and results

34
Demonstration Running a SAS Program
  • By default, the following conventions appear
  • Notes are written in blue.
  • Read these because they might be 'good' or 'bad,
    depending on the intention.
  • Warnings are written in green.
  • Read these because they might be 'good' or 'bad,
    depending on the intention.
  • Errors are written in red.
  • Read these carefully.
  • Errors stop SAS processing and indicate that
    something must be corrected.

35
Demonstration Running a SAS Program
  • You might want a clean log when you submit your
    program.
  • To clear the contents of the window, do one of
    the following
  • Issue the CLEAR LOG command in the command box.
  • Select Edit ? Clear All.
  • Select the New icon.
  • ? The icon resembles a clean sheet of paper,
    and you want a blank Log window.

36
Demonstration Running a SAS Program
  • There are three ways to view the Output window
  • Menu-driven
  • Select View ? Output.
  • Command Box
  • Type the OUTPUT command in the Command Box.
  • Select the Output tab.

37
Demonstration Running a SAS Program
  • Examining Your Program Results
  • The Output window
  • is one of the primary windows and is open by
    default
  • becomes the active window each time that it
    receives output
  • automatically accumulates output in the order in
    which it is generated.
  • SAS writes to the bottom of the window each time
    that output is generated.

38
Demonstration Running a SAS Program
  • You might want a clean Output window when you
    submit your program.
  • With the Output window active, you can do one of
    the following
  • Issue the CLEAR OUTPUT command in the command
    box.
  • Select Edit ? Clear All to clear the contents of
    the window.
  • Click on the New button.

39
Demonstration Running a SAS Program
  • Examining Your Program Results
  • The Output window displays the last page of
    output generated by the program submitted.
  • To scroll vertically in the Output window, use
    one of the following methods
  • the vertical scrollbar
  • the PAGE UP or PAGE DOWN keys on the keyboard
  • issue the TOP and BOTTOM commands

40
Demonstration Running a SAS Program
  • To return to the Enhanced Editor window, use one
    of the following methods
  • Select the Editor tab.
  • If the window is visible, select it to make it
    active.

41
What Do You Do If
  • What if you close a window accidentally?
  • Select View and the window that you want to open
    (Log, Editor, Output).
  • If you close the Log or Output window, you need
    to reopen it in order to see results.
  • If you close the Editor window, you need to open
    the Enhanced Editor window.

42
What Do You Do If
  • Why are there two different types of editors?
  • On the UNIX and Mainframe platforms, you can only
    use the Program Editor.
  • Windows supports the Program Editor (for backward
    compatibility) as well as the Enhanced Editor.
  • Submit the same program in each window and see
    what happens.

43
Program Editor versus Enhanced Editor
Program Editor Enhanced Editor
Coloring is different. Coloring is different.
Title bar Program Editor Title bar Editor
Has a blue added to the icons
Can only have one window open Can have many windows open
Code not kept after a submit Keeps code after a submit
44
What Do You Do If
  • The code you forgot to save disappeared?
  • Do you have to retype it?
  • Is there way to get it back?

45
What Do You Do If
  • you lost your previously submitted code?
  • Select Run ? Recall Last Submit.
  • The code is returned to your Editor window.

46
Exercise
  • This exercise reinforces the concepts discussed
    previously.

47
Exercises
  • 1. Submitting a Program
  • With the Editor window active, open the SAS
    program c02ex1.sas
  • Submit the program.
  • Based on the report in the Output window, how
    many observations and variables are in the data
    set?
  • Examine the Log window.
  • Based on the log notes, how many observations and
    variables are in the data set?
  • Clear the Log and Output windows.

48
Exercises
  • 2. Issuing the KEYS Command
  • The Keys window is
  • a secondary window
  • used to browse or change function key
    definitions.
  • closed by issuing the END command.
  • Issue the KEYS command. Browse the contents of
    the window by scrolling vertically.
  • Close the KEYS window.

49
Exercises
  • You want to clear the Log and Output windows by
    simply pressing a button.
  • Using the KEYS command, you can set the F12 key
    to issue the commands.

50
Exercises
  • 3. Setting a Function Key
  • Issue the KEYS command to access the Keys window.
  • Click in the definition area next to the F12 key.
  • Type the commands that you want to assign to the
    key, and separate them with a semicolon.Clear
    Log Clear Output
  • Close the window. SAS automatically saves your
    definition in your user preferences in the
    Sasuser library. The key will be set every time
    that you log on to SAS.
  • Press the F12 key. What happens?

51
Exercises
  • Setting a Function Key
  • Any of the commands that can be issued in the
    command box can be assigned to a function key.
  • Notice that SUBMIT is a command, and does not go
    in a program.
  • However, RUN is a statement that goes inside a
    program. You cannot use it in the command box,
    nor as a hot key.
  • Statements go in programs.
  • Commands go in the command box.

52
Section 2.3
  • Mastering Fundamental Concepts

53
Objectives
  • Define the components of a SAS data set.
  • Define a SAS variable.
  • Identify a missing value and a SAS date value.
  • State the naming conventions for SAS data sets
    and variables.
  • Explain SAS syntax rules.
  • Investigate a SAS data set using the CONTENTS and
    PRINT procedures.

54
SAS Vocabulary
  • SAS Data Set
  • Descriptor Portion
  • PROC CONTENTS
  • Data Portion
  • Character Variables
  • Numeric Variables
  • SAS Names
  • SAS Date Value
  • Character Missing Value
  • Numeric Missing Value
  • PROC PRINT
  • SAS Statement
  • Free Format
  • SAS Comments

55
Mastering Fundamental Concepts
  • In this section, you will learn basic SAS
    concepts.
  • These concepts are the basics that you need to
    know so that you can program in SAS.

56
SAS Data Set Terminology
SAS documentation and text in the SAS windowing
environment use the following terms
interchangeably
57
SAS Data Sets
  • A SAS data set consists of two parts.

SAS Data Set
Descriptor Portion
Data Portion
58
SAS Data Sets
  • A SAS data set is a specially structured file
    that contains data values.
  • Data must be in the form of a SAS data set to be
    processed by most SAS procedures.

59
SAS Data Sets
  • SAS data sets have a descriptor portion and a
    data portion.

General data set information data set name
data set label date/time created storage
information number of observations Information
for each variable Name Type Length
Position Format Informat Label
DescriptorPortion

DataPortion
60
Browsing the Descriptor Portion
  • The descriptor portion of a SAS data set simply
    describes the data. It contains the following
  • general information about the SAS data set such
    as data set name and number of observations
  • variable attributes such as name, type, length,
    position, informat, format, and label
  • How can you see the descriptor portion of a SAS
    data set?
  • Use the CONTENTS procedure.

61
Browsing the Descriptor Portion
  • General form of the CONTENTS procedure
  • Example

PROC CONTENTS DATASAS-data-setRUN
proc contents datasashelp.classrun
62
Partial PROC CONTENTS Output
The SAS System The
CONTENTS Procedure Data Set Name SASHELP.CLASS
Observations 19 Member Type DATA
Variables 5 Engine V9
Indexes 0 Created
Wednesday, May 12, 2004 Observation Length
40 105355 PM Last Modified
Wednesday, May 12, 2004 Deleted Observations
0 105355 PM Protection
Compressed NO Data Set Type
Sorted NO
Alphabetic List of Variables
and Attributes Variable
Type Len 3 Age
Num 8 4 Height
Num 8 1 Name
Char 8 2 Sex
Char 1 5 Weight
Num 8

63
SAS Data Sets Data Portion
The data portion of a SAS data set is a
rectangular table of character and/or numeric
data values. Variable names are part of
the descriptor portion, not the data portion.
Variablenames
LastName FirstName JobTitle Salary TORRES
JAN Pilot 50000 LANGKAMM SARAH
Mechanic 80000 SMITH MICHAEL Mechanic
40000 WAGSCHAL NADJA Pilot
77500 TOERMOEN JOCHEN Pilot 65000
Variablevalues
Numeric values
Character values
64
SAS Variable Values
  • There are two types of variables character and
    numeric.
  • Character variables
  • contain any value letters, numbers, special
    characters, and blanks
  • are stored with a length of 1 to 32,767 bytes
  • have one byte equal to one character
  • are left-aligned.

continued...
65
SAS Variable Values
  • Numeric variables
  • are stored as floating point numbers in 8 bytes
    of storage by default, allowing for 16 to 17
    significant digits
  • are not restricted to 8 digits
  • are right-aligned.

66
SAS Data Set and Variable Names
  • SAS data set and variable names must adhere to
    the following rules
  • can be up to 32 characters long.
  • can be uppercase, lowercase, or mixed-case.
  • must start with a letter or underscore.
    Subsequent characters can be numbers, letters, or
    underscores.

67
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
? fivemonthsdata
? FiveMonthsData
...
68
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
...
69
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
...
70
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
...
71
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
...
72
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
...
73
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
...
74
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
...
75
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
...
76
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
? fivemonthsdata
...
77
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
? fivemonthsdata
...
78
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
? fivemonthsdata
? FiveMonthsData
...
79
Valid SAS Names
  • Select the valid default SAS names.

? data5mon
? data5mon
? 5monthsdata
? data5
? five months data
? fivemonthsdata
? FiveMonthsData
80
Exercise
  • This exercise reinforces the concepts discussed
    previously.

81
Exercises
  1. Create your own SAS name.
  2. Create a data set name for containing the names
    and birth dates of your family members for three
    generations.
  3. Create a variable name for the column containing
    the name of the generation to which an individual
    belonged.

82
Dates in SAS
  • An important concept with data is dates.
  • You might need to do calculations on dates.
  • How old is a person or how much time passed
    between two dates?

83
SAS Dates
  • You have two character dates
  • 01Jan2006
  • 01Apr2006
  • If you sorted these dates, they would not display
    in chronological order, but in alphabetic order.
  • 01Apr2006
  • 01Jan2006
  • Dates stored as numbers enable calculations and
    proper sorting.

84
SAS Date Values
  • SAS stores date values as numeric values.
  • A SAS date value is stored as the number of days
    between January 1, 1960, and a specific date.

85
Missing Data Values
A value must exist for every variable for each
observation. Missing values are valid values.
LastName FirstName JobTitle
Salary TORRES JAN Pilot
50000 LANGKAMM SARAH Mechanic
80000 SMITH MICHAEL Mechanic .
WAGSCHAL NADJA Pilot
77500 TOERMOEN JOCHEN 65000
A numericmissing valueis displayedas a period.
A character missing value is displayed as a blank.
86
Browsing the Data Portion
  • The PRINT procedure displays the data portion of
    a SAS data set.
  • By default, PROC PRINT displays the following
  • all observations
  • all variables
  • an OBS column on the left side

87
Browsing the Data Portion
  • General form of the PRINT procedure
  • Example

PROC PRINT DATASAS-data-set RUN
proc print datawork.staffrun
88
PROC PRINT Output
The SAS System
First Obs LastName
Name JobTitle Salary 1
TORRES JAN Pilot 50000
2 LANGKAMM SARAH Mechanic
80000 3 SMITH MICHAEL
Mechanic 40000 4 LEISTNER
COLIN Mechanic 36000 5 WADE
KIRSTEN Pilot 85000 6
TOMAS HARALD Pilot 105000
7 WAUGH TIM Pilot
70000 8 LEHMANN DAGMAR
Mechanic 64000 9 TRETTHAHN
MICHAEL Pilot 100000 10 TIETZ
OTTO Pilot 45000 11
O'DONOGHUE ART Mechanic 52000
12 WALKER THOMAS Pilot
95000 13 NOROVIITA JOACHIM
Mechanic 78000 14 OESTERBERG
ANJA Mechanic 80000 15
LAUFFER CRAIG Mechanic 40000
16 TORR JUGDISH Pilot
45000 17 WAGSCHAL NADJA
Pilot 77500 18 TOERMOEN
JOCHEN Pilot 65000
89
SAS Syntax Rules
  • SAS statements
  • usually begin with an identifying keyword
  • always end with a semicolon.

data work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means
datawork.staff class JobTitle var
Salary run
continued...
90
SAS Syntax Rules
  • SAS statements are free-format.
  • One or more blanks or special characters can be
    used to separate words
  • They can begin and end in any column
  • A single statement can span multiple lines
  • Several statements can be on the same line
  • Unconventional Spacing Example

data work.staff infile 'raw-data-file' input
LastName 1-20 FirstName 21-30 JobTitle
36-43 Salary 54-59 run proc means
datawork.staff class JobTitle var
Salaryrun
91
SAS Syntax Rules
  • SAS statements are free-format.
  • One or more blanks or special characters can be
    used to separate words
  • They can begin and end in any column
  • A single statement can span multiple lines
  • Several statements can be on the same line
  • Unconventional Spacing Example

data work.staff infile 'raw-data-file' input
LastName 1-20 FirstName 21-30 JobTitle
36-43 Salary 54-59 run proc means
datawork.staff class JobTitle var
Salaryrun
92
SAS Syntax Rules
  • SAS statements are free-format.
  • One or more blanks or special characters can be
    used to separate words.
  • They can begin and end in any column.
  • A single statement can span multiple lines.
  • Several statements can be on the same line.
  • Unconventional Spacing Example

data work.staff infile 'raw-data-file' input
LastName 1-20 FirstName 21-30 JobTitle
36-43 Salary 54-59 run proc means
datawork.staff class JobTitle var
Salaryrun
93
SAS Syntax Rules
  • SAS statements are free-format.
  • One or more blanks or special characters can be
    used to separate words.
  • They can begin and end in any column.
  • A single statement can span multiple lines.
  • Several statements can be on the same line.
  • Unconventional Spacing Example

data work.staff infile 'raw-data-file' input
LastName 1-20 FirstName 21-30 JobTitle
36-43 Salary 54-59 run proc means
datawork.staff class JobTitle var
Salaryrun
94
SAS Syntax Rules
...
  • SAS statements are free-format.
  • One or more blanks or special characters can be
    used to separate words.
  • They can begin and end in any column.
  • A single statement can span multiple lines.
  • Several statements can be on the same line.
  • Unconventional Spacing Example

data work.staff infile 'raw-data-file' input
LastName 1-20 FirstName 21-30 JobTitle
36-43 Salary 54-59 run proc means
datawork.staff class JobTitle var
Salaryrun
95
SAS Syntax Rules
  • Indentation and good spacing makes the program
    easier to read.
  • Conventional Spacing Example

data work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means
datawork.staff class JobTitle var
Salary run
96
SAS Comments
  • You can comment a block of text or code.
  • Type / to begin a comment.
  • Type your comment text.
  • Type / to end the comment.

/ Create work.staff data set / data
work.staff infile 'raw-data-file' input
LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run / Produce
listing report of work.staff / proc print
datawork.staff run
97
SAS Comments
  • Commenting one statement.
  • Type an asterisk () at the beginning of the
    statement.
  • Everything that is between the asterisk and the
    semicolon is a comment.
  • ? The comments the entire statement, not only
    one line. How does SAS recognize the end of
    a statement?

data work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run
98
Shortcut to Comments
  • Highlight what you want to comment.
  • Hold down the CTRL and / buttons.
  • To Undo Highlight and hold down the CTRL, SHIFT,
    and / buttons.

99
Exercise Section 2.3
  • This exercise reinforces the concepts discussed
    previously.

100
Section 2.4
  • Diagnosing and Correcting Syntax Errors

101
Objectives
  • Identify SAS syntax errors.
  • Debug and edit a program with errors.
  • Resubmit the corrected program.
  • Save the corrected program.

102
SAS Vocabulary
  • Syntax Errors
  • Options
  • RECALL

103
Syntax Errors
  • Syntax errors include the following
  • misspelled keywords
  • missing or invalid punctuation
  • invalid options

daat work.staff infile emplist.dat
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means datawork.staff
average max class JobTitle var
Salary run
Average is not a valid option.
104
Syntax Errors
  • Procedure Options
  • Options tell SAS that you want the procedure to
    be processed differently than its default
    behavior.
  • In this example, you want only two statistics
    displayed average and max.
  • Average is not a valid option for PROC MEANS.

proc means datawork.staff average max class
JobTitle var Salary run
105
Syntax Errors
  • When SAS encounters a syntax error, SAS
    underlines where SAS thinks things went wrong.
  • The following information is written to the SAS
    log
  • the word ERROR or WARNING
  • the location of the error
  • an explanation of the error

106
Demonstration Debugging a SAS Program
  • This demonstration illustrates these tasks
  • how to submit a SAS program that contains errors
  • diagnosing the errors
  • correcting the errors
  • saving the corrected program

107
Demonstration Debugging a SAS Program
In this demonstration, you open a program that
has errors and submit it. Find the errors and
correct them. Programs to open
c02s4d1.sas c02s4d2.sas
108
Demonstration Debugging a SAS Program
  • Start with the program
  • c02s4d1.sas

109
Demonstration Debugging a SAS Program
daat work.staff infile 'emplist.dat'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means datawork.staff
average max class JobTitle var
Salary run
Submit a SAS Program with Errors
110
Demonstration Debugging a SAS Program
The SAS log contains error messages and warnings.
1 daat work.staff ---- 14 WARNING
14-169 Assuming the symbol DATA was misspelled
as daat.   2 infile emplist.dat' 3
input LastName 1-20 FirstName 21-30 4
JobTitle 36-43 Salary 54-59 5
run   NOTE The infile emplist.dat' is
File Nameemplist.dat',
RECFMV,LRECL256   NOTE 18 records were read
from the infile emplist.dat'. The minimum
record length was 59. The maximum record
length was 59. NOTE The data set WORK.STAFF has
18 observations and 4 variables.
Part 1 of 4
111
Demonstration Debugging a SAS Program
The SAS log contains error messages and warnings.
6 7 proc print datawork.staff 8 run
--- 22 - 200 ERROR 22-322
Syntax error, expecting one of the following ,
(, DATA, DOUBLE, HEADING, LABEL, N,
NOOBS, OBS, ROUND, ROWS, SPLIT,
STYLE, UNIFORM, WIDTH. ERROR 200-322 The symbol
is not recognized and will be ignored. 9   NOTE
The SAS System stopped processing this step
because of errors. NOTE PROCEDURE PRINT
used (Total process time) real time
0.06 seconds cpu time 0.06
seconds
Part 2 of 4
112
Demonstration Debugging a SAS Program
The SAS log contains error messages and warnings.
10 proc means datawork.staff average max
------- ---
22 202 ERROR 22-322
Syntax error, expecting one of the following ,
(, ALPHA, CHARTYPE, CLASSDATA, CLM,
COMPLETETYPES, CSS, CV, DATA,
DESCEND, DESCENDING, DESCENDTYPES,
EXCLNPWGT, EXCLNPWGTS, EXCLUSIVE,
FW, IDMIN, KURTOSIS, LCLM, MAX,
MAXDEC, MEAN, MEDIAN, MIN, MISSING, N, NDEC,
NMISS, NONOBS, NOPRINT, NOTHREADS,
NOTRAP, NWAY, ORDER, P1, P10, P25,
P5, P50, P75, P90, P95, P99,
PCTLDEF, PRINT, PRINTALL, PRINTALLTYPES,
PRINTIDS, PRINTIDVARS, PROBT, Q1,
Q3, QMARKERS, QMETHOD, QNTLDEF,
QRANGE, RANGE, SKEWNESS, STDDEV,
STDERR, SUM, SUMSIZE, SUMWGT, T, THREADS, UCLM,
USS, VAR, VARDEF. ERROR 202-322 The
option or parameter is not recognized and
will be ignored. 11 class
JobTitle 12 var Salary 13 run
Part 3 of 4
113
Demonstration Debugging a SAS Program
The SAS log contains error messages and warnings.
  NOTE The SAS System stopped processing this
step because of errors. NOTE PROCEDURE
MEANS used (Total process time) real time
0.05 seconds cpu time
0.05 seconds
Part 4 of 4
114
Demonstration Debugging a SAS Program
  • The log indicates that SAS
  • assumed that the keyword DATA was misspelled and
    executed the DATA step
  • interpreted the word RUN as an option in the PROC
    PRINT statement (because there was a missing
    semicolon), so PROC PRINT was not executed
  • did not recognize the word AVERAGE as a valid
    option in the PROC MEANS statement, so the PROC
    MEANS step was not executed.

115
Demonstration Debugging a SAS Program
  • If you use the Enhanced Editor, the program
    remains in the Editor window.
  • However, if you use the Editor window, the code
    disappears with each submit.
  • Use the RECALL command or select Run ? Recall
    Last Submit to recall the program that you
    submitted back to the Editor window. The original
    program is moved into the Editor window.

continued...
116
Demonstration Debugging a SAS Program
  • Edit the program.
  • Correct the spelling of DATA.
  • Put a semicolon at the end of the PROC PRINT
    statement.
  • Change the word AVERAGE to MEAN in the PROC MEANS
    statement.
  • Submit the program.
  • The program runs successfully without errors and
    generates output.

continued...
117
Demonstration Debugging a SAS Program
data work.staff infile 'emplist.dat'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means datawork.staff
mean max class JobTitle var Salary run
118
Demonstration Submitting Part of the Code
  • You do not always have to submit the entire
    program. You can submit part of a program. For
    example, perhaps you only want to submit the PROC
    PRINT step.
  • Highlight the part of your program that you want
    to run.
  • Submit the program by selecting Run ? Submit or
    clicking on the Submit button.
  • Only the highlighted code is executed.

119
Demonstration Submitting Part of the Code
  • What happens if you do not highlight the entire
    step?
  • Submit only the PROC PRINT statement.
  • What happens?
  • The Output window becomes active.
  • The log has no notes.
  • The PROC PRINT running message is displayed in
    the Editor window title bar (blue bar).

120
Demonstration Submitting Part of the Code
  • Why is SAS telling you that PROC PRINT is still
    running?
  • Did you get output in the Output window?
  • Are there any messages in the Log?

121
Demonstration Solution
  • How does SAS determine the end of the step?
  • Was the end of the step submitted to SAS?
  • No, SAS is still waiting. After SAS gets a step
    boundary, SAS can finish executing the step.

122
Demonstration Solution
  • Highlight the RUN statement and submit only that
    statement.
  • This enables you to see the output once. If you
    submit the entire procedure, you see the output
    twice.

123
Demonstration Debugging a SAS Program
  • Open the second program
  • c02s4d2.sas

124
Another Example
  • Do you notice anything odd with this program?

data work.staff infile 'emplist.dat input
LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means datawork.staff
mean max class JobTitle var Salary run
125
Using Quotation Marks in SAS
  • Rules for using quotation marks are as follows
  • You can use single or double quotation marks.
  • Quotation marks must be in pairs. " " or
    ' '
  • Quotation marks must match.
  • " '
  • Typically, it does not matter if you use double
    or single quotation marks, as long as they match.

126
Unbalanced Quotation Marks
  • The closing quotation marks for the INFILE
    statement is missing.

data work.staff infile 'c\sas\data\emplist.da
t input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc
print datawork.staff run proc means
datawork.staff average max class JobTitle
var Salary run
127
Unbalanced Quotation Marks
  • Submit the program and browse the SAS log.
  • There are no notes in the SAS log because all of
    the SAS statements after the INFILE statement
    became part of the quoted string.
  • The banner on the window indicates that the DATA
    step is still running because the RUN statement
    was not recognized.

128
Fixing Quotation Errors
  • Simply adding the needed quotation mark and
    submitting the code will not work.
  • Why?
  • ' The first two quotation marks are considered a
    matched pair.
  • ' ' By adding the new quotation mark, you
    still have an unmatched quotation mark.
  • SAS continues to look for a match.

129
Fixing Quotation Errors
  • Because you know that the problem is unmatched
    quotation marks, you want SAS to stop processing
    so that you can fix the error.
  • How?
  • There are two ways to fix the quotation marks
  • 1. Programmatically
  • 2. Point and click

130
Programmatically
  • You want SAS to stop processing because the code
    did not execute properly.
  • You will add some code to stop the SAS process
    and then delete that code.

131
Programmatically
  • What is SAS waiting for?
  • SAS expects a single quotation mark followed by a
    semicolon.
  • You need to end the step with the RUN statement.
  • ' run

132
Programmatically
  • ' run
  • What if you forgot to submit a RUN statement?
    You added an unbalanced quotation mark.
  • Make the first part a comment.
  • ' run

133
Programmatically
  • " run
  • What if you used a double quotation mark instead
    of single quotation mark in the program?
  • Does this solve the problem?

134
Programmatically
  • ' run
  • What if you used a double quotation mark instead
    of single quotation mark in the program? Does
    this solve that?
  • No, you need to account for both single and
    double quotation marks.
  • ' " run

135
Programmatically
  • Submit the following
  • ' " run
  • If the quotation mark counter within SAS has an
    uneven number of quotation marks, SAS reads the
    quotation in the comment above as the matching
    quotation mark.
  • SAS then has an even number of quotation marks in
    the quotation mark counter and runs successfully,
    assuming no other errors occur.
  • Either double or single quotation marks can be
    used.

136
Point-and-Click
  • To correct the problem, click the Break icon
    or press the CTRL and BREAK keys.
  • 2. Select Cancel Submitted Statements in the
    Tasking Manager window and select OK.

137
Point-and-Click
  • Select Y to cancel submitted statements ? OK.

138
Fixing the Program
  1. Select the Editor window to make it active.
  2. Add a closing quotation mark to the file
    reference in the INFILE statement.
  3. Resubmit the program.

139
Resubmitting the Program
27 data work.staff 28 infile
'raw-data-file' 29 input LastName 1-20
FirstName 21-30 30 JobTitle 36-43
Salary 54-59 31 run   NOTE 18 records were
read from the infile 'raw-data-file'. The
minimum record length was 59. The maximum
record length was 59. NOTE The data set
WORK.STAFF has 18 observations and 4
variables. 32 33 proc print datawork.staff 34
run   NOTE There were 18 observations read
from the dataset WORK.STAFF. 35 36 proc means
datawork.staff mean max 37 class
JobTitle 38 var Salary 39 run NOTE
There were 18 observations read from the dataset
WORK.STAFF.
140
Saving a SAS Program
  • There are two methods to save a SAS program
  • Menu-drivenSelect File ? Save. Select File ?
    Save As.
  • Toolbar Shortcut
  • Save button
  • ? If changes were made to the program since it
    was last saved, SAS will put an asterisk on
    the blue title bar of the Editor window. This
    indicates that you need to save the program.

141
What Happens If
  • you forgot to save a program and need to get
    the code back?
  • If you did not close the SAS session, you can
    recall your code.
  • When you submit your code, SAS puts the text into
    a buffer. Code is added to this buffer every time
    that you submit.

142
What If You Need to Get the Code Back?
  • This buffer is similar to a stack of plates in
    the cafeteria.
  • When you submit your code, SAS puts the text
    into a buffer.
  • Code is added to this buffer every time that you
    submit code.
  • The last code block that is added to the buffer
    is the first code block available to remove (as
    withthat stack of plates).

LIFO
Last In First Out
143
What If You Need to Get the Code Back?
  • Use one of these methods
  • Select Run ? Recall Last Submit.
  • Submit the RECALL command.
  • Repeat this until you see the desired code in
    your Editor window.
  • SAS puts the new entries at the top of the
    editor window.

144
Recall a Submitted Program
  • Program statements accumulate in a recall buffer
    each time that you issue a SUBMIT command.

daat work.staff infile 'raw-data-file'
input LastName 1-20 FirstName 21-30
JobTitle 36-43 Salary 54-59 run proc print
datawork.staff run proc means datawork.staff
average max class JobTitle var
Salary run data work.staff infile
'raw-data-file' input LastName 1-20
FirstName 21-30 JobTitle 36-43
Salary 54-59 run proc print datawork.staff
run proc means datawork.staff mean max
class Jobtitle var Salary run
Submit Number 1
Submit Number 2
145
Recall a Submitted Program
  • Issue the RECALL command (from the Run menu) once
    to recall the most recently submitted program.

Submit Number 1
Issue RECALL once.
Submit Number 2
Submit Number 2 statements are recalled.
146
Recall a Submitted Program
  • Issue the RECALL command again to recall Submit
    Number 1 statements.

Submit Number 1
Issue RECALL again.
Submit Number 2
147
Exercise Section 2.4
  • This exercise reinforces the concepts discussed
    previously.
Write a Comment
User Comments (0)
About PowerShow.com