Simple Move Example


Scenario - Simple MOVE statement usage in COBOL program.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SIMPMOVE.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-VAR1        PIC 9(04)  VALUE 2561.
          05 WS-VAR2        PIC 9(06).
          05 WS-VAR3        PIC 9(02).

       PROCEDURE DIVISION.
           MOVE WS-VAR1       TO WS-VAR2
                                 WS-VAR3.

           DISPLAY 'WS-VAR1:   ' WS-VAR1.
           DISPLAY 'WS-VAR2:   ' WS-VAR2.
           DISPLAY 'WS-VAR3:   ' WS-VAR3.

           STOP RUN.

Output -

WS-VAR1:   2561
WS-VAR2:   002561
WS-VAR3:   61

Explaining Example -

In the above example, WS-VAR1 is declared as a numeric variable of length four and initialized with 2561. Similarly, WS-VAR2 is declared with length 6, and WS-VAR3 is declared with length 2.

WS-VAR2 has a larger size than WS-VAR1. So, the data movement takes place from the rightmost byte because of the right justification for the numeric data type. After executing the move statement below, 2561 moved to WS-VAR2 and the additional two bytes at the leftmost are filled with ZEROES.

MOVE WS-VAR1       TO WS-VAR2

WS-VAR3 is smaller in size than WS-VAR1. After executing the move statement below, 61 will be moved to WS-VAR3, and 25 will be ignored.

MOVE VAR-1	TO 	VAR-3.