COBOL OCCURS DEPENDING ON: Validate the Count Before Processing a Variable Table
A variable table in a COBOL record is easy to read incorrectly: the program sees a maximum number of occurrences, while the business record declares how many are meaningful. This tutorial shows how to validate that declared count, process only valid occurrences, and collect enough evidence before treating a count problem as a data or program defect.
A variable table in a COBOL record is easy to read incorrectly: the program sees a maximum number of occurrences, while the business record declares how many are meaningful. This tutorial shows how to validate that declared count, process only valid occurrences, and collect enough evidence before treating a count problem as a data or program defect.
The small field that controls a large decision
OCCURS DEPENDING ON (ODO) is often used when one input record can carry a variable number of related items. A payment header might contain up to 20 adjustment amounts; a policy record might hold a varying number of beneficiaries. The count field tells the program which entries are present for that record.
The operational risk is not the PERFORM VARYING itself. It is treating the count as automatically trustworthy. If the count is zero when the business process requires at least one item, is greater than the table maximum, or has been misread because the layout is wrong, a loop can produce incomplete totals or attempt to use data that should not exist. Depending on compiler options, file organization, and record definitions, an ODO also affects how records are interpreted. Local standards therefore matter.
This article uses a fictional adjustment record. The field names and thresholds are examples, not a production copybook.
Minimal concept: maximum storage is not the active table size
Consider this working-storage layout:
01 WS-ADJUSTMENT-RECORD.
05 WS-ACCOUNT-NO PIC X(10).
05 WS-ADJUSTMENT-COUNT PIC 9(2).
05 WS-ADJUSTMENT-ITEM OCCURS 0 TO 20 TIMES
DEPENDING ON WS-ADJUSTMENT-COUNT.
10 WS-ADJUSTMENT-CODE PIC X(03).
10 WS-ADJUSTMENT-AMOUNT PIC S9(7)V99 COMP-3.
The table has a maximum of 20 occurrences. That does not mean entries 1 through 20 are usable on every record. For an input count of 3, only entries 1 through 3 belong to the logical record. Looking at entry 4 because it happens to contain bytes in storage is not a valid business operation.
In many applications, the actual input layout is declared in an FD record or passed through a copybook rather than built in working-storage. The same rule applies: the count must be checked before it controls processing. Do not silently cap a count of 27 to 20 and continue. That hides a malformed record and can make reconciliation totals look plausible while losing information.
Worked example: reject one bad count without corrupting the batch total
Assume that a batch program reads adjustment records, validates every amount, and adds accepted amounts to a run total. The input definition has an upper limit of 20. The following logic deliberately separates count validation from item processing.
01 WS-ADJUSTMENT-COUNT-MAX PIC 9(2) VALUE 20.
01 WS-ITEM-SUBSCRIPT PIC 9(2) VALUE ZERO.
01 WS-RECORD-ACCEPTED PIC X VALUE 'N'.
88 RECORD-ACCEPTED VALUE 'Y'.
3000-PROCESS-RECORD.
MOVE 'N' TO WS-RECORD-ACCEPTED
IF WS-ADJUSTMENT-COUNT = ZERO
PERFORM 3100-REPORT-BAD-COUNT
ELSE
IF WS-ADJUSTMENT-COUNT > WS-ADJUSTMENT-COUNT-MAX
PERFORM 3100-REPORT-BAD-COUNT
ELSE
MOVE 'Y' TO WS-RECORD-ACCEPTED
PERFORM VARYING WS-ITEM-SUBSCRIPT FROM 1 BY 1
UNTIL WS-ITEM-SUBSCRIPT > WS-ADJUSTMENT-COUNT
PERFORM 3200-VALIDATE-AND-ACCUMULATE
END-PERFORM
END-IF
END-IF.
```
3100-REPORT-BAD-COUNT should write a controlled reject record with a safe business key, the received count, and a reason code such as COUNT-ZERO or COUNT-OVER-MAX. Avoid putting sensitive payload data in a broadly accessible spool report. The report has two jobs: make reconciliation possible and give support staff a way to find the source record without guessing.
3200-VALIDATE-AND-ACCUMULATE should still validate the code and amount for each active occurrence. A valid count does not prove that every child item is valid. For example, an amount might be negative when the interface contract permits only positive adjustments, or an unused code may have arrived from an upstream application.
One important design choice remains: should one invalid child item reject the whole parent record, or should valid children continue? That is a business and control decision. For financial postings, rejecting the parent record is often safer because the claimed count, detail total, and output total can be reconciled together. For a non-financial notification feed, a valid-item-with-reject-report rule may be acceptable. Document the policy; do not let a loop accidentally define it.
Failure cases worth diagnosing separately
| Observation | Likely question to answer | Unsafe shortcut |
| --- | --- | --- |
| Count is zero | Is an empty list permitted for this transaction type? | Assuming zero means end of file or a harmless record |
| Count exceeds 20 | Is the sender using a newer layout or is the field offset wrong? | Processing the first 20 and discarding the rest |
| Many records have the same impossible count | Did a copybook, code page, or input-file version change? | Correcting individual records before checking the interface |
| Count is valid but detail values look shifted | Is the file being read with the correct record format and layout? | Changing the loop limit to make one sample look right |
| Reject volume suddenly rises | Did upstream rules or the business calendar change? | Rerunning without preserving the first-run evidence |
A count of 99 is not automatically proof of corrupt data. It may indicate a layout mismatch: the program could be reading bytes from the wrong position because an upstream file version changed, a record prefix was omitted, or an incompatible copybook was promoted. Conversely, a count of 20 is not suspicious merely because it equals the maximum. Evidence decides the case.
Evidence before deciding on correction or rerun
When production support sees count rejects, collect a small, repeatable evidence set before changing code, reformatting a file, or rerunning a step:
- Job name, step name, completion code, and the input dataset generation or immutable delivery identifier.
- Read count, accepted-parent count, rejected-parent count, and processed-detail count.
- Reject reason counts, including zero, over-maximum, and invalid-detail reasons.
- A masked or approved sample of the relevant raw record bytes and the expected field offsets, handled under local data rules.
- The copybook or interface version expected by both sender and receiver.
- Whether the run updated downstream data, and the commit/restart position if it did.
- Control-total comparison: input records, accepted detail amounts, rejected detail amounts, and output records where applicable.
This list prevents a common mistake: treating a syntactically successful COBOL run as proof that the input was correctly interpreted. A job can end with RC=0 while business data is being rejected or silently misclassified.
Before rerun
Do not rerun merely because a count validation fix has been proposed. First establish whether the original run produced outputs or updates that must be backed out, superseded, or excluded. Check the site’s restart and reconciliation procedures, particularly where DB2 commits, VSAM updates, GDGs, or downstream extracts are involved.
Use this short gate:
1. Confirm the exact input version and preserve the original reject report and counts.
2. Confirm why the count failed: invalid source data, changed interface layout, or program defect.
3. Validate the correction with representative records: zero, one, maximum, over-maximum, and an invalid child item.
4. Reconcile expected parent and detail totals in a non-production environment where possible.
5. Obtain the required business and operational approval before any controlled restart or rerun.
Do not alter a shared copybook or relax the maximum simply to clear a batch. A maximum increase can be a real interface change, but it needs coordinated sender/receiver agreement, test evidence, and change control.
Practical takeaway
With OCCURS DEPENDING ON, the count field is a control field, not just a loop bound. Validate it explicitly, process only the declared occurrences, and preserve rejection and reconciliation evidence before deciding whether the input, layout, or program needs correction.