Hechsp 001 Chapter 2

147
Chapter 2 Getting Started with the SAS ® System

description

SAS for HS Chapter 2

Transcript of Hechsp 001 Chapter 2

Page 1: Hechsp 001 Chapter 2

Chapter 2

Getting Started with the SAS® System

Page 2: Hechsp 001 Chapter 2

Section 2.1

Introduction to SAS Programs

Page 3: Hechsp 001 Chapter 2

3 3

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

Objectives

Page 4: Hechsp 001 Chapter 2

4 4

SAS Vocabulary SAS program DATA step PROC step

Page 5: Hechsp 001 Chapter 2

5 5

Turning Data into information

DATAStep

DATAStep

Data

OutputOutput

SAS Data Set

PROCStep

LastName FirstName Age

TORRES JAN 23LANGKAMM SARAH 46SMITH MICHAEL 71WAGSCHAL NADJA 37TOERMOEN JOCHEN 16

Page 6: Hechsp 001 Chapter 2

6 6

DATAStep

DATAStep

OutputOutput

SAS ProgramsA SAS program is a sequence of steps that the user submits for execution.

Raw Data

SAS Data Set

SAS Data Set

129986542,Fred,Barnes,54,654543681,Alicia,Burnett,23,241453384,Connor,Coats

PROCStep

LastName FirstName Age

TORRES JAN 23LANGKAMM SARAH 46SMITH MICHAEL 71WAGSCHAL NADJA 37TOERMOEN JOCHEN 16

LastName FirstName Age

TORRES JAN 23LANGKAMM SARAH 46SMITH MICHAEL 71WAGSCHAL NADJA 37TOERMOEN JOCHEN 16

DATA steps are typically used to create SAS data sets.

PROC (procedure) steps are used to process SAS data sets (generate reports and sort data).

Page 7: Hechsp 001 Chapter 2

7 7

data work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff;run;

proc means data=work.staff; class JobTitle; var Salary;run;

DATADATAStep

PROCSteps

SAS Programs

Page 8: Hechsp 001 Chapter 2

8 8

SAS ProgramsSAS 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.

Page 9: Hechsp 001 Chapter 2

9 9

Step BoundariesSAS 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 data=work.staff;run;

proc means data=work.staff; class JobTitle; var Salary;run;

DATAStep

PROCSteps

Page 10: Hechsp 001 Chapter 2

1010

data work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff;

proc means data=work.staff; class JobTitle; var Salary;run;

Step Boundaries

Page 11: Hechsp 001 Chapter 2

1111

Running a SAS ProgramVersions 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

Page 12: Hechsp 001 Chapter 2

1212

SAS Windowing EnvironmentInteractive windows enable you to interface with SAS.

SAS Windowing Environment Interface

Page 13: Hechsp 001 Chapter 2

Section 2.2

Running SAS Programs

Page 14: Hechsp 001 Chapter 2

1414

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

Objectives

Page 15: Hechsp 001 Chapter 2

1515

SAS VocabularySAS Output CLEAR LOG

INCLUDE OUTPUT

Editor window WED

SUBMIT Enhanced Editor

SAS log Program Editor

LOG KEYS

Page 16: Hechsp 001 Chapter 2

1616

When you execute a SAS program, the output generated by SAS is divided into two major parts:

Submitting a SAS Program

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.

Page 17: Hechsp 001 Chapter 2

1717

SAS Log1 data work.staff;2 infile 'raw-data-file';3 input LastName $ 1-20 FirstName $ 21-304 JobTitle $ 36-43 Salary 54-59;5 run;NOTE: The infile 'raw-data-file' is: File Name= 'raw-data-file', RECFM=V,LRECL=256NOTE: 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 data=work.staff;7 run;NOTE: There were 18 observations read from the dataset WORK.STAFF.

8 proc means data=work.staff;9 class JobTitle;10 var Salary;11 run;NOTE: There were 18 observations read from the dataset WORK.STAFF.

Page 18: Hechsp 001 Chapter 2

1818

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

PRINT Procedure Output

Page 19: Hechsp 001 Chapter 2

1919

The SAS System

The MEANS Procedure

Analysis Variable : Salary

