COBOL

COBOL OCCURS DEPENDING ON: Validate the Count Field Before Reading Variable Data

A variable-length COBOL record can look valid until an `OCCURS DEPENDING ON` count says there are more repeated items than the record actually contains. This tutorial shows how to validate that control field before using it for business processing, and how to investigate a bad input record without turning a batch rerun into duplicate work.

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

A variable-length COBOL record can look valid until an OCCURS DEPENDING ON count says there are more repeated items than the record actually contains. This tutorial shows how to validate that control field before using it for business processing, and how to investigate a bad input record without turning a batch rerun into duplicate work.

The record layout, field names, and values below are fictional. Compiler options, file formats, copybook standards, and recovery procedures vary by installation. Do not edit an inbound file, weaken a validation check, or rerun an updating step until the source, impact, and restart boundary are established.

The count is input, not a promise

OCCURS DEPENDING ON (ODO) lets a layout describe a variable number of repeating entries. It is useful for a message, a variable-detail extract, or an internal record assembled from an interface. But the count field arrives from somewhere: an input record, a prior program, or a caller. Treat it as untrusted until it passes the rules required by the layout.

Consider a fictional order record with space for up to five item entries:

       01  ORDER-RECORD.
           05  OR-ORDER-ID         PIC X(10).
           05  OR-ITEM-COUNT       PIC 9.
           05  OR-ITEM             OCCURS 1 TO 5 TIMES
                                  DEPENDING ON OR-ITEM-COUNT.
               10  OR-SKU          PIC X(8).
               10  OR-QTY          PIC 9(3).
           05  OR-TRAILER-CODE     PIC X(2).

The layout expresses two separate ideas. The maximum allocated area has room for five OR-ITEM occurrences. The current logical record says how many of those occurrences are meaningful. If OR-ITEM-COUNT contains 3, a loop should use entries 1 through 3, not all five. If it contains 0, 6, or a nonnumeric value in an external character record, the program needs a defined reject path rather than an assumption.

A dangerous support shortcut is to change the loop bound to five so the job gets past an abnormal end. That may read filler as a SKU, consume trailer bytes as detail, or make a malformed interface appear successful. It conceals the contract failure from the sender and can contaminate downstream updates.

Build the validation at the boundary

Validate the count before any subscripted reference, calculation, or update based on the repeated area. The exact coding style depends on the compiler and local standard, but the order matters: establish that the count is numeric and within the permitted bounds; only then iterate.

       IF OR-ITEM-COUNT NOT NUMERIC
           MOVE 'COUNT NOT NUMERIC' TO WS-REJECT-REASON
           PERFORM WRITE-REJECT-AND-STOP-RECORD
       ELSE
           IF OR-ITEM-COUNT < 1 OR OR-ITEM-COUNT > 5
               MOVE 'COUNT OUT OF RANGE' TO WS-REJECT-REASON
               PERFORM WRITE-REJECT-AND-STOP-RECORD
           ELSE
               PERFORM VARYING WS-IX FROM 1 BY 1
                 UNTIL WS-IX > OR-ITEM-COUNT
                   PERFORM VALIDATE-AND-PROCESS-ITEM
               END-PERFORM
           END-IF
       END-IF

This illustration assumes at least one item is required. Some interfaces legitimately allow zero items; if so, make zero an explicit accepted case rather than an accidental pass-through. The maximum should normally come from the interface contract or copybook design, not from a number copied into several programs. A named constant or shared definition helps prevent one consumer accepting seven entries while another accepts five.

NUMERIC is not a complete interface check. A count of 3 can still be inconsistent with the physical input length, trailer placement, or record type. Fixed-length records may carry a maximum-sized area where unused occurrences are blank; variable-length records may contain only the active occurrences. Know which format the producer promised before deciding how to validate length.

Worked diagnosis: count says four, record contains two

Assume a nightly input file carries variable-detail records. A program logs a reject for order R1045802: count is four, but the received record length supports only two complete item entries before the trailer. The immediate question is not whether COBOL can loop four times. It is whether producer and consumer agree on the record contract.

Capture a masked or synthetic representation of the structural facts, not customer data:

Record type: ORDER
Declared item count: 4
Received logical length: 36 bytes
Fixed header length: 11 bytes
One item length: 11 bytes
Trailer length: 2 bytes
Length needed for 4 items: 11 + (4 x 11) + 2 = 57 bytes
Length supports: 2 complete items

The arithmetic provides an evidence-based explanation. It can indicate truncation in transmission, a sender using an older item length, a malformed count, or a consumer using the wrong copybook version. It does not identify which explanation is true by itself.

Use a short diagnosis flow:

Count is valid and within maximum?
  No  -> reject with reason; preserve record identity and source evidence.
  Yes -> Does actual logical length support that many complete occurrences?
           No  -> quarantine/reject; compare producer format and transport evidence.
           Yes -> Does each active occurrence pass field validation?
                    No  -> reject according to the interface contract.
                    Yes -> process and reconcile the accepted record.

The logical length must be taken from the correct source. For a variable blocked sequential file, that may be the record length returned by the file-access method or an application-provided length field. For a fixed blocked file, it may instead be a defined record length. Do not invent a length formula from an editor display or use a block size as though it were one record’s length.

Failure patterns worth separating

| Observation | What it can mean | Safer next action |
| --- | --- | --- |
| Count is blank or contains a non-digit | Bad inbound data, wrong record type, or shifted layout | Reject with a structural reason; examine source and record classification. |
| Count exceeds the ODO maximum | Sender defect or incompatible version | Hold the record for interface review; do not extend the receiver limit casually. |
| Count is in range but logical length is too short | Truncation or a layout mismatch | Compare transport and producer/consumer version evidence. |
| Length is adequate but an item is invalid | Detail-level data-quality issue | Apply the agreed reject or exception process. |
| All records suddenly reject after a deployment | Active copybook or record version may differ | Prove the deployed build and interface version before changing data. |

Compiler bounds-checking options can help expose invalid subscripts during test, but they are not an input-control design. An out-of-range protection may stop a program after a bad record reaches processing; a deliberate validation path records why the record was rejected and prevents partial business work. Review compiler behavior for the installed product and options rather than relying on a generic expectation.

What to verify before rerun

A record-level reject is often recoverable without replaying an entire cycle. A failed job is different: it may have processed earlier orders, committed DB2 updates, written an output file, or sent an interface before it encountered the malformed record.

Before a rerun, verify:

Do not “repair” a count in production based on how many bytes seem present. Two complete-looking occurrences may be part of a truncated four-item record, and a downstream total may require the original business event to be resent by its owner.

Practical takeaway

An ODO count controls how much of a record the program is entitled to interpret. Validate its format, range, and relationship to the actual record length before looping. When those checks fail, preserve structural evidence and use the agreed reject and recovery process instead of letting an uncertain rerun turn one malformed record into duplicated business activity.

← Back to all articles