Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

COBOL  Module 8 – Indexed and relative files (VSAM concepts)

Relative files, VSAM organisations and IDCAMS

Use COBOL relative files with a RELATIVE KEY, compare the VSAM KSDS, ESDS, RRDS and LDS organisations, and read IDCAMS DEFINE, REPRO and PRINT steps.

  • Intermediate
  • 15 minutes
  • Examples run with GnuCOBOL 3.2 and Python 3.14.8
  • By MySmartCoPilot
Free core, Pro depth: 5 of 15 minutes free
Free: A relative file keeps each record in a numbered slot · VSAM keeps data in four organisations, and IDCAMS defines them · Practise: slots and organisations. With a pass: 2 in-depth parts and 3 interview answers, about 10 minutes.

What you will learn

  • Use a relative file with a RELATIVE KEY to write, read, delete and reuse numbered slots
  • Describe the VSAM KSDS, ESDS, RRDS and LDS organisations and the COBOL organization that opens each
  • Read IDCAMS DEFINE CLUSTER, REPRO and PRINT steps and predict what each command does
  • Explain what a relative file holds on disk after a DELETE, and what a RELATIVE KEY that is too small does

Before you start

What you’ll learn · Before you start

What you will learn

  • Use a relative file with a RELATIVE KEY to write, read, delete and reuse numbered slots
  • Describe the VSAM KSDS, ESDS, RRDS and LDS organisations and the COBOL organization that opens each
  • Read IDCAMS DEFINE CLUSTER, REPRO and PRINT steps and predict what each command does
  • Explain what a relative file holds on disk after a DELETE, and what a RELATIVE KEY that is too small does

Before you start

On this page

A branch has 50 lockers, numbered 1 to 50, and a locker’s number is all a clerk needs to find who holds it. A relative file stores each record in the slot whose number is its key, so locker 4 is slot 4, with no key field in the record.

This lesson runs a file of lockers with GnuCOBOL, then names the four kinds of VSAM data set that hold files on z/OS and the IDCAMS commands that define, load and print them.

A relative file keeps each record in a numbered slot

lockers.cob declares ORGANIZATION IS RELATIVE and RELATIVE KEY IS LK-SLOT. The RELATIVE KEY: The item named by RELATIVE KEY: a number outside the record that tells a relative-file statement which slot to use. A READ NEXT puts the number of the slot it read there. is a number in WORKING-STORAGE, not a field of the record: a WRITE, READ or DELETE takes its slot from LK-SLOT, and a READ NEXT puts the number of the slot it read there.

