COBOL copybook example: every line explained
A copybook is the record layout of a file: the name, type and length of each field, in order. Here is a complete one, the offset and length of each field, and how a real record reads with it. The data is fictional.
The copybook
A customer record of 72 bytes, in fixed format: columns 1 to 6 hold a sequence number, column 7 an asterisk for a comment, and the code sits in columns 8 to 72.
000100 01 CUST-REC. 000200 05 REC-TYPE PIC X. 000300 88 REC-HEADER VALUE 'H'. 000400 88 REC-DETAIL VALUE 'D'. 000500 05 CUST-ID PIC 9(8). 000600* CUSTOMER NAME, BLANK-PADDED 000700 05 CUST-NAME PIC X(20). 000800 05 BALANCE PIC S9(7)V99 COMP-3. 000900 05 OPEN-DATE. 001000 10 OPEN-YYYY PIC 9(4). 001100 10 OPEN-MM PIC 99. 001200 10 OPEN-DD PIC 99. 001300 05 OPEN-DATE-N REDEFINES OPEN-DATE PIC 9(8). 001400 05 TX-COUNT PIC S9(4) COMP. 001500 05 RATE COMP-2. 001600 05 MONTHLY PIC S9(5) OCCURS 3 TIMES. 001700 05 FILLER PIC X(5).
- 01 CUST-REC is the record. 05 and 10 are levels: a 10 belongs to the 05 above it.
- PIC X(20) is 20 characters of text. PIC 9(8) is 8 digits stored as characters (zoned), one byte each.
- PIC S9(7)V99 COMP-3 is a signed packed decimal with 7 digits before an implied decimal point and 2 after: 5 bytes. V does not take a byte.
- PIC S9(4) COMP is a binary halfword: 2 bytes, big-endian, two's complement.
- COMP-2 has no PIC: an 8-byte floating-point number in IBM hexadecimal format.
- 88 REC-HEADER VALUE 'H' gives a name to a value of REC-TYPE; it takes no byte.
- FILLER is space that no program names, often padding to the record length.
Offsets and lengths
Each field starts where the previous one ends. This is the table that Mainframe: Show copybook offsets and lengths gives for this copybook (position from 1):
Field Position Length REC-TYPE 1 1 CUST-ID 2 8 CUST-NAME 10 20 BALANCE 30 5 COMP-3 OPEN-DATE 35 8 group OPEN-YYYY 35 4 OPEN-MM 39 2 OPEN-DD 41 2 OPEN-DATE-N 35 8 REDEFINES OPEN-DATE TX-COUNT 43 2 COMP RATE 45 8 COMP-2 MONTHLY 53 5 x 3 (OCCURS) FILLER 68 5 Total 72
If the total does not match the record length of the file, the copybook is not the one that wrote the file. The extension says so: “The copybook describes 80 bytes, records have only 72: fields after byte 72 are empty.”
REDEFINES and OCCURS
OPEN-DATE-N REDEFINES OPEN-DATE: the same 8 bytes, read two ways. As a group they give 2026, 11 and 07; as one PIC 9(8) they give 20261107. A REDEFINES takes no extra byte, which is why OPEN-DATE-N and OPEN-DATE both start at position 35. The viewer decodes every view on the same bytes in the record detail.
MONTHLY PIC S9(5) OCCURS 3 TIMES is a table of three zoned numbers of 5 bytes, at positions 53, 58 and 63. In the first record of the test file they hold F0F0F1F0C0, F0F0F0F0D5 and F0F0F0F0F7: 100, -5 and 7. The sign of a zoned number is the first half of its last byte (C positive, D negative).
OCCURS DEPENDING ON
When the number of entries changes from record to record, the table depends on a count field. Records then have different lengths and the file is usually variable (RECFM=VB):
01 ORDER-REC.
05 ORDER-ID PIC 9(6).
05 LINE-COUNT PIC 9(2).
05 LINES OCCURS 1 TO 10 TIMES DEPENDING ON LINE-COUNT.
10 SKU PIC X(8).
10 QTY PIC S9(5) COMP-3.Each LINES entry is 11 bytes (SKU 8, QTY 3 as COMP-3). With LINE-COUNT at 2, the record is 8 + 2 × 11 = 30 bytes; the largest possible record, with 10 lines, is 118. In the free version of the extension, the fields before the table are decoded; Pro cuts the table at its real count and flags a count outside 1 to 10 as “OCCURS DEPENDING ON count out of range”.
Several 01 levels in one copybook
A file with a header, details and a trailer has one copybook with one 01 per record type, here with the customer layout brought in by COPY:
01 HEADER-REC.
05 H-TYPE PIC X.
05 H-FILE-DATE PIC 9(8).
05 H-SOURCE PIC X(21).
COPY CUST.
01 TRAILER-REC.
05 T-TYPE PIC X.
05 T-COUNT PIC 9(7).
05 T-TOTAL PIC S9(11)V99 COMP-3.Which layout applies to a record is decided by a field, usually the first byte (H-TYPE, REC-TYPE, T-TYPE). Free: one layout for the whole file, and the count of records of each type; Pro reads each record with the layout chosen by that field value.
Frequently asked questions
What is a copybook in COBOL?
A source file, usually with the .cpy extension, that holds a record layout and is brought into programs with a COPY statement. Every program that reads or writes the file uses the same copybook, so it is also the best documentation of the file.
How long is a COMP-3 field?
Half a byte per digit plus half a byte for the sign, rounded up: PIC S9(7)V99 has 9 digits, so 5 bytes; PIC S9(5) has 5 digits, so 3 bytes.
Can I read a data file with its copybook without a mainframe?
Yes. Copy the file in binary mode and the copybook to your computer, open the file in VS Code with Mainframe File Viewer and pick the copybook: each record is shown field by field, with the hex next to each value.
Read your own files the same way
Mainframe File Viewer for VS Code reads your EBCDIC files with their copybook, on your machine, with the hex next to every value. Free, no account; Pro adds several layouts per file, VB and OCCURS DEPENDING ON cut into fields, comparison details and CSV and JSON export.
A format or an in-house rule of your own? We build it in five working days, at a fixed price: EUR 490 + VAT where applicable, per work package. See the work packages