COBOL MOVE
MOVE is COBOL's assignment statement. It copies a value into one or more receiving items:
MOVE source TO target-1 [target-2 ...]The interesting part is what happens when the source and the receiver have different sizes or types. COBOL never fails here: it pads or truncates according to fixed rules. Learning these rules is essential.
Alphanumeric moves: left-aligned
When the receiver is alphanumeric (PIC X), characters are copied from the left. A shorter value is padded
with spaces on the right; a longer value is cut off on the right.
IDENTIFICATION DIVISION.
PROGRAM-ID. MOVE-TEXT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SHORT PIC X(4).
01 WS-LONG PIC X(12).
PROCEDURE DIVISION.
MOVE "Amsterdam" TO WS-SHORT WS-LONG
DISPLAY "X(4): [" WS-SHORT "]"
DISPLAY "X(12): [" WS-LONG "]"
STOP RUN.X(4): [Amst]
X(12): [Amsterdam ]
Numeric moves: aligned on the decimal point
When the receiver is numeric, the value is aligned on the decimal point. Integer digits that don't fit are lost from the left (the most significant digits!), missing positions become zeros, and extra decimals are cut off on the right (not rounded).
IDENTIFICATION DIVISION.
PROGRAM-ID. MOVE-NUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SOURCE PIC 9(5)V999 VALUE 12345.678.
01 WS-SMALL PIC 999V9.
01 WS-BIG PIC 9(7)V9(4).
01 WS-WHOLE PIC 9(5).
PROCEDURE DIVISION.
MOVE WS-SOURCE TO WS-SMALL WS-BIG WS-WHOLE
DISPLAY "Source: " WS-SOURCE
DISPLAY "999V9: " WS-SMALL
DISPLAY "9(7)V9(4): " WS-BIG
DISPLAY "9(5): " WS-WHOLE
STOP RUN.Source: 12345.678
999V9: 345.6
9(7)V9(4): 0012345.6780
9(5): 12345
12345.678 moved into 999V9 keeps 345.6: the 12 on the left and the 78 on the right are dropped
without any warning.
Warning: Silent high-order truncation is a classic source of production bugs. Size receiving fields for the largest value the business allows, and use
ON SIZE ERRORin arithmetic (later lesson) to catch overflows.
Mixing numbers and text
| Move | Result |
|---|---|
| numeric → alphanumeric | The digits are copied as text, left-aligned (the sign is not copied) |
| alphanumeric → numeric | The characters are treated as an unsigned integer; non-digits give garbage. Use FUNCTION NUMVAL instead |
| numeric → numeric-edited | The value is formatted (see edited pictures) |
| group → anything | A plain byte copy, as alphanumeric |
IDENTIFICATION DIVISION.
PROGRAM-ID. MOVE-MIXED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-QTY PIC 9(4) VALUE 75.
01 WS-QTY-TEXT PIC X(6).
01 WS-INPUT PIC X(3) VALUE "042".
01 WS-NUMBER PIC 9(3).
PROCEDURE DIVISION.
MOVE WS-QTY TO WS-QTY-TEXT
DISPLAY "Number as text: [" WS-QTY-TEXT "]"
MOVE WS-INPUT TO WS-NUMBER
ADD 1 TO WS-NUMBER
DISPLAY "Text as number + 1: " WS-NUMBER
STOP RUN.Number as text: [0075 ]
Text as number + 1: 043
Moving literals and figurative constants
IDENTIFICATION DIVISION.
PROGRAM-ID. MOVE-LITERALS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STATUS PIC X(8) VALUE "OPEN".
01 WS-TOTAL PIC S9(5)V99 VALUE 812.40.
01 WS-CODE PIC X(5).
PROCEDURE DIVISION.
MOVE "CLOSED" TO WS-STATUS
MOVE ZERO TO WS-TOTAL
MOVE ALL "#" TO WS-CODE
DISPLAY WS-STATUS "|" WS-TOTAL "|" WS-CODE
MOVE -3.456 TO WS-TOTAL
DISPLAY "After MOVE -3.456: " WS-TOTAL
STOP RUN.CLOSED |+00000.00|#####
After MOVE -3.456: -00003.45
Notice that -3.456 became -3.45: the third decimal was truncated, not rounded. Use
COMPUTE … ROUNDED when you need rounding.
Common mistakes
- Expecting
MOVEto round. It truncates. UseROUNDEDwith arithmetic. - Moving a signed value into an unsigned field: the sign is lost.
- Moving text with letters into a numeric field and then calculating with it.
- Forgetting that
MOVE A TO Bcopies from A to B (the opposite order tob = ain other languages).
Test yourself
What does MOVE "Amsterdam" TO WS-CITY store if WS-CITY is PIC X(4)?
- "dam "
- "Amst"
- "Amsterdam"
- A runtime error
What does MOVE 12345 TO WS-N store if WS-N is PIC 999?
- 123
- 345
- 999
- 12345
Does MOVE 2.718 TO WS-X with WS-X PIC 9V99 round the value?
- Yes, it stores 2.72
- No, it stores 2.71
- It stores 3
- It is a compile error