lockers.cob: four lockers placed, a taken slot, an empty one and a release COB · lockers.cob
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LOCKERS.
      * A relative file of lockers: the locker number is the slot.
      * Allocates four lockers, tries a taken one and an empty one,
      * releases locker 2 and lists the lockers in use.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT LOCKER-FILE ASSIGN TO "LOCKERS"
               ORGANIZATION IS RELATIVE
               ACCESS MODE IS DYNAMIC
               RELATIVE KEY IS LK-SLOT
               FILE STATUS IS LK-STATUS.
       DATA DIVISION.
       FILE SECTION.
       FD  LOCKER-FILE.
       01  LOCKER-REC.
           05  LK-HOLDER        PIC X(6).
           05  LK-SINCE         PIC 9(4).
       WORKING-STORAGE SECTION.
       01  LK-SLOT              PIC 9(3).
       01  LK-STATUS            PIC XX.
       PROCEDURE DIVISION.
           OPEN OUTPUT LOCKER-FILE
           MOVE 1 TO LK-SLOT
           MOVE "AC00011001" TO LOCKER-REC
           PERFORM PLACE-LOCKER
           MOVE 4 TO LK-SLOT
           MOVE "AC00041004" TO LOCKER-REC
           PERFORM PLACE-LOCKER
           MOVE 7 TO LK-SLOT
           MOVE "AC00071007" TO LOCKER-REC
           PERFORM PLACE-LOCKER
           MOVE 2 TO LK-SLOT
           MOVE "AC00021002" TO LOCKER-REC
           PERFORM PLACE-LOCKER
           MOVE 4 TO LK-SLOT
           MOVE "AC00091009" TO LOCKER-REC
           PERFORM PLACE-LOCKER
           CLOSE LOCKER-FILE
           OPEN I-O LOCKER-FILE
           MOVE 3 TO LK-SLOT
           READ LOCKER-FILE
           DISPLAY "READ locker   003  " LK-STATUS "  empty"
           MOVE 2 TO LK-SLOT
           DELETE LOCKER-FILE
           DISPLAY "DELETE locker 002  " LK-STATUS "  released"
           MOVE 0 TO LK-SLOT
           READ LOCKER-FILE
           DISPLAY "READ locker   000  " LK-STATUS "  no slot 0"
           MOVE 1 TO LK-SLOT
           START LOCKER-FILE KEY IS NOT LESS THAN LK-SLOT
           DISPLAY "START >= 001       " LK-STATUS
           READ LOCKER-FILE NEXT RECORD
           PERFORM UNTIL LK-STATUS NOT = "00"
               DISPLAY "READ NEXT     " LK-SLOT "  " LK-STATUS "  "
                   LK-HOLDER " since " LK-SINCE
               READ LOCKER-FILE NEXT RECORD
           END-PERFORM
           DISPLAY "READ NEXT          " LK-STATUS "  the end"
           CLOSE LOCKER-FILE
           STOP RUN.
       PLACE-LOCKER.
           WRITE LOCKER-REC
               INVALID KEY
                   DISPLAY "WRITE locker  " LK-SLOT "  " LK-STATUS
                       "  taken: " LK-HOLDER " not placed"
               NOT INVALID KEY
                   DISPLAY "WRITE locker  " LK-SLOT "  " LK-STATUS
                       "  " LK-HOLDER
           END-WRITE.
Statuses 22 and 23, a released locker, and the lockers in use Python · lockers_session.py
from session import files, run

# Lockers allocated, a taken one and an empty one tried, locker 2 released, and the lockers in use listed.
files("lockers.cob")
run("""
cobc -x lockers.cob
DD_LOCKERS=lockers.dat ./lockers
""")

Output

$ cobc -x lockers.cob
$ DD_LOCKERS=lockers.dat ./lockers
WRITE locker  001  00  AC0001
WRITE locker  004  00  AC0004
WRITE locker  007  00  AC0007
WRITE locker  002  00  AC0002
WRITE locker  004  22  taken: AC0009 not placed
READ locker   003  23  empty
DELETE locker 002  00  released
READ locker   000  23  no slot 0
START >= 001       00
READ NEXT     001  00  AC0001 since 1001
READ NEXT     004  00  AC0004 since 1004
READ NEXT     007  00  AC0007 since 1007
READ NEXT          10  the end

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 lockers_session.py

The WRITEs to slots 1, 4, 7 and 2 returned 00, in that order, and the second WRITE to slot 4 returned 22, because AC0004 already held it. A READ of slot 3 returned 23: the slot is empty. DELETE emptied slot 2, a READ with LK-SLOT 0 returned 23, since slots count from 1, and the browse read slots 1, 4 and 7, skipping the empty ones. Step through the seven slots:

Interactive · step by step Seven slots of the locker file, one statement at a time Step through the run. LK-SLOT, outside the record, says which slot each statement uses, and the status says what it found.
Seven slots of the locker file, one statement at a time, step 8 of 8. Showing Slot 1 (AC0001 since 1001), Slot 2 (empty again), Slot 3, Slot 4 (AC0004 since 1004), Slot 5, Slot 6, Slot 7 (AC0007 since 1007), LK-SLOT (LK-SLOT = 007, then 10). In focus: Slot 1, Slot 4, Slot 7, LK-SLOT.Slot 1AC0001 since 1001Slot 2empty againSlot 3emptySlot 4AC0004 since 1004Slot 5emptySlot 6emptySlot 7AC0007 since 1007LK-SLOTLK-SLOT = 007, then 10