NJobTitle 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 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

MEANS Procedure Output

Page 20: Hechsp 001 Chapter 2

2020

This demonstration illustrates how to invoke SAS and open and run a SAS program.

Running a SAS Program

Page 21: Hechsp 001 Chapter 2

2121

There are two ways to open SAS:

1. Menu-driven

Select Start Programs SAS SAS 9.1.

2. Desktop shortcut

SAS toolbar button

Demonstration – Invoking SAS

Page 22: Hechsp 001 Chapter 2

2222

Command Box

Output Window (behind) Current Working Directory

Menus Toolbar

ExplorerLog Window

Enhanced Editor

Page 23: Hechsp 001 Chapter 2

2323

There are three ways to open a SAS program:

1. Command Box

Type the INCLUDE command in the Command Box.

2. Menu-driven

Select File Open.

3. Toolbar Shortcut

Open button

Demonstration – Running a SAS Program

Page 24: Hechsp 001 Chapter 2

2424

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.

Demonstration – Running a SAS Program

Page 25: Hechsp 001 Chapter 2

2525

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

Demonstration – Running a SAS Program

Page 26: Hechsp 001 Chapter 2

2626

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

Demonstration – Running a SAS Program

Page 27: Hechsp 001 Chapter 2

2727

There are three ways to submit a SAS program:

1. Menu-driven

Select File Submit.

2. Command Box

Type the SUBMIT command in the Command Box.

3. Toolbar Shortcut

Submit button

Demonstration – Running a SAS Program

Page 28: Hechsp 001 Chapter 2

2828

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.

Demonstration – Running a SAS Program

Page 29: Hechsp 001 Chapter 2

2929

Always check the SAS log when you submit a program.

Browse the messages that the program generated.

Demonstration – Running a SAS Program

Remember

Page 30: Hechsp 001 Chapter 2

3030

There are three ways to view the SAS log:

1. Menu-driven

Select View Log.

2. Command Box

Type the LOG command in the Command Box.

3. Tabs

Select the Log tab.

Demonstration – Running a SAS Program

Page 31: Hechsp 001 Chapter 2

3131

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.

Demonstration – Running a SAS Program

Page 32: Hechsp 001 Chapter 2

3232

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.

Demonstration – Running a SAS Program

Page 33: Hechsp 001 Chapter 2

3333

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

Demonstration – Running a SAS Program

Page 34: Hechsp 001 Chapter 2

3434

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.

Demonstration – Running a SAS Program

Page 35: Hechsp 001 Chapter 2

3535

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, andyou want a blank Log window.

Demonstration – Running a SAS Program

Page 36: Hechsp 001 Chapter 2

3636

There are three ways to view the Output window:

1. Menu-driven

Select View Output.

2. Command Box

Type the OUTPUT command in the Command Box.

3. Select the Output tab.

Demonstration – Running a SAS Program

Page 37: Hechsp 001 Chapter 2

3737

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.

Demonstration – Running a SAS Program

Page 38: Hechsp 001 Chapter 2

3838

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.

Demonstration – Running a SAS Program

Page 39: Hechsp 001 Chapter 2

3939

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

Demonstration – Running a SAS Program

Page 40: Hechsp 001 Chapter 2

4040

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.

Demonstration – Running a SAS Program

Page 41: Hechsp 001 Chapter 2

4141

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.

Page 42: Hechsp 001 Chapter 2

4242

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.

Page 43: Hechsp 001 Chapter 2

4343

Program Editor versus Enhanced EditorProgram Editor Enhanced EditorColoring 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

Page 44: Hechsp 001 Chapter 2

4444

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?

Page 45: Hechsp 001 Chapter 2

4545

What Do You Do If…… you lost your previously submitted code?

Select Run Recall Last Submit.

The code is returned to your Editor window.

Page 46: Hechsp 001 Chapter 2

4646

This exercise reinforces the concepts discussed previously.

Exercise

Page 47: Hechsp 001 Chapter 2

4747

1. Submitting a Program

a. With the Editor window active, open the SAS program c02ex1.sas

b. Submit the program. – Based on the report in the Output window, how

many observations and variables are in the data set?

c. Examine the Log window. – Based on the log notes, how many observations

