Previous Page | Next Page

Processing a KSDS in a SAS Job

Combined Operations on a KSDS


Introduction to Combined Operations on a KSDS

You might want to perform more than one operation on a KSDS 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 number of 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 might need to process the KSDS in its INFILE statement.


Adding Records without Reading


Introduction to Adding Records without Reading

When you do not execute an INPUT statement before the PUT statement (because you are adding records without reading from the KSDS), SAS assumes that the data in the PUT statement is to be added as a new record. You must specify a new primary key in the PUT statement data.

If a record with this key already exists, VSAM refuses to replace it and returns a logical error with a feedback code of 8. To replace the existing record with the new data, set the KEY= variable to match the PUT statement key data, read the record with an INPUT statement, and re-execute the PUT statement. Remember that VSAM does not allow you to change the primary key field.


Key Testing with FEEDBACK= and the PUT Statement

You can use the FEEDBACK= option to test whether a record with a particular key exists. Then you can 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 key-testing technique using the FEEDBACK= option and the data in the PUT statement:

Here is an example:

data twelve;
   length keyvar $9.;
   infile myksds vsam feedback=fdbk  key=keyvar keypos=poskey;
   file myksds vsam;

      /* Assign a value to the KEYVAR variable,    */
      /* which contains the record's key.          */
   keyvar='964514789';
   lastname='Flintstone    ';
   frstname='Fred        ';
   address='1234 Quarry Rd';
   city='Boulder ';
   state='CO';
   zip='12345 ';
   balance='00999';
   gpa='1.33';
   class='SE';
   hrs='13';
   finaid='Y';

    /* Try to write as a new record (that is, without reading). */
      put @poskey  keyvar  $9.
          @10 lastname $10.
          @20 frstname $10.
          @30 address  $15.
          @55 city     $15.
          @70 state    $2.
          @72 zip      $5.
          @77 balance  $5.
          @82 gpa      $4.
          @86 class    $2.
          @88 hrs      $2.
      
      /* If the record already exists, reset FDBK and _ERROR_  */
      /* to 0, read in the record, write the record's key, and */
      /* update the record with new data.                      */


      if fdbk=8 then do;
          fdbk =0;
         _error_ = 0;
         input;
         put @ poskey  keyvar  $9.
             @10 lastname $10.
             @20 frstname $10.
             @30 address  $15.
             @55 city     $15.
             @70 state    $2.
             @72 zip      $5.
             @77 balance  $5.
             @82 gpa      $4.
             @86 class    $2.
             @88 hrs      $2.
             @90 finaid   $1.;

      end;  /* If FDBK=8 */
      stop;
run;


Adding Records after Reading


Introduction to Adding Records after Reading

When you are reading from the KSDS before you write, SAS assumes that the data that is in the PUT buffer is to modify the record that you have just read. This is true unless you have changed the primary key with PUT @ statements after an INPUT statement and before the final PUT statement executes.

When you have changed the primary key 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 as long as the key field does not duplicate the key of an existing record. A VSAM logical error occurs if the key duplicates the key of an existing record.


Key Testing with FEEDBACK=, KEY=, and the INPUT Statement

You can use the FEEDBACK= option to test whether a record with a particular key exists. You then can 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 key-testing technique using the FEEDBACK= and KEY= options and an INPUT statement:

Here is an example:

data thirteen;
   length keyvar $9.;
   infile myksds vsam feedback=fdbk key=keyvar keypos=poskey;

      /* Assign a value to the KEYVAR variable, */
      /* which contains the record's key        */
   keyvar='984312769';
   lastname='Rubble       ';
   frstname='Barney      ';
   address='1234 Gravel Rd';
   city='Boulder ';
   state='CO';
   zip='12345 ';
   balance='00001';
   gpa='0.33';
   class='SE';
   hrs='13';
   finaid='Y';

   input;

      /* If there is no record with this key, reset the FDBK and    */
      /* _ERROR_ variables to 0, and write a message on the SAS     */
      /* print file that a new record has been added with this key. */

   if fdbk=16 then do;
      fdbk =0;
      _error_ = 0;
      file print;
      put 'New record added.  Key is '  keyvar;
   end;

      /* Write the record to the data set: we are updating if there */
      /* is a record with this key and adding a new record if       */
      /* there is not. 
                                             */
   file myksds vsam;
   put @poskey  keyvar  $9.
       @10 lastname $10.
       @20 frstname $10.
       @30 address  $15.
       @55 city     $15.
       @70 state    $2.
       @72 zip      $5.
       @77 balance  $5.
       @82 gpa      $4.
       @86 class    $2.
       @88 hrs      $2.
       @90 finaid   $1.;

   stop;
run;


Comparing Key-Testing Techniques

Notice the differences between the two key-testing techniques:

Previous Page | Next Page | Top of Page