The drawing shows the whole scene; its 8 steps are listed below.

Every step as text
  1. Locker 1. The program moves 1 to LK-SLOT, the RELATIVE KEY, and runs WRITE. The record AC0001 since 1001 goes into slot 1, with status 00. No field of the record says which slot it is in.
  2. Lockers 4 and 7. Two more WRITEs put AC0004 in slot 4 and AC0007 in slot 7. Slots 2, 3, 5 and 6 stay empty, because a relative file may leave gaps between the slots it uses.
  3. Locker 2, below the others. AC0002 goes into slot 2, below slots already in use. In random or dynamic access the order of the WRITEs does not matter.
  4. A taken slot. A WRITE of AC0009 into slot 4 finds AC0004 there, so it places nothing and returns 22, the invalid key condition for a slot in use.
  5. An empty slot. A READ of slot 3 finds no record there and returns 23, as a READ of a missing key does in an indexed file.
  6. Locker 2 released. DELETE with LK-SLOT 2 removes AC0002 and returns 00. Slot 2 is empty again and can take a new holder.
  7. No slot 0. Slots are numbered from 1. A READ with LK-SLOT 0 found nothing to read and returned 23.
  8. The lockers in use. START KEY IS NOT LESS THAN 1 and READ NEXT read slots 1, 4 and 7 and skip the empty ones, 2, 3, 5 and 6. The READ NEXT after slot 7 returns 10, and each READ NEXT puts its slot number in LK-SLOT.

A relative file: A file of numbered slots, ORGANIZATION IS RELATIVE, in which each record is stored and found by the number of its slot. On z/OS it is a VSAM relative record data set. uses the same statuses as an indexed file, 22 when the slot is taken and 23 when it is empty or does not exist, but it finds records another way:

What differs Indexed file Relative file
A record is found by a key field inside it the slot number in the RELATIVE KEY
A WRITE to a taken key or slot 22 22
A READ of a missing key or empty slot 23 23
READ NEXT reads records in key order full slots in number order

Choose a relative file when the records are numbered from 1 with few gaps, such as lockers or the days of a year. A key such as AC0001, which is not a small number, belongs in an indexed file.

VSAM keeps data in four organisations, and IDCAMS defines them

On z/OS a relative file is one of the four VSAM: Virtual Storage Access Method, the z/OS access method for key-sequenced, entry-sequenced, relative-record and linear data sets. COBOL's indexed and relative files are VSAM data sets on z/OS. More in “Indexed files: keys, access modes and INVALID KEY” organisations, and IBM’s Programming Guide pairs three of them with COBOL’s organizations:

  • A KSDS: A key-sequenced data set: a VSAM data set whose records are stored and found by a key field. A COBOL indexed file on z/OS is a KSDS. More in “Indexed files: keys, access modes and INVALID KEY” finds each record by a key field inside it; a COBOL indexed file is one.
  • An ESDS: An entry-sequenced data set: a VSAM data set that keeps its records in the order they were written. COBOL opens one as ORGANIZATION IS SEQUENTIAL. keeps records in the order they were written; COBOL opens it as a sequential file.
  • A RRDS: A relative record data set: a VSAM data set whose records are found by their relative record numbers. COBOL opens one as ORGANIZATION IS RELATIVE. finds each record by its relative record number: The number of a record's slot in a relative file or a VSAM relative record data set, counted from 1.; COBOL opens it as a relative file.
  • A linear data set: A VSAM data set that holds a stream of bytes rather than records, used heavily by Db2 and z/OS system functions. COBOL's file statements do not open one. holds bytes, not records, and COBOL’s file statements do not open it.