and variables are in the data set?

d. Clear the Log and Output windows.

Exercises

Page 48: Hechsp 001 Chapter 2

4848

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.

a. Issue the KEYS command. Browse the contents of the window by scrolling vertically.

b. Close the KEYS window.

Exercises

Page 49: Hechsp 001 Chapter 2

4949

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.

Exercises

Page 50: Hechsp 001 Chapter 2

5050

3. Setting a Function Keya. Issue the KEYS command to access the Keys

window.b. Click in the definition area next to the F12 key.c. Type the commands that you want to assign to the

key, and separate them with a semicolon.

Clear Log; Clear Outputd. 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.

e. Press the F12 key. What happens?

Exercises

Page 51: Hechsp 001 Chapter 2

5151

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.

Exercises

Page 52: Hechsp 001 Chapter 2

Section 2.3

Mastering Fundamental Concepts

Page 53: Hechsp 001 Chapter 2

5353

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.

Objectives

Page 54: Hechsp 001 Chapter 2

5454

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

Page 55: Hechsp 001 Chapter 2

5555

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.

Mastering Fundamental Concepts

Page 56: Hechsp 001 Chapter 2

5656

SAS documentation and text in the SAS windowing environment use the following terms interchangeably:

SAS Data SetSAS Data Set TableTable

VariableVariable ColumnColumn

ObservationObservation RowRow

SAS Data Set Terminology

Page 57: Hechsp 001 Chapter 2

5757

SAS Data Sets

SAS Data Set

Descriptor Portion

Data Portion

A SAS data set consists of two parts.

Page 58: Hechsp 001 Chapter 2

5858

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.

Page 59: Hechsp 001 Chapter 2

5959

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

SAS Data Sets

Page 60: Hechsp 001 Chapter 2

6060

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.

Browsing the Descriptor Portion

Page 61: Hechsp 001 Chapter 2

6161

General form of the CONTENTS procedure:

Example:

PROC CONTENTS DATA=SAS-data-set;RUN;

PROC CONTENTS DATA=SAS-data-set;RUN;

proc contents data=sashelp.class;run;

Browsing the Descriptor Portion

Page 62: Hechsp 001 Chapter 2

6262

The SAS System

The CONTENTS Procedure

Data Set Name SASHELP.CLASS Observations 19Member Type DATA Variables 5Engine V9 Indexes 0Created Wednesday, May 12, 2004 Observation Length 40 10:53:55 PMLast Modified Wednesday, May 12, 2004 Deleted Observations 0

10:53:55 PMProtection Compressed NOData 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

Partial PROC CONTENTS Output

Page 63: Hechsp 001 Chapter 2

6363

Numeric values

Variable

names

Variable

values

LastName FirstName JobTitle Salary

TORRES JAN Pilot 50000LANGKAMM SARAH Mechanic 80000SMITH MICHAEL Mechanic 40000WAGSCHAL NADJA Pilot 77500TOERMOEN JOCHEN Pilot 65000

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.

Character values

SAS Data Sets: Data Portion

Page 64: Hechsp 001 Chapter 2

6464

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.

SAS Variable Values

continued...

Page 65: Hechsp 001 Chapter 2

6565

SAS Variable ValuesNumeric 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.

Page 66: Hechsp 001 Chapter 2

6666

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.

SAS Data Set and Variable Names

Page 67: Hechsp 001 Chapter 2

6767

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

FiveMonthsData

...

Page 68: Hechsp 001 Chapter 2

6868

data5mon

Select the valid default SAS names.

data5mon

Valid SAS Names

...

Page 69: Hechsp 001 Chapter 2

6969

Select the valid default SAS names.

Valid SAS Names

data5mon

...

Page 70: Hechsp 001 Chapter 2

7070

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

...

Page 71: Hechsp 001 Chapter 2

7171

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

...

Page 72: Hechsp 001 Chapter 2

7272

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

...

Page 73: Hechsp 001 Chapter 2

7373

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

...

Page 74: Hechsp 001 Chapter 2

7474

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

five months data

...

Page 75: Hechsp 001 Chapter 2

7575

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

five months data

...

Page 76: Hechsp 001 Chapter 2

