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:
-
When the FEEDBACK= variable is
0 after the PUT statement executes, the data in the PUT statement
has been added as a new record.
-
When the FEEDBACK= variable is
8 after the PUT statement executes, a record with that key already
exists. Therefore, the data in the PUT buffer is not added as a new
record, because VSAM does not allow duplicate primary keys.
To replace the existing
record, reset the FEEDBACK= and _ERROR_ variables to 0, set the KEY=
variable to match the PUT statement key data, issue an INPUT statement,
and re-execute 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;