The four VSAM organisations differ first in how a program finds a record, so that is the first question to settle for a new file. IDCAMS: Access method services, the z/OS utility that a job step runs as PGM=IDCAMS. Its commands define, load, print and delete VSAM data sets, such as DEFINE CLUSTER, REPRO and PRINT., access method services, is the utility a job step runs to create these data sets, and IBM’s Programming Guide requires every VSAM data set to be defined and catalogued with it before a program uses it. Its DEFINE CLUSTER command creates a data set, REPRO copies records into it, and PRINT lists the records it holds. Answer two questions about some data in the table below to see which organisation fits:

Interactive · answer and compare Which VSAM organisation fits the data? Answer two questions about how the program finds its records and whether it removes them. An organisation that cannot do it is ruled out, with the reason.
How does the program find the record it needs?
Must the program remove records?

Answer the questions above: the table then marks what fits your case and says why the rest does not.

The four VSAM organisations against how a program finds, removes and opens records
Organisation Finds a record by Removing a record COBOL file statements
KSDS The COBOL file: ORGANIZATION IS INDEXED Good a key field in the record, such as AC-NUMBER Good DELETE by key Good READ, WRITE, REWRITE, DELETE and START
ESDS The COBOL file: ORGANIZATION IS SEQUENTIAL Mixed its place only: records are read in the order they were written Poor IBM: a record cannot be shortened, lengthened or deleted Mixed sequential READ and WRITE; REWRITE at the same length
RRDS The COBOL file: ORGANIZATION IS RELATIVE Good its relative record number, the slot Good DELETE empties the slot for a new record Good READ, WRITE, REWRITE, DELETE and START
LDS The COBOL file: no COBOL organization Poor no records: a byte stream Poor no records to remove Poor none: it needs control interval access
How the answers decide
  • “How does the program find the record it needs?” By a key field in the record: rules out ESDS, RRDS and LDS. Only a KSDS finds a record by a field inside it. An RRDS finds a record by its slot number and an ESDS by its place in the file.
  • “How does the program find the record it needs?” By a number such as a locker: rules out ESDS and LDS and adds a caveat to KSDS. An RRDS goes straight to slot n. A KSDS can hold the number as its key too, at the cost of an index to keep.
  • “How does the program find the record it needs?” Only in the order records were written: rules out LDS and adds a caveat to KSDS and RRDS. An ESDS keeps the order records were written in. A KSDS or an RRDS can be read in order too, but carries a key or slot the data does not need.
  • “Must the program remove records?” Yes: rules out ESDS and LDS. IBM says that once a record is in an ESDS it cannot be shortened, lengthened or deleted, and an LDS has no records.

Checkpoint

Check: Slots, the RELATIVE KEY and statuses 22 and 23 · The four VSAM organisations and IDCAMS

  1. Question 1 of 2 After the run of lockers.cob, a program moves 2 to LK-SLOT and runs WRITE for AC0008. What does it return?

    Choose one answer.

    A hint

    Look at what the DELETE did to slot 2.

    Show the answer: Question 1 of 2

    Answer: 00, because the DELETE left slot 2 empty and a WRITE may fill an empty slot

    A relative file's slots are filled and emptied by number. WRITE needs an empty slot, READ and DELETE a full one, and the statuses 22 and 23 say which rule a statement broke.

  2. Question 2 of 2 A branch keeps a log of every cash transaction, read back only in the order it was written and never changed or removed. Which VSAM organisation fits, and how does COBOL open it?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: An ESDS, opened as ORGANIZATION IS SEQUENTIAL

    Choose the organisation by how records are found: by a key field (KSDS), by a number (RRDS) or only in the order written (ESDS). An LDS is not a COBOL file at all.

Practise: slots and organisations

These questions use the locker run and the four organisations.

Practice that adapts to you

Practise this lesson

