Chapter 2 Getting Started with SAS Section 2

  • Slides: 146
Download presentation
Chapter 2 Getting Started with SAS®

Chapter 2 Getting Started with SAS®

Section 2. 1 Introduction to SAS Programs

Section 2. 1 Introduction to SAS Programs

Objectives n n 3 State the components of a SAS program. State the modes

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

SAS Vocabulary n n n 4 SAS program DATA step PROC step

SAS Vocabulary n n n 4 SAS program DATA step PROC step

Turning Data into Information SAS Data Set Data DATA Step Last. Name First. Name

Turning Data into Information SAS Data Set Data DATA Step Last. Name First. Name Age TORRES LANGKAMM SMITH WAGSCHAL TOERMOEN JAN 23 SARAH 46 MICHAEL NADJA 37 JOCHEN 71 PROC Step 5 16 Output

SAS Programs A SAS program is a sequence of steps that the user submits

SAS Programs A SAS program is a sequence of steps that the user submits for execution. DATA steps are typically used to create SAS data sets. Raw Data 129986542, F red, Barnes, 54, 65454368 1, Alicia, Bu rnett, 23, 24 1453384, Con nor, Coats SAS Data Set DATA Step SAS Data Set 6 Last. Name First. Name Age TORRES LANGKAMM SMITH WAGSCHAL TOERMOEN JAN SARAH MICHAEL NADJA JOCHEN 23 46 71 37 16 PROC Step Output PROC (procedure) steps are used to process SAS data sets (generate reports and sort data).

SAS Programs data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First.

SAS Programs data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; DATA Step proc print data=work. staff; run; proc means data=work. staff; class Job. Title; var Salary; run; 7 PROC Steps

SAS Programs SAS executes one step at a time, statement by statement, independently of

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 in its entirety. 8

Step Boundaries SAS steps begin with DATA statements or PROC statements. SAS detects the

Step Boundaries SAS steps begin with DATA statements or PROC statements. SAS detects the end of a step when it encounters n a RUN statement (for most steps) n a QUIT statement (for some run-group procedures) n the beginning of another step (DATA or PROC statement). data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff; class Job. Title; var Salary; run; 9 DATA Step PROC Steps

Step Boundaries data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First.

Step Boundaries data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; proc means data=work. staff; class Job. Title; var Salary; run; 10

Running a SAS Program Versions of SAS exist for several different operating systems. Windows

Running a SAS Program Versions of SAS exist for several different operating systems. Windows n SAS Windowing Environment n SAS Enterprise Guide (point-and-click interface) UNIX n SAS Windowing Environment only Mainframe n SAS Windowing Environment only 11

SAS Windowing Environment Interactive windows enable you to interface with SAS Windowing Environment Interface

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

Section 2. 2 Running SAS Programs

Section 2. 2 Running SAS Programs

Objectives n n n 14 Start SAS and include a SAS program in your

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

SAS Vocabulary 15 SAS Output CLEAR LOG INCLUDE OUTPUT Editor window WED SUBMIT Enhanced

SAS Vocabulary 15 SAS Output CLEAR LOG INCLUDE OUTPUT Editor window WED SUBMIT Enhanced Editor SAS log Program Editor LOG KEYS

Submitting a SAS Program When you execute a SAS program, the output generated by

Submitting a SAS Program When you execute a SAS program, the output generated by SAS is divided into two major parts: 16 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.

SAS Log 1 data work. staff; 2 infile 'raw-data-file'; 3 input Last. Name $

SAS Log 1 data work. staff; 2 infile 'raw-data-file'; 3 input Last. Name $ 1 -20 First. Name $ 21 -30 4 Job. Title $ 36 -43 Salary 54 -59; 5 run; NOTE: The infile 'raw-data-file' is: File Name= 'raw-data-file', RECFM=V, LRECL=256 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 data=work. staff; 7 run; NOTE: There were 18 observations read from the dataset WORK. STAFF. 8 proc means data=work. staff; 9 class Job. Title; 10 var Salary; 11 run; NOTE: There were 18 observations read from the dataset WORK. STAFF. 17

