![]() | ![]() | ![]() |
This example demonstrates how to obtain information about a system option with the SAS function GETOPTION and how to use that information to control the processing of a program.
The GETOPTION function allows you to check SAS options within a DATA step. You can then control processing based on the values of specific options.
The years in the data lines in this example are specified with two-digit years. Assume the code in subsequent steps requires the option YEARCUTOFF to be set to 1920. The DATA step verifies the YEARCUTOFF setting. When the value is not 1920, an ABORT RETURN statement executes and stops the program.
Original Program
|
Using SAS Automatic Variables to Correct Errors
SAS creates SAS automatic variables when a DATA step executes. You can use these variables in your DATA step programming as you would any other variable.
|
You can write the values of SAS automatic variables to the SAS log to help you analyze your program and data. Your code can test the values of SAS automatic variables to direct conditional execution of DATA step statements.
Automatic variables are temporary and SAS does not store them in the data sets it creates. To save the value of an automatic variable, assign its value to a data set variable. |
Table 3.4 lists four automatic variables that are helpful in debugging your DATA steps. When a DATA step executes, SAS always creates the two automatic variables _ERROR_ and _N_. SAS creates the other two variables described in the table when your code includes a BY statement and input from one or more data sets. These four variables are part of the program data vector.
Table 3.4: SAS Automatic Variables Useful in Debugging DATA Steps
| Automatic Variable | Function |
| _ERROR_ |
When to use:
|
| FIRST.variable | Indicates whether the data values being processed are
from the first observation in a BY group where:
variable is the BY-group variable to examine. When to use:
|
| LAST.variable | Indicates whether the data values being processed are
from the last observation in a BY group where:
variable is the BY-group variable to examine. SAS sets the LAST.variable to 1 when the current observation is the last observation in the specified BY group. Otherwise, SAS sets this variable to 0. When to use:
|
| _N_ | Retains the number of iterations of a DATA step.
When to use:
|
Working with the _ERROR_ Automatic Variable
Your code can test the value of the _ERROR_ automatic variable to detect specific errors and then conditionally execute statements in your DATA steps.
The value of _ERROR_ is set to 0 at the start of iteration of a DATA step; SAS does not retain the value of _ERROR_ over iterations. When SAS encounters specific errors, it sets the value of _ERROR_ to 1. Your code can also set the value of _ERROR_ to 1.
When either you or SAS sets _ERROR_ to 1, SAS writes to the SAS log messages and the values in the program data vector.
| The value of the system option ERRORS= determines the maximum number of observations for which SAS lists complete error messages. |
For example, when ERRORS=20, SAS lists in the SAS log error messages for the first 20 observations that have errors. SAS does not identify errors in observations beyond these first 20. SAS writes a message to the SAS log when it reaches the maximum number of errors.
| Once you correct the first 20 observations with errors and run the corrected DATA step again, you may find additional observations with errors after those first 20. |
When SAS reads data from an external file or from data lines, it prints in the SAS log the column ruler the first time it sets _ERROR_ to 1.
About the Author
Michele Burlew is a self-employed SAS programmer with more than 20 years of experience. She specializes in designing and programming a variety of SAS applications, and she has expertise in using many SAS products. Michele's extensive experience in developing programs and working with end users gives her keen insight into the needs of SAS software users.
Her books are available from the online bookstore.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
| Type: | Sample |
| Topic: | Non SAS Authors ==> Michele Burlew Internal Administration ==> BBU |
| Date Modified: | 2007-09-11 15:47:58 |
| Date Created: | 2005-02-28 12:33:08 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | All | n/a | n/a |


