Record layouts in COBOL define the structure and organization of data within file records. They specify field names, data types (PIC clauses), field sizes, positions, and hierarchical organization. Understanding record layouts is essential for designing file structures, ensuring data integrity, accessing fields correctly, and maintaining compatibility with existing systems in mainframe COBOL programs.
A record layout defines:
Record layouts are defined in the FILE SECTION and determine how data is stored and accessed.
Basic record layout structure:
123456789DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(50). 05 CUSTOMER-BALANCE PIC 9(8)V99. 05 FILLER PIC X(35). *> Padding to fixed length
This defines:
Level numbers create hierarchical structures:
123456789101101 EMPLOYEE-RECORD. *> Level 01: Record 05 EMPLOYEE-ID PIC 9(6). *> Level 05: Field 05 EMPLOYEE-NAME. *> Level 05: Group 10 FIRST-NAME PIC X(15). *> Level 10: Field in group 10 LAST-NAME PIC X(15). *> Level 10: Field in group 05 EMPLOYEE-ADDRESS. *> Level 05: Group 10 STREET PIC X(30). *> Level 10: Field in group 10 CITY PIC X(20). *> Level 10: Field in group 10 STATE PIC X(2). *> Level 10: Field in group 10 ZIP-CODE PIC 9(5). *> Level 10: Field in group 05 EMPLOYEE-SALARY PIC 9(7)V99. *> Level 05: Field
Level number rules:
Fields are defined with PIC clauses:
1234567801 EXAMPLE-RECORD. 05 NUMERIC-FIELD PIC 9(5). *> 5 digits 05 ALPHANUMERIC PIC X(30). *> 30 characters 05 DECIMAL-FIELD PIC 9(8)V99. *> 8 digits, 2 decimal places 05 SIGNED-NUMERIC PIC S9(6). *> Signed 6-digit number 05 EDITED-NUMERIC PIC $,$$$,$$9.99. *> Edited with $ and commas 05 DATE-FIELD PIC 9(8). *> Date (YYYYMMDD) 05 TIME-FIELD PIC 9(6). *> Time (HHMMSS)
PIC clause meanings:
Hierarchical structures organize related fields:
12345678910111213141501 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(6). 05 EMPLOYEE-NAME. 10 FIRST-NAME PIC X(15). 10 MIDDLE-INIT PIC X(1). 10 LAST-NAME PIC X(15). 05 EMPLOYEE-ADDRESS. 10 STREET-ADDRESS. 15 STREET-NUMBER PIC X(10). 15 STREET-NAME PIC X(20). 10 CITY PIC X(20). 10 STATE PIC X(2). 10 ZIP-CODE PIC 9(5). 05 EMPLOYEE-SALARY PIC 9(7)V99. 05 EMPLOYEE-DEPARTMENT PIC X(20).
This hierarchical structure:
Most COBOL records are fixed-length:
123456701 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). *> 5 bytes 05 CUSTOMER-NAME PIC X(30). *> 30 bytes 05 CUSTOMER-ADDRESS PIC X(50). *> 50 bytes 05 CUSTOMER-BALANCE PIC 9(8)V99. *> 8 bytes (V doesn't count) 05 FILLER PIC X(39). *> 39 bytes padding *> Total: 132 bytes (fixed length)
Fixed-length records:
FILLER creates unnamed fields for padding and alignment:
123456789101112131415161701 REPORT-RECORD. 05 REPORT-DATE PIC X(10). *> 10 bytes 05 FILLER PIC X(2) VALUE SPACES. *> 2 bytes padding 05 REPORT-TITLE PIC X(40). *> 40 bytes 05 FILLER PIC X(80). *> 80 bytes reserved for future *> Total: 132 bytes *> FILLER for alignment 01 ALIGNED-RECORD. 05 FIELD-1 PIC X(10). 05 FILLER PIC X(2). *> Align to word boundary 05 FIELD-2 PIC X(20). *> FILLER with initial value 01 INITIALIZED-RECORD. 05 DATA-FIELD PIC X(30). 05 FILLER PIC X(102) VALUE SPACES. *> Initialize to spaces
FILLER is useful for:
REDEFINES provides different views of the same storage:
1234567891011121314151617181901 DATA-RECORD. 05 RAW-DATA PIC X(20). 05 PARSED-DATA REDEFINES RAW-DATA. 10 FIELD-1 PIC X(5). 10 FIELD-2 PIC X(10). 10 FIELD-3 PIC X(5). *> Usage: PROCEDURE DIVISION. MAIN-PARA. *> Read as raw data READ FILE INTO RAW-DATA *> Access as parsed fields DISPLAY "Field 1: " FIELD-1 DISPLAY "Field 2: " FIELD-2 DISPLAY "Field 3: " FIELD-3 STOP RUN.
REDEFINES allows:
Record length is the sum of all field lengths:
123456701 EXAMPLE-RECORD. 05 FIELD-1 PIC 9(5). *> 5 bytes 05 FIELD-2 PIC X(30). *> 30 bytes 05 FIELD-3 PIC 9(8)V99. *> 8 bytes (V doesn't count) 05 FIELD-4 PIC X(20). *> 20 bytes 05 FILLER PIC X(69). *> 69 bytes *> Total: 5 + 30 + 8 + 20 + 69 = 132 bytes
Length calculation rules:
1234567*> Simple flat structure - all fields at same level 01 SIMPLE-RECORD. 05 ID PIC 9(5). 05 NAME PIC X(30). 05 AMOUNT PIC 9(8)V99. 05 DATE-FIELD PIC 9(8). 05 FILLER PIC X(81).
123456789101112*> Grouped structure - related fields together 01 GROUPED-RECORD. 05 RECORD-ID PIC 9(5). 05 NAME-GROUP. 10 FIRST-NAME PIC X(15). 10 LAST-NAME PIC X(15). 05 ADDRESS-GROUP. 10 STREET PIC X(30). 10 CITY PIC X(20). 10 STATE PIC X(2). 05 AMOUNT-FIELD PIC 9(8)V99. 05 FILLER PIC X(45).
1234567891011*> Handle different record types with REDEFINES 01 INPUT-RECORD PIC X(100). 01 HEADER-RECORD REDEFINES INPUT-RECORD. 05 REC-TYPE PIC X VALUE 'H'. 05 HEADER-DATA PIC X(99). 01 DETAIL-RECORD REDEFINES INPUT-RECORD. 05 REC-TYPE PIC X VALUE 'D'. 05 DETAIL-DATA PIC X(99). 01 TRAILER-RECORD REDEFINES INPUT-RECORD. 05 REC-TYPE PIC X VALUE 'T'. 05 TRAILER-DATA PIC X(99).
Follow these best practices:
Avoid these common mistakes:
1234567891011*> WRONG: Record length doesn't match file definition FD CUSTOMER-FILE RECORD CONTAINS 132 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). *> Only 5 bytes! *> CORRECT: Match file definition 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 FILLER PIC X(97). *> Total: 132 bytes
1234567891011*> WRONG: Variable length 01 RECORD. 05 FIELD-1 PIC X(10). 05 FIELD-2 PIC X(20). *> Total: 30 bytes, but file expects 132 *> CORRECT: Fixed length with FILLER 01 RECORD. 05 FIELD-1 PIC X(10). 05 FIELD-2 PIC X(20). 05 FILLER PIC X(102). *> Pad to 132 bytes
Think of record layouts like a form with boxes:
So record layouts are like form templates that show exactly where each piece of information goes and how big each box is!
Complete these exercises to reinforce your understanding:
Design a record layout for a product file with product ID (5 digits), product name (30 chars), price (8 digits, 2 decimals), and quantity (5 digits). Make it 132 bytes total using FILLER.
Create a hierarchical record layout for an employee file with employee ID, name (first, middle initial, last), address (street, city, state, zip), and salary. Use groups to organize related fields.
Calculate the total length of a record with: ID (PIC 9(5)), Name (PIC X(30)), Amount (PIC 9(8)V99), Date (PIC 9(8)), and determine how much FILLER is needed to make it 132 bytes.
Create a record layout that uses REDEFINES to view a 20-character field both as raw data and as three separate fields (5, 10, 5 characters).
Create a complete file definition including SELECT statement, FD, and record layout for a customer file with ID, name, address, and balance. Ensure the record is exactly 132 bytes.
1. What level number is used for the record itself?
2. What is FILLER used for?
3. How do you create hierarchical record structures?
4. What does REDEFINES do?
5. What is the length of a PIC X(30) field?
6. Where are record layouts defined?