PRINT Procedure Output The SAS System First Obs Last. Name Job. Title Salary 1

PRINT Procedure Output The SAS System First Obs Last. Name Job. Title 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 18

MEANS Procedure Output The SAS System The MEANS Procedure Analysis Variable : Salary N

MEANS Procedure Output The SAS System The MEANS Procedure Analysis Variable : Salary N Job. Title Obs N Mean Std Dev Minimum ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Mechanic 8 8 58750. 00 19151. 65 36000. 00 Pilot 10 73750. 00 22523. 14 45000. 00 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Analysis Variable : Salary N Job. Title Obs Maximum ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Mechanic 8 80000. 00 Pilot 10 105000. 00 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 19

Running a SAS Program This demonstration illustrates how to invoke SAS and open and

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

Demonstration – Invoking SAS There are two ways to open SAS: 1. Menu-driven Select

Demonstration – Invoking SAS There are two ways to open SAS: 1. Menu-driven Select Start Programs SAS 9. 1. 2. Desktop shortcut Select the SAS toolbar button. 21

Command Box Explorer Menus Toolbar Log Window SAS Editor Output Window (behind) 22 Current

Command Box Explorer Menus Toolbar Log Window SAS Editor Output Window (behind) 22 Current Working Directory

Demonstration – Running a SAS Program There are three ways to open a SAS

Demonstration – Running a SAS Program 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 Select the Open button. 23

Demonstration – Running a SAS Program Issue the INCLUDE command in the command box

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

Demonstration – Running a SAS Program You can use the SAS Enhanced Editor to

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

Demonstration – Running a SAS Program In the SAS Enhanced Editor, the syntax in

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

Demonstration – Running a SAS Program There are three ways to submit a SAS

Demonstration – Running a SAS Program 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 Use the Submit button. 27

Demonstration – Running a SAS Program If your code generates output, the output is

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

Demonstration – Running a SAS Program There are three ways to view the SAS

Demonstration – Running a SAS Program 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. 29

Demonstration – Running a SAS Program Examining Your Program Results The Log window has

Demonstration – Running a SAS Program Examining Your Program Results The Log window has the following characteristics: n is one of the primary windows and is open by default n acts as an audit trail for your SAS session n 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. 30

Demonstration – Running a SAS Program Navigating in the Log n Use the PAGE

Demonstration – Running a SAS Program Navigating in the Log n Use the PAGE UP and PAGE DOWN keys. n 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. 31

Demonstration – Running a SAS Program The Log window contains the programming statements that

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

Demonstration – Running a SAS Program By default, the following conventions appear: n Notes

Demonstration – Running a SAS Program By default, the following conventions appear: n Notes are written in blue. – Notes inform you of what happened during processing. n Warnings are written in green. – Warnings indicate that a process ran but might have resulted in something other than what you intended. n Errors are written in red. – Read these carefully. – Errors stop SAS processing and indicate that something must be corrected. 33

Demonstration – Running a SAS Program You might want a clean log when you

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: n Issue the CLEAR LOG command in the command box. n n 34 Select Edit Clear All. Select the New icon.

Demonstration – Running a SAS Program There are three ways to view the Output

Demonstration – Running a SAS Program 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. 35

Demonstration – Running a SAS Program Examining Your Program Results The Output window n

Demonstration – Running a SAS Program Examining Your Program Results The Output window n is one of the primary windows and is open by default n becomes the active window each time that it receives output n 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. 36

Demonstration – Running a SAS Program Examining Your Program Results The Output window displays

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

Demonstration – Running a SAS Program You might want a clean Output window when

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: n Issue the CLEAR OUTPUT command in the command box. n Select Edit Clear All to clear the contents of the window. n Click on the New button. 38

