Compaq COBOL AAQ2G1FTK User Manual
Page 298
Using the SORT and MERGE Statements
9.3 Sample Programs Using the SORT and MERGE Statements
Example 9–12 (Cont.) Merging Files
FD
TOTAL-SALES
LABEL RECORDS ARE STANDARD.
01
TOTAL-RECORD
PIC X(100).
WORKING-STORAGE SECTION.
01
INITIAL-READ
PIC X
VALUE "Y".
01
THE-COUNTERS.
03
PRODUCT-AMT
PIC S9(7)V99.
03
REGION1-AMT
PIC S9(9)V99.
03
REGION2-AMT
PIC S9(9)V99.
03
REGION3-AMT
PIC S9(9)V99.
03
TOTAL-AMT
PIC S9(11)V99.
01
SAVE-MERGE-REC.
03
S-REGION-CODE
PIC XX.
03
S-PRODUCT-CODE
PIC X(10).
03
S-SALES-AMT
PIC S9(7)V99.
03
FILLER
PIC X(79).
PROCEDURE DIVISION.
000-START SECTION.
010-MERGE-FILES.
OPEN OUTPUT TOTAL-SALES.
MERGE MERGE-FILE ON ASCENDING KEY M-PRODUCT-CODE
USING REGION1-SALES REGION2-SALES REGION3-SALES
OUTPUT PROCEDURE IS 020-BUILD-TOTAL-SALES
THRU 100-DONE-TOTAL-SALES.
DISPLAY "TOTAL SALES FOR REGION 1 " REGION1-AMT.
DISPLAY "TOTAL SALES FOR REGION 2 " REGION2-AMT.
DISPLAY "TOTAL SALES FOR REGION 3 " REGION3-AMT.
DISPLAY "TOTAL ALL SALES
" TOTAL-AMT.
CLOSE TOTAL-SALES.
DISPLAY "END OF PROGRAM MERGE01".
STOP RUN.
020-BUILD-TOTAL-SALES SECTION.
030-GET-MERGE-RECORDS.
RETURN MERGE-FILE AT END
MOVE PRODUCT-AMT TO S-SALES-AMT
WRITE TOTAL-RECORD FROM SAVE-MERGE-REC
GO TO 100-DONE-TOTAL-SALES.
IF INITIAL-READ = "Y"
MOVE "N" TO INITIAL-READ
MOVE MERGE-REC TO SAVE-MERGE-REC
PERFORM 050-TALLY-AMOUNTS
GO TO 030-GET-MERGE-RECORDS.
040-COMPARE-PRODUCT-CODE.
IF M-PRODUCT-CODE = S-PRODUCT-CODE
PERFORM 050-TALLY-AMOUNTS
GO TO 030-GET-MERGE-RECORDS.
MOVE PRODUCT-AMT TO S-SALES-AMT.
MOVE ZEROES TO PRODUCT-AMT.
WRITE TOTAL-RECORD FROM SAVE-MERGE-REC.
MOVE MERGE-REC TO SAVE-MERGE-REC.
GO TO 040-COMPARE-PRODUCT-CODE.
(continued on next page)
9–20 Using the SORT and MERGE Statements