COBOL

COBOL REDEFINES for Mixed Record Types: Validate the Record Code Before Reading an Overlay

A mixed-record interface can contain a header, many details, and a trailer in one sequential file. `REDEFINES` makes each layout available, but it does not tell COBOL which layout is present. This tutorial shows a safe way to classify the record first, then use the appropriate overlay without turning a malformed header or trailer into a business transaction.

InputValidationProcessingOutput
Original DBala learning flow — a visual summary of this article’s practical decision path.

A mixed-record interface can contain a header, many details, and a trailer in one sequential file. REDEFINES makes each layout available, but it does not tell COBOL which layout is present. This tutorial shows a safe way to classify the record first, then use the appropriate overlay without turning a malformed header or trailer into a business transaction.

The record names, layouts, and values below are fictional. File standards, character sets, copybook conventions, and error-handling rules vary by site. Do not change production parsing logic or rerun a failed feed until the actual input, reject handling, and downstream effects have been reviewed.

The problem is not REDEFINES—it is an unchecked assumption

A file interface often uses the first byte as a record code:

All records may have the same physical length, which makes a single 01 record with several REDEFINES convenient. The dangerous shortcut is to reference DETAIL-AMOUNT before confirming that the record is a D. COBOL will not reject that reference merely because the current bytes are a header. It will interpret whatever happens to be in the same positions as an amount.

That can produce a misleading symptom: a job completes with a count or total that is wrong, rather than failing at the record that caused the problem. It can also create a false diagnosis that a numeric field is corrupt when the program was simply reading the wrong layout.

The operational question is therefore narrow: what evidence proves that a record was classified before its overlay was used?

A compact layout for a fixed-length feed

The following example uses 80-byte records. The common record area contains only the code. Each specific layout overlays the full record. This is an educational pattern, not a universal copybook design.

       FD  IN-FILE.
       01  IN-RECORD                 PIC X(80).

WORKING-STORAGE SECTION.
01 WS-RECORD-VIEW.
05 WS-REC-CODE PIC X.
05 FILLER PIC X(79).

01 WS-HEADER REDEFINES WS-RECORD-VIEW.
05 HDR-REC-CODE PIC X.
05 HDR-SENDER-ID PIC X(8).
05 HDR-BUSINESS-DATE PIC 9(8).
05 FILLER PIC X(63).

01 WS-DETAIL REDEFINES WS-RECORD-VIEW.
05 DTL-REC-CODE PIC X.
05 DTL-ACCOUNT-NO PIC X(12).
05 DTL-AMOUNT PIC S9(7)V99 COMP-3.
05 DTL-CURRENCY PIC X(3).
05 FILLER PIC X(58).

01 WS-TRAILER REDEFINES WS-RECORD-VIEW.
05 TRL-REC-CODE PIC X.
05 TRL-DETAIL-COUNT PIC 9(7).
05 TRL-TOTAL-AMOUNT PIC S9(9)V99 COMP-3.
05 FILLER PIC X(67).
```

The physical input record and the working-storage view are separate deliberately. After a successful READ, move the bytes into the view. That makes the point at which the program interprets the record visible in the code and gives a natural place to initialize validation flags or capture a reject copy.

       READ IN-FILE
           AT END
               MOVE 'Y' TO WS-END-OF-FILE
           NOT AT END
               MOVE IN-RECORD TO WS-RECORD-VIEW
               PERFORM PROCESS-RECORD
       END-READ

REDEFINES consumes no extra storage for each alternative view. It is an overlay, not a conversion. In particular, DTL-AMOUNT does not become a valid packed-decimal field simply because the program names it. Its bytes must come from a detail record whose field definition and encoding agree with the sender contract.

Classify first, then validate the relevant fields

A useful processing paragraph makes record type the gate. It should not calculate totals, update DB2, or write output until classification succeeds.

       PROCESS-RECORD.
           EVALUATE WS-REC-CODE
               WHEN 'H'
                   PERFORM VALIDATE-HEADER
                   IF WS-HEADER-VALID
                       ADD 1 TO WS-HEADER-COUNT
                   ELSE
                       PERFORM WRITE-REJECT
                   END-IF

WHEN 'D'
PERFORM VALIDATE-DETAIL
IF WS-DETAIL-VALID
ADD 1 TO WS-DETAIL-COUNT
ADD DTL-AMOUNT TO WS-CALCULATED-AMOUNT
PERFORM APPLY-DETAIL
ELSE
PERFORM WRITE-REJECT
END-IF

WHEN 'T'
PERFORM VALIDATE-TRAILER
IF WS-TRAILER-VALID
MOVE 'Y' TO WS-TRAILER-SEEN
ELSE
PERFORM WRITE-REJECT
END-IF

WHEN OTHER
PERFORM WRITE-REJECT
END-EVALUATE.
```

This code has two distinct decisions. First, EVALUATE chooses the layout from the one-byte common field. Second, the validation paragraph for that type decides whether its business fields are usable. For example, VALIDATE-DETAIL might check that the account identifier is populated, the currency is in the interface's permitted set, and the packed amount is valid under the site's numeric-data rules. VALIDATE-TRAILER should compare its claimed totals only after the entire file is read.

An implementation may reject immediately, collect all bad records, or stop on the first structural error. That is a business and recovery decision. The code should make that decision explicit; silently skipping an unknown record can leave a partial file looking complete.

A small decision table prevents accidental detail processing

| Record code | Allowed position | Overlay permitted | Immediate action |
|---|---|---|---|
| H | First logical record, once | Header | Check sender/date; establish file context |
| D | After valid header and before trailer | Detail | Validate and process or reject |
| T | After at least the allowed detail set, once | Trailer | Retain claimed controls for end-of-file comparison |
| Other, blank, or repeated control | Never | None beyond common view | Reject or stop under the interface rule |

Position checks matter as much as the code. A second header in the middle of a feed may have valid-looking date bytes, but it is normally a structural error. Likewise, a D after a trailer must not be processed just because the detail overlay looks valid.

A small state flag set is usually sufficient: header seen, trailer seen, detail count, calculated amount, and reject count. Keep any sequence or checkpoint fields required by the local recovery design separate from these simple file controls.

Failure cases that look similar but need different actions

A record has an unknown code. Preserve the record identifier, input sequence number, and code in the reject evidence. Do not reinterpret it as a detail. It may indicate a sender layout change, a code-page issue, or an incorrect file.

The code is D, but a field fails validation. This is a detail-level data failure, not proof that the whole file is the wrong format. The interface agreement determines whether to reject the record, suspend the feed, or request a corrected retransmission.

The trailer count differs from the calculated count. This is a file-level control failure. A completed program step is not proof that the file was fully and correctly applied. Do not release downstream output merely because every individual detail looked valid.

A decimal-data exception occurs while examining a detail. Capture the input record safely according to local data-protection rules, the program location, and the record classification path. Then determine whether the bytes were actually a detail field before concluding that the sender supplied invalid numeric data.

The program is restarted after a partial run. Classification does not solve duplicate-update risk. Establish whether valid details were committed, whether the program supports a documented checkpoint, and whether rejects or output datasets already exist. A restart point must be tied to an approved recovery design, not only to a record count.

What to verify before accepting or rerunning the feed

Practical takeaway

REDEFINES is a useful representation tool, not a record-type detector. Move the record into a common view, classify it from the agreed discriminator, validate only the chosen layout, and reconcile the file controls before treating the batch as successful. That small ordering discipline prevents a malformed control record from becoming an unintended transaction.

← Back to all articles