How to Handle VSAM files in PL1 language

Handling VSAM Files

VSAM data sets

3 Types of VSAM data sets are there :-

  1. Key Sequenced Data Set (KSDS)
  2. Entry Sequenced Data Set (ESDS)
  3. Relative Record Data Set (RRDS)

Note :- Sequential is the default file organization, hence vsam should be specified in the ENV option.

Vsam files are always record files. Records on a vsam file are not held in blocks, hence the blksize option even if specified is ignored. A recsize option will be checked against the details stored with the file.

The utility IDCAMS (access method services) is used to set up a vsam file initially.

Various DCL for VSAM datasets
e.g. :- dcl vfile file record direct keyed input env(vsam);

Vsam files are all direct access files. associated with every record is a key, which helps in accessing the record in any order.

e.g. :- dcl vfile file record sequential input env(vsam);

If you want to start at the beginning and process every record, just as with a consecutive file, specify the file as sequential.

e.g. :- dcl vfile file record sequential keyed input env(vsam);

The first read of the program specifies the key of the record you want to start at. subsequent sequential read statements are issued without a key.

e.g. :- dcl infile file record direct keyed input env(vsam);
to read a vsam file directly.

e.g. :- dcl infile file record direct update env(vsam password(‘master’));

When a file is originally set, it can be given one or more passwords. if attribute record is given, it means that it must be a file, hence the attribute file is implied and therefore optional.

Key Sequenced Data Set (KSDS)

KSDS is used to hold master file data. identification is by means of key so the attribute DIRECT implies KEYED.
e.g.:- dcl amast file record direct input env(vsam);
Sequential access is possible with ksds.

A ksds has a key which is part of a data record, this is known as an embedded key. Keys are also stored in another part of data set called as prime index.  The keys are unique since duplication is not allowed.

KSDS Features Reading a ksds directly :- Records are accessed by specifying a value using the key option.
e.g.:- dcl amast file record direct input env(vsam);

       dcl 1 area,
             2 kskey char(5),
             2 ksname char(15);
       read file(amast) into(area) key('00010');

Updating a ksds directly :- The file should be opened as update. you cannot amend the key.

e.g.: dcl kmast file record direct update env(vsam);

      dcl 1 area,
            2 kskey char(5) init('00081'),
            2 ksname char(15);
      read file(kmast) into(area) key(kskey);
      /* amend */
      rewrite file(kmast) from(area) key(kskey);

Delete a record in ksds :- :- :- Use delete statement.

e.g. :- kskey = ‘00056’;

        delete file(ksds) key(kskey);

Add a record in ksds :- :- :- Use write statement.

e.g. :- kskey = ‘00024’; ksname = ‘Applets’;

        write file(ksds) from(area) keyfrom(kskey);
  • First direct access & then sequential access :-

e.g.: dcl ramp file record sequential keyed

        read file(ramp) into(area) key('123/999/b');
        read file(ramp) into(area);

GENKEY option

  • g. :- dcl vsam file record sequential keyed
                     env(vsam genkey);
        read file(vmast) into(area) key('123');
        read file(vmast) into(area);

Assume that the key is defined as nine chars long.

If genkey option is not specified in the environment statement, vsam searches for a record with a key of ・23・

If genkey option is specified the first read reads the first record of data set beginning with 123. Second read continues with the record immediately following it.

VSAM files in PLI

ENTRY Sequenced Data Set (ESDS)

It is the most likely consecutive non-vsam file. i.e. data is not in a particular order, hence the file is sequential. An esds file which already has data on it, may only be opened with either input or update attributes.

e.g. :- dcl esds file record sequential update env(vsam);

Updating an esds file :-
It is possible to change records once they have been written to esds file.
e.g. :- read file(esds) into(area);

        rewrite file(esds) from(area);

Write can also be used and the new records added are always added at the end.
Delete is not allowed.

“How To Take the First Steps To Scale Up Your Career-When I stepped into a front desk role, I never imagined I would be an owner by age 25. I often get asked if I knew I wanted to be a business owner and entrepreneur.

The truth is, I didn’t. It never occurred to me until much later in my career. I kept working hard and learning as much as I could, and everything was like a domino effect — one opportunity to the next.

At some point, I realized that I was cut out to be a business owner, entrepreneur and leader. I’m deeply grateful for my experiences and proud of the hard work I put in, but I can’t stress this enough: I didn’t know it going in. I’m happy where I am and am continuing to learn, grow, and excel. Read more.”

Keyed access to an ESDS file

An esds is a sequential file and has no defined key. The information that can be used for keyed access is called relative byte address (rba).
record-1  : record-2  : record-3  : record-4
                 :                   :              :
50 bytes  : 40 bytes  : 60 bytes  : 30 bytes
                  :                     :             :

rba=0  rba=50      rba=90      rba=150

You have to get hold of rba by using KEYTO option on read and write.
g. :- dcl esds file record sequential keyed update

        dcl rba char(4);
        write file(esds) from(area) keyto(rba);

Rba must be a 4 byte character string. this will add a new record to the end of the file and place the relative byte address of the record in the field rba.

This is then used as a key on a read or rewrite. file should be declared as keyed.
e.g. :- read file(esds) into(area) key(rba);

  • g. :- /* example of esds read and rewrite */
        dcl esds file record sequential keyed update
        dcl rba char(4);
        read file(esds) into(area) keyto(rba);
        . . . . . .

        rewrite file(esds) from(area) key(rba);

RELATIVE Record Data Set (RRDS)

Records are read and can be accessed sequentially or directly with or without keys. The data set may be thought of as a set of numbered slots each of which may hold record. the numbering starts at 1 and goes to the maximum specified.

Record number is the relative record number. this is the key to the file. The key must be declared as a character string of length 8.
g. :- dcl relrecno char(8) init(‘00000145’);

  • g. :- dcl relrecno char(8) init(‘ 145’);

Keyed access to an RRDS
File may be declared as sequential or direct. If the file is declared as keyed sequential all key options can be used. if you leave key reference, processing would be just for a sequential file.

dcl rrds file record sequential keyed update env(vsam);
dcl rrkey char(8);
rrkey = ('00000025');
read file(rrds) into(area) key(rrkey); /* read 25 */
rewrite file(rrds) from(area);     /* 25 is rewritten */
read file(rrds) into(area) keyto(rrkey); /* requests a */
                                         /* record number. */
                                         /* the next record will */
                                         /* be presented and the */
                                         /* key value will be */
                                         /* placed in rrkey */


  • g. :- /* assume record 5 exists on file rrds. */
        dcl rrds file record sequential keyed update
        dcl relrecno char(8);
        dcl 1 area,
              2 ...... ;

        relrecno = '00000005';

        read file(rrds) into(area) key(relrecno);


        delete file(rrds) key(relrecno);
        write file(rrds) from(area)

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.