Adding Records to an RRDS

Introduction to Adding Records to an RRDS

To add records to an RRDS, complete the following 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 options in the INFILE statement, which must precede the FILE statement.
  2. Use the PUT statement to write the record.
For a list of options that you can use when adding records, see Access Types for RRDS Operations.
When you add records to an existing RRDS, you do not have to include an INPUT statement with the INFILE statement. An INPUT statement is unnecessary because you do not have to read a record in order to add a new record.

Access Type When Adding Records

In an RRDS, records are added with direct access. (See Access Types for RRDS Operations). You must supply the relative-record number by using the RRN= variable in the INFILE statement. The slot that is specified by the RRN= variable must be vacant in order to add a new record to that location in the data set.

Adding Records While Reading

If you are adding new records while reading the RRDS sequentially, use the SEQUENTIAL option in the INFILE statement. The SEQUENTIAL option specifies sequential retrieval that is combined with direct record storage. Use the SEQUENTIAL option only when you add new RRDS records while sequentially reading existing records. Use both the SEQUENTIAL and the RRN= options in the INFILE statement to indicate that the RRN= variable specifies the record that is to be added rather than the record that is to be read.
data addrrns;
      /* Create a new RRDS record and assign it to the */
      /* NEWREC variable.                              */
   length newrec $90.;
   id='984312769';
   lastname='Rubble    ';
   frstname='Barney    ';
   address='1234 Gravel Rd           ';
   city='Boulder        ';
   state='CO';
   zip='12345';
   balance='00001';
   gpa='0.33';
   class='SE';
   hrs='13';
   finaid='Y';
   newrec = id||lastname||frstname||address||city||state||
            zip||balance||gpa||class||hrs||finaid;

      /* Assign the RRN of the new record (NEWREC) to */
      /* the ADDRRN variable.                         */
   addrrn=31;
run;

data four;
   set addrrns;
   n = 0;

      /* Read the RRDS records with sequential access. */
   infile myrrds vsam rrn=addrrn sequential;
   input;
   n = n+1;

      /* 
Add the new RRDS record with direct access: RRN=ADDRRN option */
      /* applies to writing when the SEQUENTIAL option is used. The    */
      /* NEWREC variable contains the complete new record.             */

    if (n=1) then do;
       file myrrds vsam;
       put newrec;
    end;
run;
In the example, a record for a new student, BARNEY RUBBLE, is defined and added to data set MYRRDS. The data set is read sequentially, but the record is added with direct access to the record slot that is identified by the ADDRRN variable.
If you are adding new records while reading the RRDS directly, use the RRN= option to specify both the record that you want to read and the slot number where you want to add a record. First, set the RRN= variable to the record that you want to read. Read the record, but before the PUT statement executes, reset the RRN= variable to the slot number where you want to insert the new record. Change the value of the RRN= variable after you retrieve the record with an INPUT statement but before you write with a PUT statement.
The following example uses direct access to read and add new records. The NEWREC variable in the SAS data set ADDRRNS contains the complete new record, and the ADDRRN variable contains the RRN where the record is to be added.
data addrrns;

     /* Create a new RRDS record and assign it to the */
     /* NEWREC variable.                              */
   length newrec $90.;
   id='995613769';
   lastname='Rubble    ';
   frstname='Bettie    ';
   address='1234 Gravel Rd           ';
   city='Boulder        ';
   state='CO';
   zip='12345';
   balance='00001';
   gpa='2.22';
   class='SE';
   hrs='13';
   finaid='Y';
   newrec = id||lastname||frstname||address||city||state||
            zip||balance||gpa||class||hrs||finaid;
      /* Assign the RRN of the new record (NEWREC) to   */
      /* the ADDRRN variable.                           */
   addrrn=32;
run;

data readrrns;
   input readrrn ;
   datalines;
31
;
run;

data five;
   set readrrns;
   set addrrns;
   rrnvar=readrrn;

      /* Read the RRDS record specified by the READRRN variable. */
   infile myrrds vsam rrn=rrnvar;
   file myrrds vsam ;
   input;

      /* Add the new RRDS record with direct access by assigning the */
      /* value of ADDRRN to the RRNVAR variable and writing the      */
      /* NEWREC variable that contains the complete new record.      */
   if (readrrn = 31) then do;
      put _infile_;
      rrnvar =addrrn;
      put newrec;
   end;
run;