Demonstration – Running a SAS Program To return to the Enhanced Editor window, use

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

What Do You Do If… … you close a window accidentally? n n 40

What Do You Do If… … you close a window accidentally? n n 40 Select View and the window that you want to open (Log, Editor, Output), for example, View Log. If you close the Log or Output window, you need to reopen those windows in order to see results. SAS does not automatically reopen the Log or Output windows after SAS executes the code. If you close the Editor window, you need to open the Enhanced Editor window. If you close the Explorer window, select View Contents Only.

The Editor Window Why are there two different types of editors? On the UNIX

The Editor Window Why are there two different types of editors? On the UNIX and Mainframe platforms, you can use only 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. 41

Program Editor versus Enhanced Editor Program Editor Enhanced Editor Coloring is different. Title bar:

Program Editor versus Enhanced Editor Program Editor Enhanced Editor Coloring is different. Title bar: Program Editor Title bar: Editor Has a blue + added to the icons Can have only one window Can have many windows open Code not kept after Keeps code after execution 42

What Do You Do If… … your previously submitted code is not in your

What Do You Do If… … your previously submitted code is not in your Editor? Select Run Recall Last Submit. The code is returned to your Editor window. 43

The KEYS Window The KEYS window has the following characteristics: n is a secondary

The KEYS Window The KEYS window has the following characteristics: n is a secondary window. n can be used to browse, add, or change function key definitions. n allows more than one command to a function key. Separate the commands with a semicolon. n is closed by issuing the END command or selecting. 44

The KEYS Window Setting a Function Key n Any of the commands that can

The KEYS Window Setting a Function Key n Any of the commands that can be issued in the command box can be assigned to a function key. n Notice that SUBMIT is a command, and is not part of the program syntax. n Commands are typed in the command box. n However, RUN is a statement inside a program. You cannot use it in the command box, nor as a function or hot key. 45

Exercises This exercise reinforces the concepts discussed previously. n n 46 Appendix A Exercises

Exercises This exercise reinforces the concepts discussed previously. n n 46 Appendix A Exercises 1 and 2

Exercises 1. Submitting a Program a. With the Editor window active, open the SAS

Exercises 1. Submitting a Program a. With the Editor window active, open the SAS program c 02 ex 1. sas in the Programs folder. 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. 47

Exercises 2. Assigning a Function Key You want to clear the Log and Output

Exercises 2. Assigning a Function Key You want to clear the Log and Output windows by simply pressing a button. Using the KEYS command, set the F 12 key to issue the commands. 48

Exercises 2. Assigning a Function Key a. Issue the KEYS command to access the

Exercises 2. Assigning a Function Key a. Issue the KEYS command to access the Keys window. b. Click in the definition area next to the F 12 key. c. Type the commands that you want to assign to the key, and separate them with a semicolon. Clear Log; Clear Output d. e. 49 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 F 12 key. What happens?

Exercises - Review: Setting a Function Key n Any of the commands that can

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

Section 2. 3 Mastering Fundamental Concepts

Section 2. 3 Mastering Fundamental Concepts

Objectives n n n 52 Define the components of a SAS data set. Define

Objectives n n n 52 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.

SAS Vocabulary n n n n 53 SAS data set Descriptor portion PROC CONTENTS

SAS Vocabulary n n n n 53 SAS data set Descriptor portion PROC CONTENTS Data portion Character variables Numeric variables SAS names n n n n SAS date value Character missing value Numeric missing value PROC PRINT SAS statement Free format SAS comments

Mastering Fundamental Concepts In this section, you learn basic SAS concepts. These concepts are

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

SAS Data Set Terminology SAS documentation and text in the SAS windowing environment use

SAS Data Set Terminology SAS documentation and text in the SAS windowing environment use the following terms interchangeably: 55 SAS Data Set Table Variable Column Observation Row

SAS Data Sets A SAS data set consists of two parts. SAS Data Set

