Processing VSAM Data Sets in SAS Programs |
Introduction to Updating Records |
Performing an update operation involves both input access (because the record must be read first) and output access (because you update by writing to the data set). Input access for an update can be either sequential or direct. Output access is sequential unless one of the direct access variables (KEY=, RRN=, or RBA=) is specified. When you update an existing record in a VSAM data set, you must use the following options and statements:
specify the VSAMUPDATE system option.
include both an INFILE and a FILE statement with the same fileref and the VSAM option in the DATA step. Specify all other options in the INFILE statement, which must precede the FILE statement.
use an INPUT statement to read the record that is being modified.
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 method enables you to change the record easily.
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 updating or when the updated record is shorter than the existing record because it avoids the problem of trying to eliminate or "blank out" the unwanted fields.
copy the input record to the output buffer (with PUT _INFILE_) and overlay selected fields in the copy. 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 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;
Limitations on Updating Records |
To maintain data integrity in multiple update situations, VSAM uses operating system facilities to protect the data. However, when either SHROPTIONS(3 X) or (4 X) is specified (full sharing by any number of users) and records in the same control area are updated simultaneously, some of the updates might be lost (X is any value). When SHROPTIONS(3 X) is used, each user is responsible for maintaining both Read and Write integrity for the data the program accesses. SHROPTIONS(4 X) requires your program to use the ENQ and DEQ macros to maintain data integrity while sharing the data set. For more information on using ENQ and DEQ, see IBM documentation.
Using the UPDATE= Option |
Sometimes a program accessing a VSAM data set reads many records but updates only a few of the records. When a record is retrieved for update, no other user, including you, can access that particular record or any other records in the same control interval until you release the record by executing another PUT or an INPUT statement for the data set. (This is significant when a VSAM data set is simultaneously accessed by other users or by an online system, such as CICS.) Use the UPDATE= option in the INFILE statement to avoid user lockout when only a few of the records that are retrieved need to be updated.
The UPDATE= option specifies a numeric SAS variable that indicates whether a record is to be read only or updated.
When you set the UPDATE= variable to a value of 1 before an INPUT statement executes, the record is retrieved for update. This is the same action that is taken if the UPDATE= option is not specified.
When you set the UPDATE= variable to a value of 0 before an INPUT statement executes, the record is not retrieved for update.
If you retrieve a record with the UPDATE= variable set to 0 (that is, the record is not retrieved for update) and then decide that you do want to update the record, reset the UPDATE= variable to 1, retrieve the record again, and then update the record. This is possible only with direct access. If you are reading the data set sequentially, you must keep track of the records that you want to update (use a SAS data set for this) and read them for update in a subsequent DATA step.
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.