COBOL Packed-Decimal Data Exceptions: Diagnose Invalid COMP-3 Input Before a Batch Rerun
A COBOL batch program can fail while reading a numeric field even when the dataset allocated successfully and the record length looks correct. This tutorial shows how to investigate a suspected invalid packed-decimal (`COMP-3`) value without guessing at the bad record, changing a copybook blindly, or rerunning a job that may already have produced partial business effects.
A COBOL batch program can fail while reading a numeric field even when the dataset allocated successfully and the record length looks correct. This tutorial shows how to investigate a suspected invalid packed-decimal (COMP-3) value without guessing at the bad record, changing a copybook blindly, or rerunning a job that may already have produced partial business effects.
The example is fictional. Compiler options, abend reporting, file layouts, and permitted diagnostic tools vary by site. Treat a numeric-data failure as evidence of a contract problem until the record, layout, and processing position prove otherwise.
Goal: separate a bad value from a bad interpretation
Packed decimal stores two decimal digits in most bytes and a sign nibble in the low half of the final byte. A field declared PIC S9(7)V99 COMP-3 needs five bytes: nine digits require five nibbles after the first four bytes provide eight, and the final nibble holds the sign. A program that interprets unrelated bytes as that field can fail just as surely as a program that receives malformed numeric data.
Suppose PAYPOST reads a daily payment file. Its input copybook contains this item:
05 IN-PAYMENT-AMOUNT PIC S9(7)V99 COMP-3.
05 IN-PAYMENT-DATE PIC 9(8).
05 IN-CHANNEL-CODE PIC X(02).
During the run, PAYPOST stops after some records. The console or spool may show a data exception, an operation exception, or a compiler/runtime message pointing near a calculation, comparison, MOVE, display, or conversion. That message identifies where invalid numeric content was used; it does not, by itself, prove that the amount field in the incoming feed is corrupt.
There are four realistic explanations to distinguish:
| Observation | Plausible cause | What to prove next |
| --- | --- | --- |
| The same input record fails in a controlled replay | A specific record has invalid packed content. | Capture its byte offset and hexadecimal representation through an approved tool. |
| Failure starts after an upstream file-format change | The producer and consumer layouts differ. | Compare record length, layout version, offsets, and field encoding. |
| The record is valid but the program branches to an old layout | A condition, copybook, or redefinition selects the wrong interpretation. | Trace the record type and branch decision. |
| Failure changes position between runs | An uninitialized field, overlay, or prior storage corruption may be involved. | Preserve diagnostics and have the application team investigate program logic. |
The safe objective is not “make the job finish.” It is to establish which bytes were consumed as numeric data, why the program reached that instruction, and what work had completed before the failure.
Read the evidence in the right order
Start with the job output and application messages. Record the job and step identifiers, the program and load module level, date/time, input dataset generation or immutable run identifier, record counts read/written/rejected, and the last application checkpoint or commit message. If the job updates DB2, sends messages, or writes a downstream extract, note those side effects separately.
Next, identify the failing operation. A statement such as this can expose an invalid value:
IF IN-PAYMENT-AMOUNT < ZERO
MOVE 'N' TO WS-ACCEPT-FLAG
ELSE
COMPUTE WS-NET-AMOUNT =
IN-PAYMENT-AMOUNT - WS-FEE-AMOUNT
END-IF
Do not assume the source statement is the source of the defect. The invalid value could have arrived in the input record, been moved from an incorrectly defined field, or been overlaid by a preceding operation. Conversely, a report line that displays an unexpected amount may be caused by a layout offset error rather than a damaged value.
Use only approved diagnostic facilities to inspect the suspect record. A hexadecimal dump is valuable because it reveals the actual nibbles rather than a display conversion that may already be invalid. For a positive 12345.67 in PIC S9(7)V99 COMP-3, the packed representation conceptually ends in a valid positive sign nibble such as C or F, subject to local conventions. A final nibble that is not accepted by the runtime, or a non-decimal nibble in a digit position, warrants investigation. Do not “repair” a byte based solely on a visual guess: an offset shift can make perfectly valid character data look like broken packed decimal.
Worked example: prove the layout before correcting data
Assume the supplier added a two-byte source indicator after the record type. The consumer is still using the older copybook. Every later field is now displaced by two bytes. IN-PAYMENT-AMOUNT is not reading the intended amount at all; it begins inside a character identifier and date field.
A simplified comparison makes the problem visible:
Old consumer layout
Offset Length Field
000 02 record type
002 05 account key
007 05 payment amount (COMP-3)
012 08 payment date
New producer layout
Offset Length Field
000 02 record type
002 02 source indicator <-- added
004 05 account key
009 05 payment amount (COMP-3)
014 08 payment date
```
The data may be valid according to the producer’s new contract, but it is invalid for the deployed consumer. A local file browse can confirm the physical LRECL; it cannot establish the semantic layout by itself. Obtain the approved interface specification or change record, compare the copybook version used to compile the program, and confirm whether the file has record-type variants or headers that alter the expected offset.
A temporary diagnostic should avoid placing business values in logs. A safer pattern is to log a run identifier, record sequence number, record type, layout version indicator, and the field offset. If policy permits a hex fragment, restrict it to the smallest non-sensitive range needed for diagnosis and use the approved protected location. Do not copy whole payment records into tickets, spool comments, or chat.
Once the mismatch is proven, the correction is usually an interface-controlled change: align the producer and consumer layouts, version the record format, or reject the unsupported version before numeric fields are referenced. A one-off REDEFINES or offset adjustment can hide the problem and make the next interface change harder to detect. If an optional layout is legitimate, make the selection explicit from a stable record type or version field, then test each variant.
Failure cases that deserve extra caution
- A short record or wrong DCB/LRECL. A copybook may be correct while truncation or a different record format changes what the program receives. Compare allocation, actual record length, and interface agreement.
- Edited versus packed fields.
PIC X(10)containing00123.45is not interchangeable with aCOMP-3amount. Convert only after validation; never redefine arbitrary character bytes as packed data to avoid an error. - Numeric validation after arithmetic. Checking a field only after a
COMPUTEis too late. Validate character input before conversion, and route invalid records to a controlled reject path where the interface supports it. - A rerun after partial updates. If the program commits periodically, earlier payments may already be posted. The bad record diagnosis and restart scope are separate decisions.
Before rerun: support checklist
- [ ] Capture the full runtime message, failing statement area, job/step details, and last reliable count or checkpoint.
- [ ] Identify the input generation and the exact record sequence or key through approved diagnostics.
- [ ] Prove the program’s deployed copybook/layout version and expected offsets.
- [ ] Compare source record layout, LRECL, record type, and any interface version indicator with the approved contract.
- [ ] Inspect the minimum necessary bytes in hexadecimal; do not expose sensitive records in operational notes.
- [ ] Determine whether the failure is bad input, layout misalignment, wrong branch, or possible program storage corruption.
- [ ] Check committed DB2 work, output files, messages, and downstream triggers before choosing any restart point.
- [ ] Obtain the normal application/interface approval for a data correction, layout change, reject decision, or documented restart.
- [ ] Reconcile read, accepted, rejected, posted, and output counts after the controlled rerun.
Practical takeaway
A packed-decimal exception is a clue about the bytes COBOL tried to use, not permission to alter data or rerun a batch step. Prove the record position and layout contract first, protect partial business work, and correct the interface or validation path under normal change control. That produces a durable fix instead of a batch run that merely gets past one bad-looking field.