Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

25
Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida

Transcript of Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Page 1: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Chapter 7 Debugging Techniques

Xiaogang Su

Department of Statistics

University of Central Florida

Page 2: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Section 7.1 Using the Debug Option (Self-Study)

Objectives

• Use the DEBUG option in the DATA statement to help identify logic problems.

Page 3: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Scenario

You have taken a new position in the company. Your predecessor wrote some code that was not working at the time he left. You need to identify what the program code is currently doing and where the problem is.

?? ?

Page 4: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Scenario - Current Results

Obs EMP_COUNTRY bonus type category

1 USA 2500 Overpaid 102 USA 2700 Overpaid 103 USA 12000 Overpaid 104 USA 4200 Overpaid 105 USA 3100 Overpaid 106 USA 2900 Overpaid 107 CANADA 8500 Overpaid 108 USA 3400 Overpaid 109 USA 2900 Overpaid 1010 USA 2700 Overpaid 1011 USA 3300 Overpaid 1012 USA 2200 Overpaid 1013 USA 1900 Overpaid 10

Page 5: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Scenario - Expected Results

Obs EMP_COUNTRY bonus type category

1 USA 2500 Overpaid 102 USA 2700 Overpaid 103 USA 12000 Overpaid 104 USA 4200 Overpaid 105 USA 3100 Overpaid 106 USA 2900 Overpaid 107 CANADA 8500 Overpaid 108 USA 3400 Overpaid 109 USA 2900 Overpaid 1010 USA 2700 Overpaid 1011 USA 3300 Underpaid 112 USA 2200 Underpaid 113 USA 1900 Overpaid 10

Page 6: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Syntax Errors versus Logic Errors

• A syntax error occurs when program statements do not conform to the rules of the SAS language. An error message is produced by SAS and written to the log.

• A logic error occurs when the program statements follow the rules, but the results are not correct.

Page 7: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Logic Errors

A non-programming example of a logic error:

Recipe for Garlic Bread

• Spread butter on bread and sprinkle garlic powder on the bread.

• Place the bread on a baking sheet.

• Place the baking sheet under the broiler for 10 minutes.

• Remove and enjoy!

Recipe for Garlic Bread

• Spread butter on bread and sprinkle garlic powder on the bread.

• Place the bread on a baking sheet.

• Place the baking sheet under the broiler for 10 minutes.

• Remove and enjoy!

Notice that there is no step about turning on the broiler. The instructions work, but do not result in a desired finished product.

Page 8: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

The DEBUG Option

The DEBUG option is an interactive interface to the DATA step during DATA step execution. This option is useful to determine

• which piece of code is executing

• which piece of code is not executing

• what the current value of a particular variable is

• whether a “watch” must be placed on particular variables to inform you when the value changes.

Page 9: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

The DEBUG Option

General form of the DEBUG option:

DATA data-set-name / DEBUG;

Page 10: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

DEBUG Commands

When the DEBUG option is running, these commands are very useful.

Abbreviation Command ActionW variable Watch Suspends execution when the

value of a specified variablechanges

L W List Watch Displays watched variablesE variable Examine Displays the value of one or

more variablesQ Quit Terminates a debugger

session/DATA stepEnter Key Executes statements one at a

time

Page 11: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

11

DEBUG Commands

Controlling Program Execution:

• GO: Starts or resumes execution of the DATA step

• JUMP: Restarts execution of a suspended program

• STEP: Executes statements one at a time in the active program

Page 12: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

12

DEBUG Commands

Controlling the Windows

• HELP: Displays information about DEBUG commands

• SWAP: Switches control between the Source window and the Log window

Page 13: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

13

DEBUG Commands

Manipulating DATA Step Variables

• CALCULATE: Evaluates a debugger expression and displays the result

• DESCRIBE: Displays the attributes of one or more variables

• EXAMINE: Displays the value of one or more variables

• SET: Assigns a new value to a specified variable

Page 14: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

14

DEBUG Commands

Manipulating Debugging Requests

• BREAK: Suspends program execution at an executable statement

• DELETE: Deletes breakpoints or the watch status of variables in the DATA step

• LIST: Displays all occurrences of the item that is listed in the argument

• SET: Controls whether the debugger displays a continuous record of the DATA step execution

• WATCH: Suspends execution when the value of a specified variable

changes

Page 15: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

15

DEBUG Commands

Tailoring the Debugger

• ENTER: Executes statements one at a time

• QUIT: Terminates a debugger session

Page 16: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

What the DEBUG Option Cannot Do

The DEBUG option can help to identify where a problem may be, but it is the programmer’s responsibility to solve the problem.

Page 17: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

The FIRSTOBS= and OBS= Options

The FIRSTOBS= and OBS= data set options are useful for testing because they can restrict the number of observations read in from a SAS data set.

The number specified on the FIRSTOBS= option indicates the first observation number to read and the number specified on the OBS= option indicates the last observation number to read.

SET data-set-name(FIRSTOBS=n OBS=n);

Page 18: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Determining Logic Errors

This demonstration illustrates using the DEBUG option to

• identify which pieces of code are or are not executing

• display the data values of selected variables

• watch the data values of selected variables change.

Program: Chap7.sas

Page 19: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Section 7.2 Using the PUT Statement (Self Study)

Objectives

• Use the PUT statement in the DATA step to help identify logic problems.

Page 20: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Scenario

You have taken a new position in the company. Your predecessor wrote some code that was not working at the time he left. You need to identify what the program code is currently doing and where the problem is.

?? ?

Page 21: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

The PUT Statement

By default the PUT statement writes information to the log. This is useful to determine

• which piece of code is executing

• which piece of code is not executing

• what the current value of a particular variable is

• what the current values of all variables are.

Page 22: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

The PUT Statement

General forms of the PUT statement:

PUT 'text';

PUT variable-name=;

PUT _ALL_;

Page 23: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

What the PUT Statement Cannot Do

The PUT statement can help identify where a problem may be, but it is the programmer’s responsibility to solve the problem.

Page 24: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

Determining Logic Errors

This demonstration illustrates running code that does not produce the intended results and using the PUT statement to

• identify which pieces of code are or are not executing

• display the data values of all variables

• display the data values of selected variables.

Page 25: Chapter 7 Debugging Techniques Xiaogang Su Department of Statistics University of Central Florida.

A way to debugFile: Chap7.sas