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.
MOVE CORRESPONDING sending-group TO receiving-group
- sending-group - The group item from which the data will be moved.
- receiving-group - The group item to which the data will be moved.
- 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.
----+----1----+----2----+----3----+----4----+----5----+ IDENTIFICATION DIVISION. PROGRAM-ID. MOVECORR. AUTHOR. MTH. 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. DISPLAY 'SOURCE GROUP: ' WS-DATE-MDY. DISPLAY 'TARGET GROUP: ' WS-DATE-DMY. STOP RUN.
SOURCE GROUP: 10-13-2023 TARGET GROUP: 13/10/2023
Explaining Example -
After executing the MOVE CORRESPONDING statement -
- WS-DATE-MM of WS-DATE-MDY value moved to WS-DATE-MM of WS-DATE-DMY.
- WS-DATE-DD of WS-DATE-MDY value moved to WS-DATE-DD of WS-DATE-DMY.
- WS-DATE-YYYY of WS-DATE-MDY value moved to WS-DATE-YYYY of WS-DATE-DMY.