MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Record Layouts

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.

What is a Record Layout?

A record layout defines:

  • Field structure: What fields are in the record and their order
  • Data types: PIC clauses specifying format and size
  • Field positions: Where each field is located in the record
  • Record length: Total size of the record
  • Hierarchy: How fields are grouped and organized
  • Alignment: How fields are positioned and padded

Record layouts are defined in the FILE SECTION and determine how data is stored and accessed.

Basic Record Layout Definition

Basic record layout structure:

cobol
1
2
3
4
5
6
7
8
9
DATA 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 01: The record itself (CUSTOMER-RECORD)
  • Level 05: Individual fields within the record
  • PIC clauses: Data type and size for each field
  • FILLER: Padding to achieve fixed record length

Level Numbers and Hierarchy

Level numbers create hierarchical structures:

Understanding Level Numbers

cobol
1
2
3
4
5
6
7
8
9
10
11
01 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:

  • Level 01: Record or highest-level item
  • Level 05-49: Fields and groups within records
  • Lower numbers = higher level: Level 05 is higher than level 10
  • Groups contain fields: A group at level 05 contains fields at level 10

Field Definitions

Fields are defined with PIC clauses:

Common PIC Clauses

cobol
1
2
3
4
5
6
7
8
01 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:

  • PIC 9(n): Numeric field, n digits
  • PIC X(n): Alphanumeric field, n characters
  • PIC 9(n)V99: Numeric with implied decimal (V doesn't take storage)
  • PIC S9(n): Signed numeric field
  • PIC with editing: Formatted display ($, commas, etc.)

Hierarchical Record Structures

Hierarchical structures organize related fields:

Example: Employee Record with Groups

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
01 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:

  • Groups related fields: Name fields together, address fields together
  • Improves readability: Makes record structure clearer
  • Enables group operations: Can MOVE entire groups
  • Reflects data relationships: Shows which fields belong together

Fixed-Length Records

Most COBOL records are fixed-length:

cobol
1
2
3
4
5
6
7
01 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:

  • Same size for all records: Every record is exactly the same length
  • Efficient random access: Easy to calculate record positions
  • Use FILLER for padding: Pad to desired fixed length
  • Simpler processing: No need to handle variable lengths

Using FILLER

FILLER creates unnamed fields for padding and alignment:

FILLER Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
01 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:

  • Padding: Making records fixed length
  • Reserved space: Space for future fields
  • Alignment: Aligning fields to specific boundaries
  • Separators: Creating visual separation (though spaces are more common)

REDEFINES Clause

REDEFINES provides different views of the same storage:

REDEFINES Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
01 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:

  • Multiple views: Same data viewed in different ways
  • Parsing: Breaking down raw data into fields
  • Type conversion: Viewing data as different types
  • Flexibility: Handling different record formats in same area

Calculating Record Length

Record length is the sum of all field lengths:

Length Calculation Example

cobol
1
2
3
4
5
6
7
01 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:

  • PIC X(n): n bytes
  • PIC 9(n): n bytes
  • PIC 9(n)V99: n bytes (V is implied decimal, no storage)
  • PIC S9(n): n bytes (sign stored separately if needed)
  • Add all fields: Sum all field lengths for total

Record Layout Design Patterns

Pattern 1: Flat Record Structure

cobol
1
2
3
4
5
6
7
*> 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).

Pattern 2: Grouped Record Structure

cobol
1
2
3
4
5
6
7
8
9
10
11
12
*> 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).

Pattern 3: Multiple Record Types

cobol
1
2
3
4
5
6
7
8
9
10
11
*> 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).

Best Practices for Record Layouts

Follow these best practices:

  • Use meaningful names: Name fields and groups descriptively
  • Group related fields: Use hierarchical structures to organize related data
  • Document layouts: Add comments explaining field purposes and formats
  • Maintain fixed length: Use FILLER to ensure fixed record lengths
  • Calculate lengths: Verify record length matches file definition
  • Use appropriate PIC clauses: Choose correct data types and sizes
  • Plan for growth: Reserve space (FILLER) for future fields if needed
  • Validate compatibility: Ensure layouts match existing file formats
  • Test field access: Verify you can access all fields correctly
  • Consider alignment: Align fields appropriately for performance if needed

Common Record Layout Mistakes

Avoid these common mistakes:

Mistake 1: Incorrect Record Length

cobol
1
2
3
4
5
6
7
8
9
10
11
*> 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

Mistake 2: Missing FILLER for Fixed Length

cobol
1
2
3
4
5
6
7
8
9
10
11
*> 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

Explain Like I'm 5: Record Layouts

Think of record layouts like a form with boxes:

  • Record layout is like the form template - it shows where each piece of information goes
  • Fields are like the boxes on the form - each box has a label (field name) and a size
  • Level numbers are like sections on the form - some boxes are grouped together in sections
  • FILLER is like blank space on the form - it's there but you don't write anything in it
  • Fixed length means every form is exactly the same size - all forms have the same boxes in the same places

So record layouts are like form templates that show exactly where each piece of information goes and how big each box is!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Basic Record Layout

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.

Exercise 2: Hierarchical Layout

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.

Exercise 3: Record Length Calculation

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.

Exercise 4: REDEFINES Usage

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).

Exercise 5: Complete File Definition

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.

Test Your Knowledge

1. What level number is used for the record itself?

  • Level 05
  • Level 01
  • Level 10
  • Level 77

2. What is FILLER used for?

  • Required fields
  • Fields that are not referenced by name (padding, reserved space)
  • Key fields
  • Numeric fields only

3. How do you create hierarchical record structures?

  • Using only level 01
  • Using level numbers to create groups and subgroups
  • Using FILLER only
  • Using REDEFINES only

4. What does REDEFINES do?

  • Creates a new field
  • Provides different views of the same storage area
  • Deletes a field
  • Renames a field

5. What is the length of a PIC X(30) field?

  • 30 bytes
  • 30 digits
  • 30 bits
  • Depends on the data

6. Where are record layouts defined?

  • WORKING-STORAGE SECTION
  • FILE SECTION under FD
  • PROCEDURE DIVISION
  • ENVIRONMENT DIVISION

Related Concepts

Related Pages