Processing a KSDS in a SAS Job |
To update records in a KSDS, complete the following steps:
specify the VSAMUPDATE system option.
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.
Use an INPUT statement to read the record being modified. You must first retrieve the record sequentially or by direct access, using either the KEY= or RBA= option, before you can update the data set.
For a list of the options that you can use when updating records, see Special SAS Options Used with a KSDS.
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 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;
Note: If you change the key of the record that was most recently retrieved, then the modified record is added as a new record. There is a VSAM restriction that does not allow you to change the primary key of a KSDS record.
In the following example, the SAS data set RKEYS contains the replacement data for a series of records in the data set MYKSDS. DATA1, DATA2, and KEYDATA are variables in the SAS data set RKEYS, which contains the new data and the VSAM key for records that are to be replaced.
data _null_; set rkeys; infile myksds vsam keypos=kp; input; file myksds vsam; put @1 data1 @30 data2 @ kp keydata; ...more SAS statements...
In most cases, when a record is retrieved for update, no user, including you, can access that particular record or any other records that are in the same control interval. Use the UPDATE= option to avoid user lockout when only a few of the records that are retrieved need to be updated. (See Using the UPDATE= Option for more information.) The following program reads records sequentially from the data set without retrieving them for update until the condition specified in the IF clause is met. When the IF condition is true (SASKEY = 547392749), the UPDATE= variable is set to 1, and the record is retrieved again with update access.
data keys; /* Use the SASKEY variable to select keys of records */ /* to process. */ infile myksds vsam keypos=kpvar keylen=klvar; retain kpvar klvar; input @kpvar saskey $varying200.klvar; run; /* Update records in a KSDS */ data updtksds; set keys; updtevar=0; infile myksds vsam key=saskey update=updtevar; input; if (saskey eq '547392749') then do; updtevar=1; input; /* Assign a value to _INFILE_, which contains the */ /* update data. */ file myksds vsam; put @1 _infile_ @10 'Flintstone Fred '; end; run;
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.