SAS Data Sets A SAS data set consists of two parts. SAS Data Set Descriptor Portion 56 Data Portion

SAS Data Sets A SAS data set is a specially structured file that contains

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

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

SAS Data Sets SAS data sets have a descriptor portion and a data portion. Descriptor 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 Data Portion 58

Browsing the Descriptor Portion The descriptor portion of a SAS data set simply describes

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

Browsing the Descriptor Portion General form of the CONTENTS procedure: PROC CONTENTS DATA=SAS-data-set; RUN;

Browsing the Descriptor Portion General form of the CONTENTS procedure: PROC CONTENTS DATA=SAS-data-set; RUN; Example: proc contents data=sashelp. class; run; 60

Partial PROC CONTENTS Output The SAS System The CONTENTS Procedure Data Set Name Member

Partial PROC CONTENTS Output The SAS System The CONTENTS Procedure Data Set Name Member Type Engine Created Last Modified SASHELP. CLASS Observations 19 DATA Variables 5 V 9 Indexes Wednesday, May 12, 2004 Observation Length 40 10: 53: 55 PM Wednesday, May 12, 2004 Deleted Observations 10: 53: 55 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 61 0 0

SAS Data Sets: Data Portion The data portion of a SAS data set is

SAS Data Sets: Data Portion The data portion of a SAS data set is a rectangular table of character and/or numeric data values. Job. Title Salary TORRES LANGKAMM SMITH WAGSCHAL TOERMOEN JAN SARAH MICHAEL NADJA JOCHEN Pilot Mechanic Pilot 50000 80000 40000 77500 65000 Character values Variable values First. Name Variable names Last. Name Numeric values Variable names are part of the descriptor portion, not the data portion. 62

SAS Variable Values There are two types of variables: character and numeric. Character variables

SAS Variable Values There are two types of variables: character and numeric. Character variables n contain any value: letters, numbers, special characters, and blanks n are stored with a length of 1 to 32, 767 bytes n have one byte equal to one character n are left-aligned. 63 continued. . .

SAS Variable Values Numeric variables n are stored as floating point numbers in 8

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

SAS Data Set and Variable Names SAS data set and variable names must adhere

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

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data fivemonthsdata Five. Months. Data 66 . . .

Valid SAS Names Select the valid default SAS names. data 5 mon 67 .

Valid SAS Names Select the valid default SAS names. data 5 mon 67 . . .

Valid SAS Names Select the valid default SAS names. data 5 mon 68 .

Valid SAS Names Select the valid default SAS names. data 5 mon 68 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata 69 .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata 69 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata 70 .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata 70 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 71

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 71 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 72

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 72 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data 73 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data 74 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data fivemonthsdata 75 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data fivemonthsdata 76 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data fivemonthsdata Five. Months. Data 77 . . .

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five

Valid SAS Names Select the valid default SAS names. data 5 monthsdata data#5 five months data fivemonthsdata Five. Months. Data 78

Exercises This exercise reinforces the concepts discussed previously. n n 79 Appendix A Exercise

Exercises This exercise reinforces the concepts discussed previously. n n 79 Appendix A Exercise 3

Exercises – Pencil and Paper 3. Assigning Names a. Create your own SAS program

Exercises – Pencil and Paper 3. Assigning Names a. Create your own SAS program 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. 80

Dates in SAS An important concept with data is dates. You might need to

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

SAS Dates You have two character dates: 01 JAN 2008 01 APR 2008 If

SAS Dates You have two character dates: 01 JAN 2008 01 APR 2008 If you sorted these dates, they would not appear in chronological order, but in alphabetic order. 01 APR 2008 01 JAN 2008 Dates stored as numbers enable calculations and proper sorting. 82

SAS Date Values SAS stores date values as numeric values. A SAS date value

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. 01 JAN 1959 01 JAN 1960 01 JAN 1961 store -365 0 366 01/01/1960 01/01/1961 display 01/01/1959 83

