Updating Records in an ESDS

Steps for Updating Records in an ESDS

To update records in an ESDS, follow these steps:
  1. Include both an INFILE and a FILE statement for the data set. Specify the VSAM option in both the INFILE and the FILE statements. Specify all other necessary options in the INFILE statement, which must precede the FILE statement.
  2. Use an INPUT statement to read the record being modified.
  3. Use the PUT statement to write the complete record. An INPUT statement brings the record into the INPUT buffer but does not copy it to the PUT buffer. This enables you to change the record easily.
For a list of options that you can use to update records in an ESDS, see SAS Options for an ESDS.
When records in an ESDS are updated, you have the following input and output access:
  • Input access for reading can be either sequential or direct. Access is sequential unless you specify the RBA= direct access option in the INFILE statement. (See Access Types for ESDS Operations.)
  • Output access for writing is direct because the last record that is read is the record updated.

Using the PUT Statement When Updating Records in an ESDS

When you update an ESDS record, you must use the PUT statement to write the complete record. There are two common ways of writing the record with the PUT statement:
  • Build the complete record by specifying all fields with the PUT statement. This method might be best when many of the fields need to be updated.
  • Copy the input record to the output buffer (with PUT _INFILE_) and overlay selected fields. This method might be best when relatively few fields need to be updated.
The latter method is the easiest for most applications. The following statement copies the last record that is read into the PUT buffer and overlays the information starting in columns 10 and 30 with the values in NEWDATA1 and NEWDATA2:
   PUT @  1 _INFILE_
       @ 10 NEWDATA1
       @ 30 NEWDATA2;
In most cases, when a record is retrieved for update, no user, including you, can access that particular record or any other records in the same control interval. Use the UPDATE= option to avoid user lockout when only a few of the records retrieved need to be updated. See Using the UPDATE= Option for more information.
In the following example, the RBAs of records are captured and stored in a SAS variable called RBAVAR from the _RBA_ automatic variable. In the next DATA step, the records are then read without being retrieved for update until the condition specified in the IF clause is met. When the IF condition is true (RBANUM=1260), the record is retrieved again with update access.
data rbas;
   infile myesds vsam ;
   input;
   rbanum=_rba_ ;
   keep rbanum;
run;

data esdsupdt;
   set rbas;
   updtevar=0;
   infile myesds vsam rba=rbanum update=updtevar;
   input;
   if (rbanum=1260) then do;
      updtevar=1;
      input;

         /* Create NEWDATA */
      lastname='Flintstone ';
      frstname='Fred      ';
      file myesds vsam ;
      put @1 _infile_
          @10 lastname
          @20 frstname;
   end;
run;