Combined Operations on an RRDS

How to Combine Operations on an RRDS

You might want to perform more than one operation on an RRDS in one DATA step. (For example, perhaps you want to read some records, update other records, and add new records in one DATA step.) Regardless of the operations, you need only one pair of INFILE and FILE statements for the entire DATA step. Specify the VSAM option in both the INFILE and the FILE statements. Specify any other options that you need to process that RRDS in its INFILE statement.
SAS determines whether you want to add new or update existing RRDS records.

Adding Records without Reading

Adding Records without Reading Overview

When you do not execute an INPUT statement before the PUT statement (because you are adding records without reading from the RRDS), SAS assumes that the data in the PUT statement is to be added as a new record, provided that you have specified an empty relative-record slot with the RRN= option. The slot that is specified by the RRN= variable must be vacant in order to add a new record in that location of the data set.
If the slot already contains a record, VSAM refuses to replace it and returns a logical error with a feedback code of 8. The FEEDBACK= option can be used to determine whether a particular slot is empty.

Slot Testing with FEEDBACK=, RRN=, and the PUT Statement

You can use the FEEDBACK= option to test whether the relative-record slot that is specified by RRN= is empty. You can then either update or add a record based on the value of the FEEDBACK= variable. The FEEDBACK= option specifies a SAS variable that is set to the VSAM logical error code when a logical error occurs. (See Error-Handling Techniques and Error Messages for more information.)
The following is the general slot-testing technique using the FEEDBACK= and RRN= options and the data in the PUT statement:
  • When the FEEDBACK= variable is 0 after the PUT statement executes, the slot is empty and the data in the PUT buffer has been added as a new record.
  • When the FEEDBACK= variable is 8 after the PUT statement executes, the slot is not empty.
    To update the existing record, reset the FEEDBACK= and _ERROR_ variables to 0, read the record with an INPUT statement, and re-execute the PUT statement.
    If you want to add a new record rather than replace the one in the slot, change the RRN= variable to another slot number and re-execute the PUT statement.
data rrdsinfo;
      /* Select values for lastname,firstname, and class. */
   length lastname $10 frstname $10 class $2;
   input id lastname frstname class;
   datalines;
   15 FLINTSTONE FRED SE 
   30 RUBBLE BARNEY SO
   31 FLINTSTONE WILMA SE
   32 RUBBLE BETTIE SO
   ;

data eight;
   set rrdsinfo;
   infile myrrds vsam feedback=fdbk rrn=id;
   file myrrds vsam;


      /* Assume this is a new record and write it without reading. */
   put @10 lastname  $10.
       @20 frstname  $10.
       @86 class     $2.;

      /* If the FEEDBACK= variable indicates that the record  */
      /* already exists, read it in and update it.            */
   if fdbk=8 then do;
       fdbk=0;
       _error_=0;
       input;
       put @1 _infile_ @86 class $2.
   end;
run;

Adding Records after Reading

Adding Records after Reading Overview

When you read from the RRDS before you write, SAS assumes that the data in the PUT statement modifies the record that you have just read unless you change the RRN= variable value before the PUT statement executes.
When you have changed the RRN= variable after an INPUT statement and before the PUT statement for the data set executes, the data in the PUT buffer is added as a new record (if the changed RRN= value specifies a vacant slot).

Slot Testing with FEEDBACK=, RRN=, and the INPUT Statement

You can use the FEEDBACK= option to test whether the relative-record slot that is specified by RRN= is empty. You can then either update or add a record based on the value of the FEEDBACK= variable. The FEEDBACK= option specifies a SAS variable that is set to the VSAM logical error code when a logical error occurs. (See Error-Handling Techniques and Error Messages for more information.)
The following is the general slot-testing technique using the FEEDBACK= and RRN= options and the INPUT statement:
  • When the FEEDBACK= variable is 0 after the INPUT statement executes, the record that is in the slot specified by RRN= has been read into the input buffer.
    Execute a PUT statement in order to update the record.
  • When the FEEDBACK= variable is 16 after the INPUT statement executes, the slot that is specified by RRN= is empty.
    Reset the FEEDBACK= and _ERROR_ variables to 0, and execute a PUT statement in order to add the PUT buffer data as a new record in this slot.
data rrdsinfo;
     /* Select values for lastname, firstname, and class. */
   length lastname $10 frstname $10 class $2;
   input id lastname frstname class;
   datalines;
   15 FLINTSTONE FRED SE     
   30 RUBBLE BARNEY SO      
   31 FLINTSTONE WILMA SE
   32 RUBBLE BETTIE SO
   ;

data nine;
   set rrdsinfo;
   infile myrrds vsam feedback=fdbk rrn=id;



      /* Read the relative-record number to be updated. */
   input;
   file myrrds vsam;

      /* If the FEEDBACK= variable indicates that the relative */
      /* record slot number is empty, reset the FDBK and       */
      /* _ERROR_ variables to 0, and write a new record.       */
   if fdbk=16 then do;
      fdbk=0;
      _error_=0;
      put @10 lastname  $10.
          @20 frstname  $10.
          @86 class     $2.;

         /* If the FEEDBACK= variable indicates PUT for update    */
         /* without previous INPUT then reset the FDBK and        */
         /* _ERROR_ variables to 0, and write the new record.     */
      if fdbk=92 then do;
          fdbk=0;
          _ERROR_=0;
          put @10 lastname  $char10.
              @20 frstname  $char10.
              @86 class     $char2.;
      end;   
end;

      /* If the record exists, update the class field. */
   else do;
      put _infile_ @86 class;
   end;
run;

Comparing Slot-testing Techniques

Notice the differences between the two slot-testing techniques:
  • The first, based on data in the PUT statement and the RRN= option, automatically adds the information as a new record if the slot is empty. Be aware that you might create a record that you do not want.
  • The second, based on an INPUT statement and the RRN= option, is a safer technique, because you must deliberately issue a PUT statement to add a new record.