Missing Data Values A value must exist for every variable for each observation. Missing

Missing Data Values A value must exist for every variable for each observation. Missing values are valid values. Last. Name First. Name Job. Title Salary TORRES LANGKAMM SMITH WAGSCHAL TOERMOEN JAN SARAH MICHAEL NADJA JOCHEN Pilot Mechanic Pilot 50000 80000. 77500 65000 A character missing value is displayed as a blank. 84 A numeric missing value is displayed as a period.

Browsing the Data Portion The PRINT procedure displays the data portion of a SAS

Browsing the Data Portion The PRINT procedure displays the data portion of a SAS data set. By default, PROC PRINT displays the following: n all observations n all variables n an Obs column on the left side 85

Browsing the Data Portion General form of the PRINT procedure: PROC PRINT DATA=SAS-data-set; RUN;

Browsing the Data Portion General form of the PRINT procedure: PROC PRINT DATA=SAS-data-set; RUN; Example: PROC is the keyword. PRINT is the procedure name. proc print data=work. staff; run; 86

PROC PRINT Output The SAS System First Obs Last. Name Job. Title Salary 1

PROC PRINT Output The SAS System First Obs Last. Name Job. Title 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 87

SAS Syntax Rules SAS statements n usually begin with an identifying keyword n always

SAS Syntax Rules SAS statements n usually begin with an identifying keyword n always end with a semicolon. data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff; class Job. Title; var Salary; run; 88 continued. . .

SAS Syntax Rules SAS statements are free-format. n n One or more blanks or

SAS Syntax Rules SAS statements are free-format. n n 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 Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc means data=work. staff; class Job. Title; var Salary; run; 89

SAS Syntax Rules SAS statements are free-format. n n One or more blanks or

SAS Syntax Rules SAS statements are free-format. n n 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 Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc means data=work. staff; class Job. Title; var Salary; run; 90

SAS Syntax Rules SAS statements are free-format. n n One or more blanks or

SAS Syntax Rules SAS statements are free-format. n n 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 Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc means data=work. staff; class Job. Title; var Salary; run; 91

SAS Syntax Rules SAS statements are free-format. n n One or more blanks or

SAS Syntax Rules SAS statements are free-format. n n 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 Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc means data=work. staff; class Job. Title; var Salary; run; 92

. . . SAS Syntax Rules SAS statements are free-format. n n One or

. . . SAS Syntax Rules SAS statements are free-format. n n 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 Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc means data=work. staff; class Job. Title; var Salary; run; 93

SAS Syntax Rules Indentation and good spacing makes the program easier to read. Conventional

SAS Syntax Rules Indentation and good spacing makes the program easier to read. Conventional Spacing Example data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff; class Job. Title; var Salary; run; 94

SAS Comments You can comment a block of text or code. n Type /*

SAS Comments You can comment a block of text or code. n Type /* to begin a comment. n Type your comment text. n Type */ to end the comment. /* Create work. staff data set */ data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; /* Produce listing report of work. staff */ proc print data=work. staff; run; 95

SAS Comments Commenting one statement. n Type an asterisk (*) at the beginning of

SAS Comments Commenting one statement. n Type an asterisk (*) at the beginning of the statement. n Everything that is between the asterisk and the semicolon is a comment. data work. staff; infile 'raw-data-file'; *input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; The * comments the entire statement, not only one line. How does SAS recognize the end of a statement? 96

Shortcut to Comments n n n 97 Highlight what you want to comment. Hold

Shortcut to Comments n n n 97 Highlight what you want to comment. Hold down the CTRL and / keys. To undo the comment, highlight the commented text and hold down the CTRL, SHIFT, and / keys.

Exercises This exercise reinforces the concepts discussed previously. 98 ch 2_sect 3_exercises. doc

Exercises This exercise reinforces the concepts discussed previously. 98 ch 2_sect 3_exercises. doc

