about using the SELECT statement conditionally, see “SELECT Statement” in SAS
DATA Step Statements: Reference.
Error Checking When Using Indexes to Randomly
Access or Update Data
The Importance of Error Checking
When reading observations with the SET statement and KEY= option or with the
MODIFY statement, error checking is imperative for several reasons. The most
important reason is that these tools use nonsequential access methods. Therefore, there is
no guarantee that an observation will be located that satisfies the request. Error checking
enables you to direct execution to specific code paths, depending on the outcome of the
I/O operation. Your program continues execution for expected conditions and terminate
execution when unexpected results occur.
Error-Checking Tools
Two tools have been created to make error checking easier when you use the MODIFY
statement or the SET statement with the KEY= option to process SAS data sets:
_IORC_ automatic variable
SYSRC autocall macro
_IORC_ is created automatically when you use the MODIFY statement or the SET
statement with KEY=. The value of _IORC_ is a numeric return code that indicates the
status of the I/O operation from the most recently executed MODIFY or SET statement
with KEY=. Checking the value of this variable enables you to detect abnormal I/O
conditions and to direct execution down specific code paths instead of having the
application terminate abnormally. For example, if the KEY= variable value does match
between two observations, you might want to combine them and output an observation.
If they do not match, however, you might want to only write a note to the log.
Because the values of the _IORC_ automatic variable are internal and subject to change,
the SYSRC macro was created to enable you to test for specific I/O conditions while
protecting your code from future changes in _IORC_ values. When you use SYSRC, you
can check the value of _IORC_ by specifying one of the mnemonics listed in the
following table.
Table 23.4 Most Common Mnemonic Values of _IORC_ for DATA Step Processing
Mnemonic Value Meaning of Return Code When Return Code Occurs
_DSENMR The Transaction data set
observation does not exist in the
Master data set.
MODIFY with BY is used and no
match occurs.
518 Chapter 23 Reading, Combining, and Modifying SAS Data Sets
Mnemonic Value Meaning of Return Code When Return Code Occurs
_DSEMTR Multiple Transaction data set
observations with the same BY
variable value do not exist in the
Master data set.
MODIFY with BY is used and
consecutive observations with the
same BY values do not find a
match in the first data set. In this
situation, the first observation that
fails to find a match returns
_DSENMR. The subsequent
observations return _DSEMTR.
_DSENOM No matching observation was
found in the Master data set.
SET or MODIFY with KEY=
finds no match.
_SENOCHN The output operation was
unsuccessful.
the KEY= option in a MODIFY
statement contains duplicate
values.
_SOK The I/O operation was successful. a match is found.
Example 1: Routing Execution When an Unexpected Condition
Occurs
Overview
This example shows how to prevent an unexpected condition from terminating the
DATA step. The goal is to update a master data set with new information from a
transaction data set. This application assumes that there are no duplicate values for the
common variable in either data set.
Note: This program works as expected only if the master and transaction data sets
contain no consecutive observations with the same value for the common variable.
For an explanation of the behavior of MODIFY with KEY= when duplicates exist,
see the MODIFY statement in SAS DATA Step Statements: Reference.
Input Data Sets
The Transaction data set contains three observations: two updates to information in
Master and a new observation about PartNumber value 6 that needs to be added. Master
is indexed on PartNumber. There are no duplicate values of PartNumber in Master or
Transaction. The following shows the Master and the Transaction input data sets:
Master Transaction
OBS PartNumber Quantity OBS PartNumber AddQuantity
1 1 10 1 4 14
2 2 20 2 6 16
3 3 30 3 2 12
4 4 40
5 5 50
Error Checking When Using Indexes to Randomly Access or Update Data 519

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.