O'Reilly logo

Step-by-Step Programming with Base SAS 9.4, Second Edition, 2nd Edition by SAS Institute

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The following output displays the modified INVENTORY_TOOL data set:
Figure 21.2 The INVENTORY_TOOL Data Set with Updated Prices
Modifying a Master Data Set with Observations
from a Transaction Data Set
Understanding the MODIFY Statement
The MODIFY statement replaces data in a master data set with data from a transaction
data set, and makes the changes in the original master data set. You can use a BY
statement to match observations from the transaction data set with observations in the
master data set. The syntax for using the MODIFY statement and the BY statement
follows:
MODIFY master-SAS-data-set transaction-SAS-data-set;
BY by-variable;
Master-SAS-data-set specifies the SAS data set that you want to modify. Transaction-
SAS-data-set specifies the SAS data set that provides the values for updating the master
data set. By-variable specifies one or more variables by which you identify
corresponding observations.
When you use a BY statement with the MODIFY statement, the DATA step uses
dynamic WHERE processing to find observations in the master data set. Neither the
master data set nor the transaction data set needs to be sorted. However, for large data
sets, sorting the data before you modify it can enhance performance significantly.
338 Chapter 21 Modifying SAS Data Sets
Adding New Observations to the Master Data Set
You can use the MODIFY statement to add observations to an existing master data set. If
the transaction data set contains an observation that does not match an observation in the
master data set, then SAS enables you to write a new observation to the master data set if
you use an explicit OUTPUT statement in your program. When you specify an explicit
OUTPUT statement, you must also specify a REPLACE statement if you want to replace
observations in place. All new observations append to the end of the master data set.
Checking for Program Errors
You can use the _IORC_ automatic variable for error checking in your DATA step
program. The _IORC_ automatic variable contains the return code for each I/O operation
that the MODIFY statement attempts to perform.
The best way to test the values of _IORC_ is with the mnemonic codes that are provided
by the SYSRC autocall macro. Each mnemonic code describes one condition. The
mnemonics provide an easy method for testing problems in a DATA step program. The
following is a partial list of codes:
_DSENMR
specifies that the transaction data set observation does not exist in the master data set
(used only with MODIFY and BY statements). If consecutive observations with
different BY values do not find a match in the master data set, then both of them
return _DSENMR.
_DSEMTR
specifies that multiple transaction data set observations with a given BY value do not
exist in the master data set (used only with MODIFY and BY statements). If
consecutive observations with the same BY values do not find a match in the master
data set, then the first observation returns _DSENMR and the subsequent
observations return _DSEMTR.
_DSENOM
specifies that no matching observation was found.
_SOK
specifies that the observation was located in the master data set and indicates that the
MODIFY statement executed successfully.
The Program
The program in this section updates values in a master data set with values from a
transaction data set. If a transaction does not exist in the master data set, then the
program adds the transaction to the master data set.
In this example, a warehouse received a shipment of new items, and the
INVENTORY_TOOL master data set must be modified to reflect the changes. The
master data set contains a complete list of the inventory items. The transaction data set
contains items that are on the master inventory as well as new inventory items.
The following program creates the ADD_INVENTORY transaction data set, which
contains items for updating the master data set. The PartNumber variable contains the
part number for the item and corresponds to PartNumber in the INVENTORY_TOOL
data set. The Description variable names the item. The NewStock variable contains the
Modifying a Master Data Set with Observations from a Transaction Data Set 339

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required