Section 2. 4 Diagnosing and Correcting Syntax Errors

Section 2. 4 Diagnosing and Correcting Syntax Errors

Objectives n n 100 Identify SAS syntax errors. Debug and edit a program with

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

SAS Vocabulary n n n 101 Syntax errors Options RECALL

SAS Vocabulary n n n 101 Syntax errors Options RECALL

Syntax Errors Syntax errors include the following: n misspelled keywords n missing or invalid

Syntax Errors Syntax errors include the following: n misspelled keywords n missing or invalid punctuation n invalid options daat work. staff; infile 'emplist. dat'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; Average is proc print data=work. staff not a valid run; option. proc means data=work. staff average max; class Job. Title; var Salary; run; 102

Syntax Errors Procedure Options n Options tell SAS that you want the procedure to

Syntax Errors Procedure Options n Options tell SAS that you want the procedure to be processed differently than its default behavior. n In this example, you want only two statistics displayed: average and max. n Average is not a valid option for PROC MEANS. proc means data=work. staff average max; class Job. Title; var Salary; run; 103

Syntax Errors When SAS encounters a syntax error, SAS underlines where SAS thinks things

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: n the word ERROR or WARNING n the location of the error n an explanation of the error 104

Demonstration – Debugging a SAS Program This demonstration illustrates these tasks: n how to

Demonstration – Debugging a SAS Program This demonstration illustrates these tasks: n how to submit a SAS program that contains errors n diagnosing the errors n correcting the errors n saving the corrected program 105

Demonstration – Debugging a SAS Program In this demonstration, you open a program that

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: c 02 s 4 d 1. sas c 02 s 4 d 2. sas Start with the c 02 s 4 d 1. sas program in the SAS Programs folder. 106

Demonstration – Debugging a SAS Program daat work. staff; infile 'emplist. dat'; input Last.

Demonstration – Debugging a SAS Program daat work. staff; infile 'emplist. dat'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff run; proc means data=work. staff average max; class Job. Title; var Salary; run; 107

Demonstration – Debugging a SAS Program The SAS log contains error messages and warnings.

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 Last. Name $ 1 -20 First. Name $ 21 -30 4 Job. Title $ 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 108

Demonstration – Debugging a SAS Program The SAS log contains error messages and warnings.

