Simple MOVE Statement
The MOVE statement is used to assign values from one data item to another. The simple MOVE operation is one of the most basic data manipulation statements in the COBOL language, and it's helpful in transferring values.
MOVE source-item TO destination-item-1 [destination-item-2 ...]
- source-item - The variable from which we want to move a value.
- destination-item-1, destination-item-2, ... - One or more variables to which we want to move the value from the source-item.
- The sending item can be an alphanumeric elementary item or a literal or a figurative constant.
- The receiving item should be an alphanumeric elementary item but not a literal or a figurative constant.
- MOVE statement can have one or more receiving items.
Practical Example -
Scenario - Simple MOVE statement coding in COBOL program.
----+----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.
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.