Loading Records into a VSAM Data Set

Loading Records into a New VSAM Data Set

VSAM does not allow you to process records while you are loading the data set. You can put the initial records into the data set, but you cannot read, update, or erase any of these records until the data set is closed. Because of this restriction, SAS requires you to use only a FILE statement, instead of both an INFILE and a FILE statement, for a VSAM data set that is to be loaded. You must also specify the VSAMLOAD System Option. After the data set is loaded and closed, you can add, update, or erase records when the VSAMUPDATE System Option is in effect.
You can read from and write to records in any other data set within the same DATA step that you use to load a VSAM data set. (For example, you can load a VSAM data set based on records that you are processing from another data set.)
VSAM requires you to load a KSDS sequentially in key order. You can load an RRDS either sequentially in record order or directly by using the RRN= direct-access option in the FILE statement. An ESDS can be loaded only sequentially.

Options Used When Loading Records into a New VSAM Data Set

When you load initial records into a new VSAM data set, use only a FILE statement and specify the VSAMLOAD System Option. In addition, you can use the following options in the FILE statement when you load a VSAM data set:
Options Used in the FILE Statement
BUFND=
KEYPOS=
RECORDS=
BUFNI=
LINE=
RESET
COL=
LINESIZE=
RRN=
FEEDBACK=
N=
VSAM
KEYLEN=
PASSWD=

Access Types When Loading Records into a VSAM Data Set

When you load records into a VSAM data set, access depends on the data set type:
  • For an ESDS, load access is sequential. That is, the records are loaded in the order in which you write them. You cannot change this order after loading the data set.
  • For a KSDS, load access is sequential. That is, you must load the records in key order.
    This VSAM restriction is imposed for performance reasons. If you attempt to load a record with a key lower than that of a previous record, VSAM returns a logical error with a feedback code of 12. If the sequential load restriction is a problem, load one or more records into the data set, and then access the data set a second time in another DATA step. After the data set is closed with one or more records, the load restrictions no longer apply. Loading the data set in this manner might take more computer resources than loading the data set sequentially. If the data to be loaded is in a SAS data set, you can sort it in primary key order with the SORT procedure before loading the VSAM data set.
  • For an RRDS, load access is sequential unless you specify the RRN= direct-access variable in the FILE statement.
    The first record is loaded into the first slot, and each subsequent record is loaded into the next successive slot unless you load the records in some other order by using the RRN= variable.

Reloading a VSAM Data Set

If you plan to reload data sets into an existing VSAM data set in a DATA step, keep the following points in mind:
  • You must define the data set with the VSAM option REUSE.
  • You must specify the VSAMLOAD System Option.
  • Use the RESET option in the FILE statement to reset the existing data set to empty (no records) when it is opened. If the VSAM data set is not defined with the REUSE option and you attempt to use the RESET option, the DATA step will not execute because VSAM does not open the data set.
  • Reload the empty data set with new records.
Data sets that have alternate indexes cannot be reloaded; they must be deleted, defined, and then loaded.

Loading a VSAM Data Set in a SAS DATA Step

The following example shows how to load a VSAM data set in a SAS DATA step. The data set is described in Sample STUDENT Data Set.
In the example, a previously defined ESDS is loaded in a SAS DATA step. The example also applies to a KSDS and an RRDS.
data load;
      /* Open a SAS data set for input. */
   set vsamdata.student;

      /* Open previously defined VSAM ESDS for output. */
   file myesds vsam;

      /* Write the data from the variable in the SAS data set to     */
      /* the appropriate column in a record of the ESDS.             */
   put @1  id       $9.   /* Student's Social Security number        */
       @10 lastname $10.  /* Student's surname                       */
       @20 frstname $10.  /* Student's given name                    */
       @30 address  $25.  /* Permanent mailing address               */
       @55 city     $15.  /* City of residence                       */
       @70 state    $2.   /* State of residence                      */
       @72 zip      $5.   /* Five-digit ZIP code                     */
       @77 balance  $5.   /* Balance from previous semester (if any) */
       @82 gpa      $4.   /* Grade point average on a 4.00 scale     */
       @86 class    $2.   /* FR, SO, JU, SE, or, GR                  */
       @88 hrs      $2.   /* Hours registered for in next semester   */
       @90 finaid   $1.;  /* Financial aid eligibility, Y or N       */
run;