hechsp 001 chapter 2

Post on 04-Nov-2014

16 Views

Category:

Education

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

SAS for HS Chapter 2

TRANSCRIPT

Chapter 2

Getting Started with the SAS® System

Section 2.1

Introduction to SAS Programs

3 3

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

Objectives

4 4

SAS Vocabulary SAS program DATA step PROC step

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

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

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

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.

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

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

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

1212

SAS Windowing EnvironmentInteractive windows enable you to interface with SAS.

SAS Windowing Environment Interface

Section 2.2

Running SAS Programs

1414

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

Objectives

1515

SAS VocabularySAS Output CLEAR LOG

INCLUDE OUTPUT

Editor window WED

SUBMIT Enhanced Editor

SAS log Program Editor

LOG KEYS

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.

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.

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

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

2020

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

Running a SAS Program

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

2222

Command Box

Output Window (behind) Current Working Directory

Menus Toolbar

ExplorerLog Window

Enhanced Editor

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

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

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

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

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

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

2929

Always check the SAS log when you submit a program.

Browse the messages that the program generated.

Demonstration – Running a SAS Program

Remember

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

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

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

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

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

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

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

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

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

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

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

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.

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.

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

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?

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.

4646

This exercise reinforces the concepts discussed previously.

Exercise

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

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

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

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

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

Section 2.3

Mastering Fundamental Concepts

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

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

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

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

5757

SAS Data Sets

SAS Data Set

Descriptor Portion

Data Portion

A SAS data set consists of two parts.

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.

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

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

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

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

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

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

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.

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

6767

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

FiveMonthsData

...

6868

data5mon

Select the valid default SAS names.

data5mon

Valid SAS Names

...

6969

Select the valid default SAS names.

Valid SAS Names

data5mon

...

7070

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

...

7171

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

...

7272

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

...

7373

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

...

7474

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

five months data

...

7575

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

Valid SAS Names

data#5

five months data

...

7676

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

...

7777

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

...

7878

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

FiveMonthsData

...

7979

data5mon

Select the valid default SAS names.

data5mon

5monthsdata

five months data

data#5

Valid SAS Names

fivemonthsdata

FiveMonthsData

8080

This exercise reinforces the concepts discussed previously.

Exercise

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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.

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?

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.

9999

This exercise reinforces the concepts discussed previously.

Exercise – Section 2.3

Section 2.4

Diagnosing and Correcting Syntax Errors

101101

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

Objectives

102102

SAS Vocabulary Syntax Errors Options RECALL

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

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.

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

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

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

108108

Start with the program:

c02s4d1.sas

Demonstration – Debugging a SAS Program

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

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

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

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

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

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

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

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

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

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

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

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

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

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

123123

Open the second program:

c02s4d2.sas

Demonstration – Debugging a SAS Program

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;

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.

=

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.

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.

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.

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

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.

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;

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;

133133

Programmatically

*"; run;

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

Does this solve the problem?

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;

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.

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

137137

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

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.

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

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.

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.

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

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

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

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;

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;

147147

This exercise reinforces the concepts discussed previously.

Exercise – Section 2.4

top related