Writing a record to a file

To write a record to a file, do the following:

  1. Open the file for output (OPENFILE filename OUTPUT).
  2. Set a variable of the same name as the file name to the record you are writing to the file.
  3. Specify the file name on the PUTFILE statement to write the record to the data set, for example:
OPENFILE PRICES OUTPUT     /* open the file for output
SET PRICES = $2590.00      /* set variable to input record
PUTFILE PRICES             /* put variable record into the file
Note: If you use a variable for the filename on a PUTFILE statement, use a nested variable to contain the record, for example:
OPENFILE &FILEID OUTPUT    /* open the file for output
SET &&FILEID = $2590.00    /* set variable to input record
PUTFILE &FILEID            /* put variable record into the file

As long as the file remains open, successive PUTFILE statements write successive records to the data set. For a data set with a disposition of NEW, OLD, or SHR, if you close the file and then re-open it, a subsequent PUTFILE statement overlays the first record in the data set. For a data set with a disposition of MOD, if you close the file and then re-open it, a subsequent PUTFILE statement adds a record to the end of the data set.

Assume a CLIST contains the following variables:
&EMPLOYEE1,; which contains the value 'BLACKBUY: $200.00'.
&EMPLOYEE2,; which contains the value 'REFY: $449.00'.
&EMPLOYEE3,; which contains the value 'YARRUM: $450.00'.
To place the previous values in a data set called D58TAN1.CURNTSAL.DATA, you can code the following:
allocate file(salaries) da('d58tan1.curntsal.data') shr reu
OPENFILE SALARIES OUTPUT   /* Open the file for output     */
SET COUNTER=1
DO WHILE &COUNTER ¬> 3
 SET EMPLOYEE=&&EMPLOYEE&COUNTER
 SET SALARIES=&EMPLOYEE    /* Set the record to be written */
 PUTFILE SALARIES          /* Write the record             */
 SET COUNTER=&COUNTER+1    /* Increase counter by one      */
END
CLOSFILE SALARIES          /* Close the file               */