Controlling the Update Process

Overview

When the DATA step contains a MODIFY statement, SAS writes the current observation to its original place in the SAS data set. This action occurs by default through an implied REPLACE statement at the bottom of the DATA step.
However, you can override this default behavior by explicitly adding the OUTPUT, REPLACE, or REMOVE statement.
General form for OUTPUT, REPLACE, and REMOVE statements:
OUTPUT;
REPLACE;
REMOVE;
Here is an explanation of the syntax:
OUTPUT
adds the current observation to the end of the data set.
REPLACE
writes the current observation to the same location in the data set.
REMOVE
removes the current observation from the data set.
Using OUTPUT, REPLACE, or REMOVE statements in a DATA step with a MODIFY statement can change the default replacement of observations. You can use these three statements together as long as the sequence is logical.
CAUTION:
If you use an OUTPUT statement in conjunction with a REPLACE or REMOVE statement, make sure that the OUTPUT statement is executed after any REPLACE or REMOVE statements to ensure the integrity of the index position.

Example

If the SAS data set Transaction has a variable named Code that has values of yes, no, and new, you can submit the following program in order to do one of the following:
  • delete rows where the value of Code is no
  • update rows where the value of Code is yes
  • append rows where the value of Code is new.
data master;
   set transaction;
   modify master key = id;
   a = b;
   if code = 'no' then remove;
   else if code = 'yes' then replace;
   else if code = 'new' then output;
run;
Note: You cannot run this example because Transaction and Master are fictitious data sets.

Monitoring I/O Error Conditions

When you use the MODIFY statement with a BY statement or KEY= option to update a data set, error checking is important for several reasons. The most important reason is that these tools use nonsequential access methods, so there is no guarantee that an observation is located so that it satisfies the request. Error checking enables you to perform updates or not, depending on the outcome of the I/O condition.
The automatic variable _IORC_ (Input Output Return Code) is created when you use the MODIFY statement with the BY statement or KEY= option. The value of _IORC_ is a numeric return code that indicates the status of the most recently executed I/O operation. Checking the value of this variable enables you to detect abnormal I/O conditions and direct execution in particular ways rather than having the application terminate abnormally.

Using _IORC_ with %SYSRC

Because the values of the _IORC_ automatic variable are internal and subject to change, %SYSRC, an autocall macro, was created to enable you to test for specific I/O conditions while protecting your code from future changes in _IORC_ values.
General form, _IORC_ with the %SYSRC autocall macro:
IF _IORC_=%SYSRC (mnemonic) THEN executable_statement;
Here is an explanation of the syntax:
mnemonic
is a code for a specific I/O condition.
Note: %SYSRC is in the autocall library. You must have the MACRO system option in effect to use this macro.
When you use %SYSRC, you can check the value of _IORC_ by specifying one of the mnemonics listed below.
Mnemonic
Meaning
_DSENMR
The observation in the transaction data set does not exist in the master data set (used only with the MODIFY and BY statements).
_DSEMTR
Multiple transaction data set observations do not exist in the master data set (used only with the MODIFY and BY statements).
_DSENOM
No matching observation (used with the KEY= option).
_SOK
The observation was located.

Example

Suppose you are using the MODIFY statement with the KEY= option to update a SAS data set. In the program below, when _IORC_ has the value _SOK, the observation is updated. When _IORC_ has the value _DSENOM, no matching observation is found, so the observation is appended to the data set by the OUTPUT statement and _ERROR_ is reset to 0 in the do group.
data master;
   set transaction;
   modify master key = id;
   if _IORC_=%sysrc(_sok) then
      do;
         a = b;
         replace;
      end;
   else
      if _IORC_=%sysrc(_dsenom) then
         do;
            output;
            _ERROR_ = 0;
         end;
run;
Tip
For more information about the _IORC_ automatic variable and %SYSRC, see information about error-checking tools in the SAS documentation.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.181.61