7676

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

...

Page 77: Hechsp 001 Chapter 2

7777

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

...

Page 78: Hechsp 001 Chapter 2

7878

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

FiveMonthsData

...

Page 79: Hechsp 001 Chapter 2

7979

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

FiveMonthsData

Page 80: Hechsp 001 Chapter 2

8080

This exercise reinforces the concepts discussed previously.

Exercise

Page 81: Hechsp 001 Chapter 2

8181

a. Create your own SAS name.

b. Create a data set name for containing the names and birth dates of your family members for three generations.

c. Create a variable name for the column containing the name of the generation to which an individual belonged.

Exercises

Page 82: Hechsp 001 Chapter 2

8282

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?

Dates in SAS

Page 83: Hechsp 001 Chapter 2

8383

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.

SAS Dates

Page 84: Hechsp 001 Chapter 2

8484

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.

01JAN1959 01JAN1960 01JAN1961

store

-365 0 366

display

01/01/1959 01/01/1960 01/01/1961

SAS Date Values

Page 85: Hechsp 001 Chapter 2

8585

LastName FirstName JobTitle Salary

TORRES JAN Pilot 50000LANGKAMM SARAH Mechanic 80000SMITH MICHAEL Mechanic . WAGSCHAL NADJA Pilot 77500TOERMOEN JOCHEN 65000

A value must exist for every variable for each observation.

Missing values are valid values.

A numericmissing valueis displayedas a period.

A character missing value is displayed as a blank.

Missing Data Values

Page 86: Hechsp 001 Chapter 2

8686

Browsing the Data PortionThe 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

Page 87: Hechsp 001 Chapter 2

8787

General form of the PRINT procedure:

Example:

PROC PRINT DATA=SAS-data-set; RUN;

PROC PRINT DATA=SAS-data-set; RUN;

proc print data=work.staff;run;

Browsing the Data Portion

PROC is the keyword.PRINT is the procedure name.

Page 88: Hechsp 001 Chapter 2

8888

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

PROC PRINT Output

Page 89: Hechsp 001 Chapter 2

8989

data work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff;run;

proc means data=work.staff; class JobTitle; var Salary;run;

SAS Syntax RulesSAS statements usually begin with an identifying keyword always end with a semicolon.

continued...

Page 90: Hechsp 001 Chapter 2

9090

data work.staff; infile 'raw-data-file';input LastName $ 1-20 FirstName $ 21-30JobTitle $ 36-43 Salary 54-59;run; proc means data=work.staff; class JobTitle; var Salary;run;

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

Page 91: Hechsp 001 Chapter 2

9191

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-30JobTitle $ 36-43 Salary 54-59;run; proc means data=work.staff; class JobTitle; var Salary;run;

SAS Syntax Rules

Page 92: Hechsp 001 Chapter 2

9292

data work.staff; infile 'raw-data-file';input LastName $ 1-20 FirstName $ 21-30JobTitle $ 36-43 Salary 54-59;run; proc means data=work.staff; class JobTitle; var Salary;run;

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

Page 93: Hechsp 001 Chapter 2

9393

data work.staff; infile 'raw-data-file';input LastName $ 1-20 FirstName $ 21-30JobTitle $ 36-43 Salary 54-59;run; proc means data=work.staff; class JobTitle; var Salary;run;

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

Page 94: Hechsp 001 Chapter 2

9494

data work.staff; infile 'raw-data-file';input LastName $ 1-20 FirstName $ 21-30JobTitle $ 36-43 Salary 54-59;run; proc means data=work.staff; class JobTitle; var Salary;run;

...

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

Page 95: Hechsp 001 Chapter 2

9595

data work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff;run;

proc means data=work.staff; class JobTitle; var Salary;run;

SAS Syntax RulesIndentation and good spacing makes the program easier to read.

Conventional Spacing Example

Page 96: Hechsp 001 Chapter 2

9696

/* 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 data=work.staff;run;

SAS CommentsYou can comment a block of text or code. Type /* to begin a comment. Type your comment text. Type */ to end the comment.

Page 97: Hechsp 001 Chapter 2

9797

SAS Comments