4 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 4 Where does lockers.cob declare LK-SLOT, its RELATIVE KEY?

    Choose one answer.

    Show the answer: Question 1 of 4

    Answer: In WORKING-STORAGE, outside the record

    A relative file's key is the number of the slot, not data in the record. RELATIVE KEY names the item that carries it, and in the run it held 1, 4, 7 and 2 before the WRITEs.

  2. Question 2 of 4 A program must print every locker from 1 to 7, the empty ones as EMPTY. Why can a START and READ NEXT loop alone not do it?
    Show the answer: Question 2 of 4

    Answer: READ NEXT skips empty slots, so the loop sees only 1, 4 and 7; read each slot by number instead, and print EMPTY on 23

    READ NEXT reads records, not slots. A report of every slot reads each number from 1 to the last with a random READ, and treats 23 as an empty slot.

  3. Question 3 of 4 Which pairs of a COBOL ORGANIZATION and a VSAM data set does IBM give?

    Choose one answer.

    Show the answer: Question 3 of 4

    Answer: INDEXED with KSDS, SEQUENTIAL with ESDS, RELATIVE with RRDS

    KSDS for indexed files, ESDS for sequential files and RRDS for relative files; the fourth organisation, LDS, has no COBOL file organization.

  4. Question 4 of 4 A new program opens a VSAM relative file on z/OS for the first time and gets a failing status, although its SELECT entry and the DD statement are right. What is the first thing to look for?

    Choose one answer.

    Show the answer: Question 4 of 4

    Answer: Whether an IDCAMS step defined the data set, since VSAM data sets must be defined before a program uses them

    On z/OS the data set comes first: IDCAMS defines it with DEFINE CLUSTER, and only then can a COBOL program open it, load it or read it.

A relative file on disk: slot bytes, what a DELETE leaves, and a key too small Pro: opens with a Pro, Premium or Ultimate pass

Every slot of a relative file sits at a fixed place in the file, which is what makes slot n quick to find. This part reads the locker file byte by byte, shows what a DELETE leaves behind, and loads eleven lockers through a key of one digit.

In this part:

  • The locker file byte by byte, with the numbers worked out
  • What a DELETE leaves in the slot, and a release that erases it
  • A RELATIVE KEY too small for its slots, on z/OS and in GnuCOBOL 3.2

4 runnable examples (COB, Python) · about 6 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

IDCAMS steps read line by line: DEFINE CLUSTER, REPRO and PRINT Pro: opens with a Pro, Premium or Ultimate pass

An IDCAMS step decides everything VSAM knows about a data set before a COBOL program opens it. This part reads a step that defines, loads and prints the account file of the indexed-files lesson, checks its input the way REPRO needs it, and defines the locker file as an RRDS.

In this part:

  • DEFINE CLUSTER for the account file, keyword by keyword
  • REPRO needs its input in key order, so check it first
  • The locker file as an RRDS, and the organisations COBOL can open

1 diagram · 3 runnable examples (JCL, Python) · about 6 minutes

Opening this part with your pass…Pro part. It opens with the same pass as the part above.

Interview questions

A warm-up with its answer, then three questions about relative files and VSAM loads that went wrong:

  1. Fresher to mid · Warm-up

    What is a relative file, and where does its RELATIVE KEY live?

    A relative file stores each record in a numbered slot, and the slot number is the record's key. The RELATIVE KEY is an item in WORKING-STORAGE, not a field of the record: before a WRITE, READ or DELETE the program moves the slot number into it, and a READ NEXT puts there the number of the slot it read. A WRITE to a slot that holds a record returns 22, a READ or DELETE of an empty slot returns 23, slots count from 1, and READ NEXT skips the empty slots. It suits records numbered from 1 with few gaps, such as lockers, and on z/OS it is a VSAM relative record data set.

  2. Mid to senior

    Customers' account numbers must not stay in the locker file once a locker is returned. A program releases lockers with DELETE on a relative file that is backed up every night. How do you make sure the number is gone, and how do you prove it?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  3. Fresher to mid

    A nightly job reloads the locker file with sequential WRITEs and prints each record's RELATIVE KEY, declared PIC 9(2), as the locker number. The branch will grow from 80 to 150 lockers. What can go wrong on z/OS and with GnuCOBOL 3.2, and what do you change?

    Model answer, Pro. First step and follow-ups

  4. Mid to senior

    An IDCAMS step loads a new account KSDS with REPRO from an unsorted extract and ends with error messages, and the next steps find far fewer accounts than the extract held. What went wrong, how do you prepare the input, and how does the job check the load?

    Model answer, Pro. First step and follow-ups

