MOVE Corresponding
MOVE Corresponding
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.
Instead of moving each individual elelmentary item, the MOVE CORRESPONDING statement matches elementary items of the sending and receiving groups based on their names and transfers the data accordingly. This can be particularly useful when dealing with records with similar structures, allowing for efficient data transfer between them.
Syntax -
MOVE CORRESPONDING sending-group TO receiving-group
Parameters -
- sending-group - The group item from which the data will be moved.
- receiving-group - The group item to which the data will be moved.
Behavior -
- The data is moved from the sending to the receiving group based on matching names.
- Data items that do not have a corresponding match in the receiving group are ignored.
- If an item in the receiving group doesn't have a corresponding item in the sending group, its value remains unchanged.
- Only elementary items within the groups are considered for the move.
Practical Example -
Scenario - Changing the date format from MM-DD-YYYY to DD/MM/YYYY.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
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.
DISPLAY 'SOURCE GROUP: ' WS-DATE-MDY.
DISPLAY 'TARGET GROUP: ' WS-DATE-DMY.
...
Output -
SOURCE GROUP: 10-13-2023 TARGET GROUP: 13/10/2023