data work.staff; infile 'raw-data-file'; *input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

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 oneline. How does SAS recognize the end of astatement?

Page 98: Hechsp 001 Chapter 2

9898

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.

Page 99: Hechsp 001 Chapter 2

9999

This exercise reinforces the concepts discussed previously.

Exercise – Section 2.3

Page 100: Hechsp 001 Chapter 2

Section 2.4

Diagnosing and Correcting Syntax Errors

Page 101: Hechsp 001 Chapter 2

101101

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

Objectives

Page 102: Hechsp 001 Chapter 2

102102

SAS Vocabulary Syntax Errors Options RECALL

Page 103: Hechsp 001 Chapter 2

103103

daat work.staff; infile ‘emplist.dat’; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff run;

proc means data=work.staff average max; class JobTitle; var Salary;run;

Syntax Errors

Average is not a valid option.

Syntax errors include the following: misspelled keywords missing or invalid punctuation invalid options

Page 104: Hechsp 001 Chapter 2

104104

proc means data=work.staff average max; class JobTitle; var Salary;run;

Syntax ErrorsProcedure 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.

Page 105: Hechsp 001 Chapter 2

105105

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

Page 106: Hechsp 001 Chapter 2

106106

This demonstration illustrates these tasks: how to submit a SAS program that contains errors diagnosing the errors correcting the errors saving the corrected program

Demonstration – Debugging a SAS Program

Page 107: Hechsp 001 Chapter 2

107107

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

Demonstration – Debugging a SAS Program

Page 108: Hechsp 001 Chapter 2

108108

Start with the program:

c02s4d1.sas

Demonstration – Debugging a SAS Program

Page 109: Hechsp 001 Chapter 2

109109

Submit a SAS Program with Errors daat work.staff; infile 'emplist.dat'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staffrun;

proc means data=work.staff average max; class JobTitle; var Salary;run;

Demonstration – Debugging a SAS Program

Page 110: Hechsp 001 Chapter 2

110110

The SAS log contains error messages and warnings. 1 daat work.staff; ---- 14WARNING 14-169: Assuming the symbol DATA was misspelled as daat. 2 infile ‘…emplist.dat';3 input LastName $ 1-20 FirstName $ 21-304 JobTitle $ 36-43 Salary 54-59;5 run; NOTE: The infile ‘…emplist.dat' is: File Name=‘…emplist.dat', RECFM=V,LRECL=256 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

Demonstration – Debugging a SAS Program

Page 111: Hechsp 001 Chapter 2

111111

The SAS log contains error messages and warnings. 67 proc print data=work.staff8 run; --- 22 - 200ERROR 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

Demonstration – Debugging a SAS Program

Page 112: Hechsp 001 Chapter 2

112112

The SAS log contains error messages and warnings. 10 proc means data=work.staff average max; ------- --- 22 202ERROR 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

Demonstration – Debugging a SAS Program

Page 113: Hechsp 001 Chapter 2

113113

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

Demonstration – Debugging a SAS Program

Page 114: Hechsp 001 Chapter 2

114114

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.

Demonstration – Debugging a SAS Program

Page 115: Hechsp 001 Chapter 2

115115

1. 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...

Demonstration – Debugging a SAS Program

Page 116: Hechsp 001 Chapter 2

116116

2. 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.

3. Submit the program.

The program runs successfully without errors and generates output.

continued...

Demonstration – Debugging a SAS Program

Page 117: Hechsp 001 Chapter 2

117117

data work.staff; infile 'emplist.dat'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff;run;

proc means data=work.staff mean max; class JobTitle; var Salary;run;

Demonstration – Debugging a SAS Program

Page 118: Hechsp 001 Chapter 2

118118

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.

Demonstration – Submitting Part of the Code

Page 119: Hechsp 001 Chapter 2

119119

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).

Demonstration – Submitting Part of the Code

Page 120: Hechsp 001 Chapter 2

120120

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?

Demonstration – Submitting Part of the Code

Page 121: Hechsp 001 Chapter 2

121121

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.

Demonstration – Solution

Page 122: Hechsp 001 Chapter 2

122122

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.

Demonstration – Solution

Page 123: Hechsp 001 Chapter 2

123123