Organisations named on this page

The VSAM, IDCAMS and file status rules here are taken from IBM’s Enterprise COBOL, DFSMS and Basic Skills pages, and the locker runs from GnuCOBOL 3.2. Neither IBM nor the GnuCOBOL project has anything to do with MySmartCoPilot.

Flashcards · 6 cards

Recall it, then check

Say the answer to yourself before you look: recalling it is what makes it stick.

  1. What is the RELATIVE KEY of a relative file?

    A number in WORKING-STORAGE, not a field of the record. WRITE, READ and DELETE take their slot from it, and READ NEXT puts there the number of the slot it read.

  2. Which statuses does a relative file give for a taken slot and an empty one?

    22 for a WRITE to a slot already in use; 23 for a READ of an empty slot, or of slot 0, since slots count from 1.

  3. Which slots does READ NEXT read in a relative file?

    Only the full ones, in order of their numbers; empty slots are skipped.

  4. When does a relative file suit data better than an indexed one?

    When the records are numbered from 1 with few gaps, such as lockers or the days of a year. A key such as AC0001, which is no small number, belongs in an indexed file.

  5. Which VSAM organisation holds each kind of COBOL file?

    An indexed file is a KSDS, a sequential one an ESDS and a relative one an RRDS. A linear data set holds bytes, which COBOL's file statements do not open.

  6. What do IDCAMS's DEFINE CLUSTER, REPRO and PRINT do?

    DEFINE CLUSTER creates a VSAM data set, which must exist before a program opens it; REPRO copies records into it; PRINT lists the records it holds.

Key takeaways

  • A relative file keeps each record in the slot its RELATIVE KEY names, a number outside the record: a taken slot returns 22, an empty slot or slot 0 returns 23, and READ NEXT skips the empty slots.
  • On z/OS, COBOL’s indexed, sequential and relative files are VSAM KSDS, ESDS and RRDS data sets, and a linear data set holds bytes that COBOL’s file statements do not open.
  • IDCAMS defines a VSAM data set before any program opens it: DEFINE CLUSTER creates it, REPRO copies records into it, and PRINT lists them.

Terms in this lesson

control interval
The unit in which VSAM stores data: records, free space and descriptor fields together, 4K bytes by default and up to 32K. Control intervals are grouped into control areas.
ESDS
An entry-sequenced data set: a VSAM data set that keeps its records in the order they were written. COBOL opens one as ORGANIZATION IS SEQUENTIAL.
IDCAMS
Access method services, the z/OS utility that a job step runs as PGM=IDCAMS. Its commands define, load, print and delete VSAM data sets, such as DEFINE CLUSTER, REPRO and PRINT.
linear data set
A VSAM data set that holds a stream of bytes rather than records, used heavily by Db2 and z/OS system functions. COBOL's file statements do not open one.
relative file
A file of numbered slots, ORGANIZATION IS RELATIVE, in which each record is stored and found by the number of its slot. On z/OS it is a VSAM relative record data set.
RELATIVE KEY
The item named by RELATIVE KEY: a number outside the record that tells a relative-file statement which slot to use. A READ NEXT puts the number of the slot it read there.
relative record number
The number of a record's slot in a relative file or a VSAM relative record data set, counted from 1.
RRDS
A relative record data set: a VSAM data set whose records are found by their relative record numbers. COBOL opens one as ORGANIZATION IS RELATIVE.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.