Demonstration – Debugging a SAS Program The SAS log contains error messages and warnings. 6 7 proc print data=work. staff 8 run; -- 22 200 ERROR 22 -322: Syntax error, expecting one of the following: ; , (, DATA, DOUBLE, HEADING, LABEL, N, NOOBS, 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 109

Demonstration – Debugging a SAS Program The SAS log contains error messages and warnings.

Demonstration – Debugging a SAS Program The SAS log contains error messages and warnings. 110 10 proc means data=work. staff average max; ------- -- 22 202 ERROR 22 -322: Syntax error, expecting one of the following: ; , (, ALPHA, CHARTYPE, CLASSDATA, CLM, COMPLETETYPES, CSS, CV, DATA, DESCENDING, DESCENDTYPES, EXCLNPWGTS, EXCLUSIVE, FW, IDMIN, KURTOSIS, LCLM, MAXDEC, MEAN, MEDIAN, MISSING, N, NDEC, NMISS, NONOBS, NOPRINT, NOTHREADS, NOTRAP, NWAY, ORDER, P 10, P 25, P 50, P 75, P 90, P 95, P 99, PCTLDEF, PRINTALL, PRINTALLTYPES, PRINTIDVARS, PROBT, Q 1, Q 3, QMARKERS, QMETHOD, QNTLDEF, QRANGE, SKEWNESS, STDDEV, STDERR, SUMSIZE, SUMWGT, T, THREADS, UCLM, USS, VARDEF. ERROR 202 -322: The option or parameter is not recognized and will be ignored. 11 class Job. Title; 12 var Salary; 13 run; Part 3 of 4

Demonstration – Debugging a SAS Program The SAS log contains error messages and warnings.

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 111

Demonstration – Debugging a SAS Program The log indicates that SAS 112 n assumed

Demonstration – Debugging a SAS Program The log indicates that SAS 112 n assumed that the keyword data was misspelled and executed the DATA step n interpreted the word run as an option in the PROC PRINT statement (because there was a missing semicolon), so PROC PRINT was not executed n 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 1. If you use the Enhanced Editor, the

Demonstration – Debugging a SAS Program 1. If you use the Enhanced Editor, the program remains in the Enhanced Editor window. n However, if you use the Program Editor window, the code disappears with each execution. n Use the RECALL command or select Run Recall Last Submit to recall the submitted program to the Editor window. The original program is moved into the Editor window. 113 continued. . .

Demonstration – Debugging a SAS Program 2. Edit the program. n Correct the spelling

Demonstration – Debugging a SAS Program 2. Edit the program. n Correct the spelling of data. n Put a semicolon at the end of the PROC PRINT statement. n Change the word average to mean in the PROC MEANS statement. 3. Submit the program. The program runs successfully without errors and generates output. 114 continued. . .

Demonstration – Debugging a SAS Program data work. staff; infile 'emplist. dat'; input Last.

Demonstration – Debugging a SAS Program data work. staff; infile 'emplist. dat'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff mean max; class Job. Title; var Salary; run; 115

Demonstration – Submitting Part of the Code You do not always have to submit

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 execute the PROC PRINT step. n n 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. 116

Demonstration – Submitting Part of the Code What happens if you do not highlight

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

Demonstration – Submitting Part of the Code Why do you a message that PROC

Demonstration – Submitting Part of the Code Why do you a message that PROC PRINT is still running? n n 118 Is there output in the Output window? Are there any messages in the log?

Demonstration – Solution How does SAS determine the end of the step? Was the

Demonstration – Solution How does SAS determine the end of the step? Was the end of the step executed by SAS? No, SAS is still waiting. After SAS is given a step boundary, SAS can finish execution of the step. 119

Demonstration – Solution Highlight the RUN statement and submit only that statement. This enables

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

Demonstration – Debugging a SAS Program Open the second program: c 02 s 4

Demonstration – Debugging a SAS Program Open the second program: c 02 s 4 d 2. sas 121

Another Example Do you notice anything odd with this program? data work. staff; infile

Another Example Do you notice anything odd with this program? data work. staff; infile 'emplist. dat; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff mean max; class Job. Title; var Salary; run; 122

Using Quotation Marks in SAS Rules for using quotation marks are as follows: n

Using Quotation Marks in SAS Rules for using quotation marks are as follows: n You can use single or double quotation marks. n Quotation marks must be in pairs. " n " or ' ' Quotation marks must match. " n 123 = ' Typically, it does not matter if you use double or single quotation marks, but they must match.

Unbalanced Quotation Marks The closing quotation marks for the INFILE statement was missing. data

Unbalanced Quotation Marks The closing quotation marks for the INFILE statement was missing. data work. staff; infile 'c: sasdataemplist. dat; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff run; proc means data=work. staff average max; class Job. Title; var Salary; run; 124

Unbalanced Quotation Marks Submit the program and browse the SAS log. There are no

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

Unbalanced Quotation Marks The closing quotation marks for the INFILE statement was missing. data

Unbalanced Quotation Marks The closing quotation marks for the INFILE statement was missing. data work. staff; infile 'c: sasdataemplist. dat; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; . . . data work. staff; infile 'c: sasdataemplist. dat'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; Simply adding the needed quotation mark and submitting the code will not work. 126

Fixing Quotation Errors Why? ' The first two quotation marks are considered a matched

Fixing Quotation Errors 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. 127

Fixing Quotation Errors Because you know that the problem is unmatched quotation marks, you

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 128

Programmatically You want SAS to stop processing because the code did not execute properly.

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

Programmatically What is SAS waiting for? SAS expects a single quotation mark followed by

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; 130

Programmatically '; run; What if you forgot to submit a RUN statement? You added

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

Programmatically *"; run; What if you used a double quotation mark instead of single

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

Programmatically *'; run; What if you used a double quotation mark instead of single

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; 133

Programmatically Submit the following: *'; *"; run; n n n 134 If the quotation

Programmatically Submit the following: *'; *"; run; n n n 134 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.

Point-and-Click 1. To correct the problem, click the Break icon or press the CTRL

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

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

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

Fixing the Program 1. Select the Editor window to make it active. 2. Add

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

Resubmitting the Program 27 data work. staff; 28 infile 'raw-data-file' ; 29 input Last.

Resubmitting the Program 27 data work. staff; 28 infile 'raw-data-file' ; 29 input Last. Name $ 1 -20 First. Name $ 21 -30 30 Job. Title $ 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 data=work. staff; 34 run; NOTE: There were 18 observations read from the dataset WORK. STAFF. 35 36 proc means data=work. staff mean max; 37 class Job. Title; 38 var Salary; 39 run; NOTE: There were 18 observations read from the dataset WORK. STAFF. 138

Saving a SAS Program Two methods that can save a SAS program: 1. Menu-driven

Saving a SAS Program Two methods that can save a SAS program: 1. Menu-driven Select File Save As. 2. Toolbar shortcut Select the Save button. If changes were made to the program since it was last saved, SAS puts an asterisk * on the blue title bar of the Editor window. This indicates that you need to save the program. 139

What Happens If … … you forgot to save a program and need to

What Happens If … … you forgot to save a program and need to retrieve your code? 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 it. 140

What If You Need to Get the Code Back? The following are characteristics of

What If You Need to Get the Code Back? The following are characteristics of the recall buffer: n When you submit your code, SAS puts the text into a buffer. n Code is added to this buffer every time that you submit code. n The last code block that is added to the buffer is the first code block available for recall in the Enhanced Editor. 141

What If You Need to Get the Code Back? Use one of these methods:

What If You Need to Get the Code Back? Use one of these methods: n Select Run Recall Last Submit. n Submit the RECALL command. Repeat this until you see the desired code in your Editor window. Remember 142 SAS puts the new entries at the top of the Editor window.

Recall a Submitted Program statements accumulate in a recall buffer each time that you

Recall a Submitted Program statements accumulate in a recall buffer each time that you issue a SUBMIT command. daat work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff run; proc means data=work. staff average max; class Job. Title; var Salary; run; data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff mean max; class Jobtitle; var Salary; run; 143 Submit Number 1 Submit Number 2

Recall a Submitted Program Issue the RECALL command (from the Run menu) once to

Recall a Submitted Program Issue the RECALL command (from the Run menu) once to recall the most recently submitted program. Issue RECALL once. Submit Number 2 data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff mean max; class Job. Title; var Salary; run; Submit Number 2 statements are recalled. 144 continued. . .

Recall a Submitted Program Issue the RECALL command again to recall Submit Number 1

Recall a Submitted Program Issue the RECALL command again to recall Submit Number 1 statements. Submit Number 1 Submit Number 2 145 daat work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff run; proc means data=work. staff average max; class Job. Title; var Salary; run; data work. staff; infile 'raw-data-file'; input Last. Name $ 1 -20 First. Name $ 21 -30 Job. Title $ 36 -43 Salary 54 -59; run; proc print data=work. staff; run; proc means data=work. staff mean max; class Job. Title; var Salary; run;

Exercises This exercise reinforces the concepts discussed previously. 146 ch 2_section 3_and_4_crossword. htm

Exercises This exercise reinforces the concepts discussed previously. 146 ch 2_section 3_and_4_crossword. htm