Data Movement Statements
Data movement statements are responsible for transferring data between data items or memory locations. The data movement statements are –
- INITIALIZE Statement
- MOVE Statement
INITIALIZE -
INITIALIZE sets the variables with system-defined initial values based on their data types. It is convenient to reset the full copybook and group variable to ensure that they start with predictable values.
Syntax -
INITIALIZE group-variable-1 [group-variable-2 ...]
- group-variable-1, group-variable-2, ...- The variable names we wish to initialize.
Example - Initialize usage in COBOL program.
IDENTIFICATION DIVISION.
PROGRAM-ID. INITGRP.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-NAME PIC X(20).
05 FILLER PIC X(05).
05 WS-GENDER PIC X(01).
05 FILLER PIC X(05).
05 WS-TODAY PIC 9(08).
05 FILLER PIC X(01).
PROCEDURE DIVISION.
MOVE ALL '-' TO WS-VAR.
DISPLAY 'BEFORE INIT: ' WS-VAR.
INITIALIZE WS-VAR.
DISPLAY 'AFTER INIT: ' WS-VAR.
STOP RUN.
JCL -
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //** //STEP01 EXEC PGM=INITGRP //STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR //SYSOUT DD SYSOUT=*
Output -
BEFORE INIT: ---------------------------------------- AFTER INIT: ----- -----00000000-
Explaining Example -
FILLER positions are not initalized with INITIALIZE statement and remaining variables initialized according to their data type.
MOVE -
MOVE statement is used to transfer data from the source data item to the target data item. It allows data to be transferred from one variable or literal to another, ensuring data values are appropriately transformed or converted based on the receiving data item's type or format.
MOVE statement is classified into below based on its usage in the program -
Simple MOVE -
The value from the sending item is moved to the receiving item. If the receiving item is numeric, the sending should ideally be numeric to avoid data errors. If the receiving is alphanumeric, almost any type of sending is allowed.
Syntax -
MOVE sending-item TO receiving-item.
Example - Moving alphanumeric string to another variable.
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SENDING-VAR PIC X(5) VALUE 'Hello'.
01 WS-RECEIVING-VAR PIC X(10).
PROCEDURE DIVISION.
MOVE WS-SENDING-VAR TO WS-RECEIVING-VAR.
DISPLAY "WS-RECEIVING-VAR: " WS-RECEIVING-VAR.
After the MOVE, WS-RECEIVING-VAR will contain 'Hello' followed by 5 spaces.
Group MOVE -
Group MOVE is used to move the data from one group item to another group item. Group MOVE can have one or more receiving items. The elementary variables in both groups should have the same name and in the same order.
Syntax -
MOVE sending-group TO receiving-group
Example - Moving data from one group to another group.
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-GRP1.
05 NUM PIC 9(02) VALUE 1.
05 NAME PIC X(20) VALUE 'MAINFRAMESTECHHELP'.
01 WS-GRP2.
05 NUM PIC 9(02).
05 NAME PIC X(20).
PROCEDURE DIVISION.
MOVE WS-GRP1 TO WS-GRP2.
After the MOVE, WS-GRP2 will contain 01MAINFRAMESTECHHELP.
Corresponding MOVE -
The MOVE CORRESPONDING is used to move data between two group items. However, the sending group's elementary items should match the receiving group's elementary items.
Syntax -
MOVE CORRESPONDING|CORR sending-group TO receiving-group
Example - Changing the date format from MM-DD-YYYY to DD/MM/YYYY.
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DATE-MDY.
05 WS-DATE-MM PIC 9(02) VALUE 10.
05 FILLER PIC X VALUE '-'.
05 WS-DATE-DD PIC 9(02) VALUE 13.
05 FILLER PIC X VALUE '-'.
05 WS-DATE-YYYY PIC 9(04) VALUE 2023.
01 WS-DATE-DMY.
05 WS-DATE-DD PIC 9(02).
05 FILLER PIC X VALUE '/'.
05 WS-DATE-MM PIC 9(02).
05 FILLER PIC X VALUE '/'.
05 WS-DATE-YYYY PIC 9(04).
PROCEDURE DIVISION.
MOVE CORR WS-DATE-MDY TO WS-DATE-DMY.
After the MOVE, WS-DATE-DMY will contain 13/10/2023.
MOVE Referencing Modification -
The reference modification is used for a special purpose to move part of data from the sending item to the receiving item. This is mainly useful when dealing with strings, where we might want to handle only a specific part of the data item.
Syntax -
variable (start-position [: length])
Example - Formatting full phone number from three different sources.
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNTRY-CODE PIC 9(02) VALUE 91.
01 WS-AREA-CODE PIC 9(03) VALUE 999.
01 WS-PHONE-NBR PIC 9(08) VALUE 87654321.
01 WS-FULL-PHN-NBR PIC 9(13).
PROCEDURE DIVISION.
MOVE WS-COUNTRY-CODE TO WS-FULL-PHN-NBR(1:2).
MOVE WS-AREA-CODE TO WS-FULL-PHN-NBR(3:2).
MOVE WS-PHONE-NBR TO WS-FULL-PHN-NBR(5:8).
After the MOVE, WS-FULL-PHN-NBR will contain 919987654321.
MOVE Combinations -
In the table, column headings indicate receiving item type, row headings indicate sending item type.
Alphabetic | Alpha-numeric | Numeric | Floating-point | |
---|---|---|---|---|
Alphabetic and SPACE | Y | Y | N | N |
Alphanumeric | Y | Y | Y | Y |
Numeric integer and ZERO | N | Y | Y | Y |
Floating-point | N | N | Y | Y |
