COBOL AT END Handling: Prevent the Last Input Record from Being Processed Twice
A sequential COBOL job can finish with a clean return code yet still produce an extra transaction if its end-of-file loop processes a record area after a failed read. This tutorial shows the control flow that prevents that failure, the evidence to collect when totals disagree, and how to test the last-record boundary safely.
A sequential COBOL job can finish with a clean return code yet still produce an extra transaction if its end-of-file loop processes a record area after a failed read. This tutorial shows the control flow that prevents that failure, the evidence to collect when totals disagree, and how to test the last-record boundary safely.
The small loop error with a large business effect
A sequential file is normally processed one record at a time: READ, check the result, apply business logic, then read again. The danger appears when those decisions are reversed. At end of file, COBOL does not magically clear the input record area. If the program evaluates fields or performs the business paragraph after the last READ has reached AT END, values from the prior successful read may still be present in working storage or the file record area.
That can replay the final input record. In a report program, the visible symptom may be one extra detail line and a count that is one too high. In an update program, it can be more serious: the last payment, adjustment, or outbound event may be attempted twice. Whether a duplicate is finally committed depends on the database design, commit pattern, and duplicate controls. Do not assume a unique index or a downstream reconciliation will always save the run.
Consider a fictional daily adjustment file. It contains five accepted input rows. The intended result is five audit records and a trailer count of five. Support sees six detail records in the generated output, while the input dataset and the first five values look correct. That pattern should lead directly to the end-of-file boundary, not to an immediate file rerun.
A safe control pattern
The most readable approach separates read success from record processing. Set an EOF switch only in the AT END phrase, and process the record only when that switch says a successful read occurred.
WORKING-STORAGE SECTION.
01 WS-END-OF-FILE PIC X VALUE 'N'.
88 END-OF-FILE VALUE 'Y'.
88 MORE-INPUT VALUE 'N'.
01 WS-INPUT-COUNT PIC 9(7) VALUE ZERO.
01 WS-OUTPUT-COUNT PIC 9(7) VALUE ZERO.
PROCEDURE DIVISION.
OPEN INPUT ADJ-IN
OUTPUT ADJ-OUT
PERFORM UNTIL END-OF-FILE
READ ADJ-IN
AT END
SET END-OF-FILE TO TRUE
NOT AT END
ADD 1 TO WS-INPUT-COUNT
PERFORM PROCESS-ADJUSTMENT
END-READ
END-PERFORM
PERFORM WRITE-TRAILER
CLOSE ADJ-IN ADJ-OUT
GOBACK.
PROCESS-ADJUSTMENT.
IF IN-ADJ-AMOUNT NOT NUMERIC
PERFORM WRITE-REJECT
ELSE
PERFORM WRITE-ACCEPTED-ADJUSTMENT
ADD 1 TO WS-OUTPUT-COUNT
END-IF.
```
The NOT AT END phrase keeps the business paragraph inside the successful-read path. No later IF NOT END-OF-FILE is required to protect this example, which makes the intent easier to review. The exact 88-level names and paragraph structure can follow local standards; the essential rule is that the program must never process a record because a previous read happened to succeed.
An equally valid pattern performs one priming READ, then loops while MORE-INPUT is true and performs the next read at the bottom. That style can be useful in older programs, but it is easier to introduce a missing initial read or a missing final read during maintenance. Prefer the style your team can test and review consistently rather than mixing both in one program.
A tempting pattern that is unsafe
This abbreviated structure looks plausible but is wrong when PROCESS-ADJUSTMENT runs after an end-of-file read:
PERFORM UNTIL END-OF-FILE
READ ADJ-IN
AT END
SET END-OF-FILE TO TRUE
END-READ
PERFORM PROCESS-ADJUSTMENT
END-PERFORM
On the final loop pass, the READ takes AT END; the EOF switch becomes true, but execution continues to the next statement in that pass. The PERFORM PROCESS-ADJUSTMENT therefore runs once more. If the processing paragraph refers to IN-ADJ-ID, IN-ADJ-AMOUNT, or a field copied from them, it can use the prior record's value.
Do not “fix” this only by clearing the input area before every read. Clearing can make the duplicate less visible, but the loop is still executing a business paragraph without a valid input record. It may instead create a blank output row, a conversion error, or a misleading reject. Correct the control flow.
What to inspect when the last record looks duplicated
First preserve the run evidence. A second execution can overwrite outputs, add new audit rows, or obscure whether the first run committed any work. Collect enough information to prove the boundary:
- Input count from the source trailer, if one exists, and the actual number of records read by the program.
- Accepted, rejected, output, and database-update counts from the program's own control report or log.
- The final two logical input records and final two output records, using masked or approved identifiers where data is sensitive.
- The job step, program version, compile/load-module promotion reference, and run timestamp.
- The commit or checkpoint position if updates occur, plus evidence of external messages, files, or audit writes.
- The exact source path around the
READ,AT END,NOT AT END, and processing paragraph call.
A count difference of exactly one is a useful clue, not proof. A repeated final record can also come from an upstream file build, a restart that begins too early, an output append disposition, or duplicate source data. Compare sequence keys and timestamps where available before assigning the cause.
Verification cases that expose the defect
A test file with many rows can hide boundary errors. Use deliberately small files in a controlled test environment:
| Test input | Expected processing result | What it proves |
| --- | --- | --- |
| Empty file | Zero detail rows; valid zero trailer if allowed | AT END path does not process stale storage |
| One valid row | One detail row | Final successful read is processed once |
| Two distinct rows | Two detail rows in order | Normal loop continuation works |
| One invalid row | One reject or controlled error, per design | Validation stays inside successful-read path |
| Valid row followed by invalid row | One accepted and one reject | Last record is not skipped or replayed |
For an update program, test against disposable data and verify both row counts and business keys after each case. A successful compile or an RC=0 does not prove the EOF boundary is correct.
Before rerun
If a production run may have replayed the final record, pause before choosing a restart point:
- [ ] Confirm whether the suspected final key appears more than once in output, audit, DB2, or an outbound interface.
- [ ] Identify the last committed unit of work; do not infer it from the last input line alone.
- [ ] Check whether duplicate prevention rejected a second update or merely hid it from one table while another side effect occurred.
- [ ] Reconcile input, accepted, rejected, output, and amount totals according to the application’s defined controls.
- [ ] Obtain the approved recovery action for the application: restart, compensating correction, controlled reprocess, or escalation.
- [ ] Test the corrected loop with empty, one-record, and multi-record files before any production change.
Local restart procedures, data retention rules, and error-handling standards vary. Do not delete output, rerun the job, or apply a compensating update simply to make counts look even.
Practical takeaway
Treat AT END as a boundary where no business processing is permitted unless a successful read has explicitly selected it. Keeping PROCESS-ADJUSTMENT inside NOT AT END, then proving the behavior with tiny boundary test files, prevents an easy-to-miss last-record replay from becoming a production reconciliation problem.