Mainframe Path Start learning free
Core8 min readLesson 3 of 5

PICTURE clauses and how data is stored

The PIC clause says what a field holds and how big it is. The USAGE says how it is physically stored. Getting these wrong is the single largest source of COBOL run-time failures.

Reading a PICTURE

PICMeansExample value
PIC X(10)10 characters, anything'SMITH '
PIC 9(5)5 digits, unsigned00042
PIC S9(5)5 digits, signed-00042
PIC S9(7)V997 digits, an implied decimal point, 2 decimals0001234.56
PIC A(20)Letters and spaces onlyrarely used
PIC 9(3)V9(2)Same as S9(3)V99 without the sign
PIC ZZZ,ZZ9.99-An edited field for printing' 12,345.67-'

USAGE: how it is physically stored

USAGEAlso calledStorageUse for
DISPLAYthe defaultOne byte per digit or characterAnything read or written as text
COMP-3packed decimalTwo digits per byte, sign in the last nibbleMoney and counters — the workhorse
COMP / BINARYbinary2, 4 or 8 bytesSubscripts, counters, flags
COMP-1 / COMP-2floating point4 / 8 bytesScientific work; almost never money
The same value, three ways
PIC 9(5) DISPLAY
Value 123455 bytesF1 F2 F3 F4 F5Readable in a dump
PIC S9(5) COMP-3
Value 123453 bytes12 34 5CC = positive sign
PIC S9(5) COMP
Value 123454 bytes00 00 30 39Pure binary

Why COMP-3 is everywhere

Packed decimal stores two digits per byte and keeps exact decimal arithmetic. A PIC S9(7)V99 COMP-3 field holds nine digits in five bytes, computes money exactly, and is the format the hardware has instructions for. It is the default choice for any numeric business field. The cost is that you cannot read it in a dump without decoding it — which is exactly the skill the next lesson needs.

MOVE and the rules that surprise people

What MOVE actually does
* character move: left-justified, padded or truncated on the RIGHT
01 WS-A PIC X(05) VALUE 'ABCDE'.
01 WS-B PIC X(03).
   MOVE WS-A TO WS-B      >> WS-B = 'ABC'   (E and D lost)

* numeric move: aligned on the DECIMAL POINT, padded or truncated at both ends
01 WS-C PIC S9(5)V99 VALUE 12345.67.
01 WS-D PIC S9(3)V9.
   MOVE WS-C TO WS-D      >> WS-D = 345.6  (high-order AND low-order lost)

* the silent one that causes S0C7 later
01 WS-E PIC X(05) VALUE SPACES.
01 WS-F PIC 9(05).
   MOVE WS-E TO WS-F      >> WS-F now contains spaces, not digits
   ADD 1 TO WS-F          >> S0C7

Initialising properly

Example, line by line

COBOLWhat it means
01 CUSTOMER-RECORD.
Group item: names the whole record. It holds no data of its own, only the fields below it.
05 CUSTOMER-ID PIC 9(8).
Numeric field of 8 digits, stored one digit per byte (zoned decimal).
05 CUSTOMER-NAME PIC X(40).
Character field of 40 bytes, padded with spaces on the right.
05 BALANCE PIC S9(7)V99 COMP-3.
Signed packed decimal: 9 digits, 2 of them after an implied decimal point, in 5 bytes.
05 STATUS-CODE PIC X.
A single character.
88 ACTIVE VALUE 'A'.
Condition name: IF ACTIVE is the same as IF STATUS-CODE = 'A'.

Try it yourself

TRY IT YOURSELF

How many bytes does BALANCE, defined as PIC S9(7)V99 COMP-3, occupy?

Show a hint

Packed decimal stores two digits per byte, and the sign needs half a byte.

Show the solution

5 bytes. 9 digits plus the sign is 10 half-bytes, and two half-bytes fit in each byte.

Common mistakes

Moving a group item to a numeric field

Groups are character data. If it contains spaces or packed data, the target is now invalid and the next arithmetic abends S0C7.

Ignoring ON SIZE ERROR

Overflow truncates silently. Add ON SIZE ERROR on any calculation where the result could exceed the field.

Assuming redefined fields are converted

REDEFINES reinterprets the same bytes; it does not convert anything. Reading a character field through a numeric REDEFINES gives you garbage.

Using COMP for money

Binary is fine for subscripts and counts. Money should be COMP-3 so decimal arithmetic stays exact.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
← The four divisionsLogic, files and calling other programs →