chapter 7 debugging techniques xiaogang su department of statistics university of central florida
Post on 05-Jan-2016
222 Views
Preview:
TRANSCRIPT
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.
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.
?? ?
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
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
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.
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.
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.
The DEBUG Option
General form of the DEBUG option:
DATA data-set-name / DEBUG;
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
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
12
DEBUG Commands
Controlling the Windows
• HELP: Displays information about DEBUG commands
• SWAP: Switches control between the Source window and the Log window
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
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
15
DEBUG Commands
Tailoring the Debugger
• ENTER: Executes statements one at a time
• QUIT: Terminates a debugger session
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.
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);
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
Section 7.2 Using the PUT Statement (Self Study)
Objectives
• Use the PUT statement in the DATA step to help identify logic problems.
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.
?? ?
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.
The PUT Statement
General forms of the PUT statement:
PUT 'text';
PUT variable-name=;
PUT _ALL_;
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.
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.
A way to debugFile: Chap7.sas
top related