Open the second program:

c02s4d2.sas

Demonstration – Debugging a SAS Program

Page 124: Hechsp 001 Chapter 2

124124

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 data=work.staff;run;

proc means data=work.staff mean max; class JobTitle; var Salary;run;

Page 125: Hechsp 001 Chapter 2

125125

Using Quotation Marks in SASRules 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.

=

Page 126: Hechsp 001 Chapter 2

126126

data work.staff; infile 'c:\sas\data\emplist.dat; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;

proc print data=work.staff run;

proc means data=work.staff average max; class JobTitle; var Salary;run;

Unbalanced Quotation MarksThe closing quotation marks for the INFILE statement is missing.

Page 127: Hechsp 001 Chapter 2

127127

Unbalanced Quotation MarksSubmit 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.

Page 128: Hechsp 001 Chapter 2

128128

Fixing Quotation ErrorsSimply 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.

Page 129: Hechsp 001 Chapter 2

129129

Fixing Quotation ErrorsBecause 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

Page 130: Hechsp 001 Chapter 2

130130

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.

Page 131: Hechsp 001 Chapter 2

131131

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;

Page 132: Hechsp 001 Chapter 2

132132

Programmatically

'; run;

What if you forgot to submit a RUN statement? You added an unbalanced quotation mark.

Make the first part a comment.

*'; run;

Page 133: Hechsp 001 Chapter 2

133133

Programmatically

*"; run;

What if you used a double quotation mark instead of single quotation mark in the program?

Does this solve the problem?

Page 134: Hechsp 001 Chapter 2

134134

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;

Page 135: Hechsp 001 Chapter 2

135135

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.

Page 136: Hechsp 001 Chapter 2

136136

1. 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.

Point-and-Click

Page 137: Hechsp 001 Chapter 2

137137

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

Page 138: Hechsp 001 Chapter 2

138138

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.

Page 139: Hechsp 001 Chapter 2

139139

27 data work.staff;28 infile 'raw-data-file' ;29 input LastName $ 1-20 FirstName $ 21-3030 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.3233 proc print data=work.staff;34 run; NOTE: There were 18 observations read from the dataset WORK.STAFF.3536 proc means data=work.staff mean max;37 class JobTitle;38 var Salary;39 run;NOTE: There were 18 observations read from the dataset WORK.STAFF.

Resubmitting the Program

Page 140: Hechsp 001 Chapter 2

140140

Saving a SAS ProgramThere are two methods to save a SAS program:

1.Menu-drivenSelect File Save. Select File Save As.

2.Toolbar Shortcut

Save button

If changes were made to the program since it waslast saved, SAS will put an asterisk * on the blue

titlebar of the Editor window. This indicates that youneed to save the program.

Page 141: Hechsp 001 Chapter 2

141141

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.

Page 142: Hechsp 001 Chapter 2

142142

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).

Last In First Out

Page 143: Hechsp 001 Chapter 2

143143

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 thetop of the editor window.

Remember

Page 144: Hechsp 001 Chapter 2

144144

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 data=work.staff run;proc means data=work.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 data=work.staff; run;proc means data=work.staff mean max; class Jobtitle; var Salary;run;

Submit Number 1

Submit Number 2

Recall a Submitted Program

Page 145: Hechsp 001 Chapter 2

145145

Issue the RECALL command (from the Run menu) once to recall the most recently submitted program.

Submit Number 1

Submit Number 2

Issue RECALL once.

Submit Number 2 statements are recalled.

Recall a Submitted Program

data work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;proc print data=work.staff; run;proc means data=work.staff mean max; class JobTitle; var Salary;run;

Page 146: Hechsp 001 Chapter 2

146146

Issue the RECALL command again to recall Submit Number 1 statements.

Submit Number 1

Submit Number 2

Issue RECALL again.

Recall a Submitted Program

daat work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ 21-30 JobTitle $ 36-43 Salary 54-59;run;proc print data=work.staff run;proc means data=work.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 data=work.staff; run;proc means data=work.staff mean max; class JobTitle; var Salary;run;

Page 147: Hechsp 001 Chapter 2

147147

This exercise reinforces the concepts discussed previously.

Exercise – Section 2.4