Compaq COBOL
User Manual


Previous Contents Index

9.1.5 Maintaining the Input Order of Records Using the WITH DUPLICATES IN ORDER Phrase

The sort orders data in the sequence specified in the ASCENDING KEY and DESCENDING KEY phrases. However, records with duplicate sort keys may not be written to the output file in the same sequence as they were read into it. The WITH DUPLICATES IN ORDER phrase ensures that any records with duplicate sort keys are in the same order in the output file as in the input file.

The following list shows the potential difference between sorting with the WITH DUPLICATES IN ORDER phrase and sorting without it:
Input File Sorted Without
Duplicates in Order
Sorted With
Duplicates in Order
Record Record Record
Name Data Name Data Name Data
JONES ABCD DAVIS LMNO DAVIS LMNO
DAVIS LMNO JONES EFGH JONES ABCD
WHITE STUV JONES ABCD JONES EFGH
JONES EFGH SMITH 1234 SMITH 1234
SMITH 1234 WHITE STUV WHITE STUV
WHITE WXYZ WHITE WXYZ WHITE WXYZ

If you omit the WITH DUPLICATES IN ORDER phrase, you cannot predict the order of records with duplicate sort keys. For example, the JONES records might not be in the same sequence as they were in the input file, but the WHITE records might be in the same order as in the input file.

In contrast, the WITH DUPLICATES IN ORDER phrase guarantees that records with duplicate sort keys remain in the same sequence as they were in the input file.

9.1.6 Specifying Non-ASCII Collating Sequences with the COLLATING SEQUENCE IS Alphabet-Name Phrase

This phrase lets you specify a collating sequence other than the ASCII default. You define collating sequences in the Environment Division SPECIAL-NAMES paragraph. A sequence specified in the COLLATING SEQUENCE IS phrase of the SORT statement overrides a sequence specified in the Environment Division PROGRAM COLLATING SEQUENCE IS phrase.

Example 9-3 shows the alphabet name NEWSEQUENCE overriding the EBCDIC-CODE collating sequence.

Example 9-3 Overriding the COLLATING SEQUENCE IS Phrase

ENVIRONMENT DIVISION. 
OBJECT-COMPUTER.  FOO 
      PROGRAM COLLATING SEQUENCE IS EBCDIC-CODE. 
SPECIAL-NAMES. 
    ALPHABET NEWSEQUENCE IS "ZYXWVUTSRQPONMLKJIHGFEDCBA" 
    ALPHABET EBCDIC-CODE IS EBCDIC. 
. 
. 
. 
PROCEDURE DIVISION. 
000-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY 
                      SORT-KEY-1 
                      SORT-KEY-2 
         COLLATING SEQUENCE IS NEWSEQUENCE 
         USING INPUT-FILE GIVING OUTPUT-FILE. 

9.1.7 Multiple Sorting

A program can contain multiple sort files, multiple SORT statements, or both multiple sort files and multiple SORT statements. Example 9-4 uses two sort files to produce two reports with different sort sequences.

Example 9-4 Using Two Sort Files

. 
. 
. 
DATA DIVISION. 
FILE SECTION. 
SD  SORT-FILE1. 
01  SORT-REC-1. 
    03  S1-KEY-1     PIC X(5). 
    03  FILLER       PIC X(40). 
    03  S1-KEY-2     PIC X(5). 
    03  FILLER       PIC X(50). 
SD  SORT-FILE2. 
01  SORT-REC-2. 
01  SORT-REC-2. 
    03  FILLER       PIC X(20). 
    03  S2-KEY-1     PIC X(10). 
    03  FILLER       PIC X(10). 
    03  S2-KEY-2     PIC X(10). 
    03  FILLER       PIC X(50). 
             . 
             . 
             . 
PROCEDURE DIVISION. 
000-SORT SECTION. 
010-DO-FIRST-SORT. 
    SORT SORT-FILE1 ON ASCENDING KEY 
                   S1-KEY-1 
                   S1-KEY-2 
                   WITH DUPLICATES IN ORDER 
                   USING INPUT-FILE 
                   OUTPUT PROCEDURE IS 050-CREATE-REPORT-1 
                                  THRU 300-DONE-REPORT-1. 
020-DO-SECOND-REPORT. 
    SORT SORT-FILE2 ON ASCENDING KEY 
                   S2-KEY-1 
                   ON DESCENDING KEY 
                   S2-KEY-2 
                   USING INPUT-FILE 
                   OUTPUT PROCEDURE IS 400-CREATE-REPORT-2 
                                  THRU 700-DONE-REPORT-2. 
030-END-JOB. 
    DISPLAY "PROGRAM ENDED". 
    STOP RUN. 
050-CREATE-REPORT-1 SECTION. 
********************************************************** 
*                                                        * 
*                                                        * 
*   Use the RETURN statement to read the sorted records. * 
*                                                        * 
*                                                        * 
********************************************************** 
300-DONE-REPORT-1 SECTION. 
310-EXIT-REPORT-1. 
    EXIT. 
400-CREATE-REPORT-2 SECTION. 
********************************************************** 
*                                                        * 
*                                                        * 
*   Use the RETURN statement to read the sorted records. * 
*                                                        * 
*                                                        * 
********************************************************** 
700-DONE-REPORT-2 SECTION. 
710-EXIT-REPORT. 
    EXIT. 

9.1.8 Sorting Variable-Length Records

If you specify the USING phrase and the input file contains variable-length records, the sort-file record must not be smaller than the smallest record, nor larger than the largest record, described in the input file.

If you specify the GIVING phrase and the output file contains variable-length records, the sort-file record must not be smaller than the smallest record, nor larger than the largest record, described in the output file.

9.1.9 Preventing I/O Aborts

All I/O errors detected during a sort can cause abnormal program termination. The Declarative USE AFTER STANDARD ERROR PROCEDURE, shown in Example 9-5, specifies error-handling procedures should I/O errors occur.

Example 9-5 The Declarative USE AFTER STANDARD ERROR PROCEDURE

PROCEDURE DIVISION. 
DECLARATIVES. 
SORT-FILE SECTION. 
    USE AFTER STANDARD ERROR PROCEDURE ON INPUT-FILE. 
SORT-ERROR. 
    DISPLAY "I-O TYPE ERROR WHILE SORTING". 
    DISPLAY "INPUT-FILE STATUS IS " INPUT-STATUS. 
    STOP RUN. 
END DECLARATIVES. 
000-SORT SECTION. 
010-DO-THE-SORT. 
    SORT SORT-FILE ON DESCENDING KEY 
                      S-KEY-1 
                   WITH DUPLICATES IN ORDER 
                   USING INPUT-FILE 
                   GIVING OUTPUT-FILE. 
    DISPLAY "END OF SORT". 
    STOP RUN. 

Note

The USE PROCEDURE phrase does not apply to Sort Description (SD) files.

9.1.10 Sorting Tables

The SORT statement can be used to order the elements in a table. This is especially useful for tables used with SEARCH ALL. The table elements are sorted based on the keys as specified in the OCCURS for the table unless you override them by specifying keys in the SORT statement. If no key is specified, the table elements are the SORT keys.

For the syntax and examples of table sorting, see the SORT statement description in the Procedure Division chapter of the Compaq COBOL Reference Manual.

9.1.11 Sorting at the Operating System Level

On OpenVMS an alternative to using the SORT statement within COBOL is to sort at the operating system level, using the bundled SORT utility.

On OpenVMS, you can choose between two sorting methods: Hypersort and Sort-32. Sort-32 is the default. Consult the DCL online help (type $HELP SORT) for details about the two methods, which have effects on optimization and other differences, and information about how to switch between Sort-32 and Hypersort. If you select Hypersort at DCL level, it will be in effect for a SORT statement within a COBOL program as well. <>

Hypersort is the sole method available on Tru64 UNIX and Windows NT Alpha.

See Appendix A for the record and key size limits with Sort-32 and Hypersort.

9.2 Merging Data with the MERGE Statement

The MERGE statement combines two or more identically sequenced files and makes their records available, in merged order, to an output procedure or to one or more output files. Use MERGE statement phrases the same way you use their SORT statement phrase equivalents. Note that the SORT phrases with DUPLICATES IN ORDER INPUT PROCEDURE are not allowed with MERGE.

In Example 9-6, district sales data is merged into one regional sales file.

Example 9-6 Using the MERGE Statement

     . 
     . 
     . 
DATA DIVISION. 
FILE SECTION. 
SD  MERGE-FILE. 
01  MERGE-REC. 
    03  FILLER            PIC XX. 
    03  M-PRODUCT-CODE    PIC X(10). 
    03  FILLER            PIC X(88). 
FD  DISTRICT1-SALES. 
01  DISTRICT1-REC         PIC X(100). 
FD  DISTRICT2-SALES. 
01  DISTRICT2-REC         PIC X(100). 
FD  REGION1-SALES. 
01  REGION1-REC           PIC X(100). 
PROCEDURE DIVISION. 
000-MERGE-FILES. 
    MERGE MERGE-FILE ON ASCENDING KEY M-PRODUCT-CODE 
          USING DISTRICT1-SALES DISTRICT2-SALES 
          GIVING REGION1-SALES. 
    STOP RUN. 

9.3 Sample Programs Using the SORT and MERGE Statements

The programs in Example 9-7, Example 9-8, Example 9-9, Example 9-10, Example 9-11, and Example 9-12 all show how to use the SORT and MERGE statements.

Example 9-7 shows how to use the SORT statement with the USING and GIVING phrases.

Example 9-7 Sorting a File with the USING and GIVING Phrases

IDENTIFICATION DIVISION. 
PROGRAM-ID.            SORTA. 
************************************************* 
*   This program shows how to sort              * 
*   a file with the USING and GIVING phrases    * 
*   of the SORT statement. The fields to be     * 
*   sorted are S-KEY-1 and S-KEY-2; they        * 
*   contain account numbers and amounts. The    * 
*   sort sequence is amount within account      * 
*   number.                                     * 
*   Notice that OUTPUT-FILE is a relative file. * 
************************************************* 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT INPUT-FILE ASSIGN TO "INPFIL". 
    SELECT OUTPUT-FILE ASSIGN TO "OUTFIL" 
           ORGANIZATION IS RELATIVE. 
    SELECT SORT-FILE ASSIGN TO "SRTFIL". 
DATA DIVISION. 
FILE SECTION. 
SD  SORT-FILE. 
01  SORT-REC. 
    03  S-KEY-1. 
        05  S-ACCOUNT-NUM      PIC X(8). 
    03  FILLER                 PIC X(32). 
    03  S-KEY-2. 
        05  S-AMOUNT           PIC S9(5)V99. 
    03  FILLER                 PIC X(53). 
FD  INPUT-FILE 
    LABEL RECORDS ARE STANDARD. 
01  IN-REC                     PIC X(100). 
FD  OUTPUT-FILE 
    LABEL RECORDS ARE STANDARD. 
01  OUT-REC                    PIC X(100). 
PROCEDURE DIVISION. 
000-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY 
                      S-KEY-1 
                      S-KEY-2 
         WITH DUPLICATES IN ORDER 
         USING INPUT-FILE GIVING OUTPUT-FILE. 
 
*********************************************************** 
*   At this point, you could transfer control to another  * 
*   section of your program and continue processing.      * 
*********************************************************** 
    DISPLAY "END OF PROGRAM SORTA". 
    STOP RUN. 

Example 9-8 shows how to use the USING and OUTPUT PROCEDURE phrases.

Example 9-8 Using the USING and OUTPUT PROCEDURE Phrases

IDENTIFICATION DIVISION. 
PROGRAM-ID. SORTB. 
************************************************************** 
*   This program shows how to sort a file                    * 
*   with the USING and OUTPUT PROCEDURE phrases              * 
*   of the SORT statement. The program eliminates            * 
*   duplicate records by adding their amounts to the         * 
*   amount in the first record with the same account         * 
*   number. Only records with unique account numbers         * 
*   are written to the output file. The fields to be         * 
*   sorted are S-KEY-1 and S-KEY-2; they contain account     * 
*   numbers and amounts. The sort sequence is amount         * 
*   within account number.                                   * 
*   Notice that the organization of OUTPUT-FILE is indexed.  * 
************************************************************** 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT INPUT-FILE ASSIGN TO "INPFIL". 
    SELECT OUTPUT-FILE ASSIGN TO "OUTFIL" 
           ORGANIZATION IS INDEXED. 
    SELECT SORT-FILE ASSIGN TO "SRTFIL". 
DATA DIVISION. 
FILE SECTION. 
SD  SORT-FILE. 
01  SORT-REC. 
    03  S-KEY-1. 
        05  S-ACCOUNT-NUM      PIC X(8). 
    03  FILLER                 PIC X(32). 
    03  S-KEY-2. 
        05  S-AMOUNT           PIC S9(5)V99. 
    03  FILLER                 PIC X(53). 
FD  INPUT-FILE 
    LABEL RECORDS ARE STANDARD. 
01  IN-REC                     PIC X(100). 
FD  OUTPUT-FILE 
    LABEL RECORDS ARE STANDARD 
    ACCESS MODE IS SEQUENTIAL 
    RECORD KEY IS OUT-KEY. 
01  OUT-REC. 
    03  OUT-KEY                PIC X(8). 
    03  FILLER                 PIC X(92). 
WORKING-STORAGE SECTION. 
01  INITIAL-SORT-READ          PIC X   VALUE "Y". 
01  SAVE-SORT-REC. 
    03  SR-ACCOUNT-NUM         PIC X(8). 
    03  FILLER                 PIC X(32). 
    03  SR-AMOUNT              PIC S9(5)V99. 
    03  FILLER                 PIC X(53). 
PROCEDURE DIVISION. 
000-START SECTION. 
005-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY 
                      S-KEY-1 
                      S-KEY-2 
         USING INPUT-FILE 
         OUTPUT PROCEDURE IS 300-CREATE-OUTPUT-FILE 
                        THRU 600-DONE-CREATE. 
************************************************************ 
*    At this point, you could transfer control to another  * 
*    section of the program and continue processing.       * 
************************************************************ 
    DISPLAY "END OF PROGRAM SORTB". 
    STOP RUN. 
 
300-CREATE-OUTPUT-FILE SECTION. 
350-OPEN-OUTPUT. 
    OPEN OUTPUT OUTPUT-FILE. 
400-READ-SORT-FILE. 
    RETURN SORT-FILE AT END 
        PERFORM 500-WRITE-THE-OUTPUT 
        CLOSE OUTPUT-FILE 
        GO TO 600-DONE-CREATE. 
    IF INITIAL-SORT-READ = "Y" 
        MOVE SORT-REC TO SAVE-SORT-REC 
        MOVE "N" TO INITIAL-SORT-READ 
        GO TO 400-READ-SORT-FILE. 
450-COMPARE-ACCOUNT-NUM. 
    IF S-ACCOUNT-NUM = SR-ACCOUNT-NUM 
        ADD S-AMOUNT TO SR-AMOUNT 
        GO TO 400-READ-SORT-FILE. 
500-WRITE-THE-OUTPUT. 
    MOVE SAVE-SORT-REC TO OUT-REC. 
    WRITE OUT-REC INVALID KEY 
        DISPLAY "INVALID KEY " SR-ACCOUNT-NUM " SORTB ABORTED" 
        CLOSE OUTPUT-FILE STOP RUN. 
550-GET-A-REC. 
    MOVE SORT-REC TO SAVE-SORT-REC. 
    GO TO 400-READ-SORT-FILE. 
600-DONE-CREATE SECTION. 
650-EXIT-PARAGRAPH. 
    EXIT. 

Example 9-9 shows how to use the INPUT PROCEDURE and OUTPUT PROCEDURE phrases.

Example 9-9 Using the INPUT PROCEDURE and OUTPUT PROCEDURE Phrases

IDENTIFICATION DIVISION. 
PROGRAM-ID. SORTC. 
********************************************************* 
*   This program shows how to use the INPUT             * 
*   PROCEDURE and OUTPUT PROCEDURE phrases of the       * 
*   SORT statement. Input to the sort is two files      * 
*   containing the same type of data. Records with      * 
*   a "D" status-code are not released to the sort.     * 
*   The program eliminates duplicate records by         * 
*   adding their amounts to the amount in the first     * 
*   record with the same account number. Only records   * 
*   with unique account numbers are written to          * 
*   the output file. The fields to be sorted are        * 
*   S-KEY-1 and S-KEY-2. The sort sequence is amount    * 
*   within account number.                              * 
********************************************************* 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT FIRST-FILE ASSIGN TO "FILE01". 
    SELECT SECOND-FILE ASSIGN TO "FILE02". 
    SELECT OUTPUT-FILE ASSIGN TO "OUTFIL". 
    SELECT SORT-FILE ASSIGN TO "SRTFIL". 
DATA DIVISION. 
FILE SECTION. 
SD  SORT-FILE. 
01  SORT-REC. 
    03  S-KEY-1. 
        05  S-ACCOUNT-NUM      PIC X(8). 
    03  FILLER                 PIC X(32). 
    03  S-KEY-2. 
        05  S-AMOUNT           PIC S9(5)V99. 
    03  FILLER                 PIC X(53). 
FD  FIRST-FILE 
    LABEL RECORDS ARE STANDARD. 
01  RECORD1. 
    03  FILLER                 PIC X(99). 
    03  R1-STATUS-CODE       PIC X. 
FD  SECOND-FILE 
    LABEL RECORDS ARE STANDARD. 
01  RECORD2. 
    03  FILLER                 PIC X(99). 
    03  R2-STATUS-CODE       PIC X. 
FD  OUTPUT-FILE 
    LABEL RECORDS ARE STANDARD. 
01  OUT-REC                    PIC X(100). 
WORKING-STORAGE SECTION. 
01  INITIAL-SORT-READ          PIC X   VALUE "Y". 
01  FILE01-COUNT               PIC 9(5) VALUE ZEROES. 
01  FILE02-COUNT               PIC 9(5) VALUE ZEROES. 
01  SORT-COUNT                 PIC 9(5) VALUE ZEROES. 
01  OUTPUT-COUNT               PIC 9(5) VALUE ZEROES. 
01  SAVE-SORT-REC. 
    03  SR-ACCOUNT-NUM         PIC X(8). 
    03  FILLER                 PIC X(32). 
    03  SR-AMOUNT              PIC S9(5)V99. 
    03  FILLER                 PIC X(53). 
PROCEDURE DIVISION. 
000-START SECTION. 
005-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY 
                      S-KEY-1 
                      S-KEY-2 
         INPUT PROCEDURE IS 010-GET-INPUT 
                       THRU 200-DONE-INPUT-GET 
         OUTPUT PROCEDURE IS 300-CREATE-OUTPUT-FILE 
                        THRU 600-DONE-CREATE. 
******************************************************** 
*   Notice the use of DISPLAY and record counters to   * 
*   produce sort statistics.                           * 
******************************************************** 
    DISPLAY "TOTAL FIRST-FILE RECORDS IS              " FILE01-COUNT. 
    DISPLAY "TOTAL SECOND-FILE RECORDS IS             " FILE02-COUNT. 
    DISPLAY "TOTAL NUMBER OF SORTED RECORDS IS        " SORT-COUNT. 
    DISPLAY "TOTAL NUMBER OF OUTPUT RECORDS IS        " OUTPUT-COUNT. 
************************************************************ 
*   At this point, you could transfer control to another   * 
*   section of the program  and continue processing.       * 
************************************************************ 
    DISPLAY "END OF PROGRAM SORTC". 
    STOP RUN. 
010-GET-INPUT SECTION. 
050-OPEN-FILES. 
    OPEN INPUT FIRST-FILE. 
100-READ-FIRST-FILE. 
    READ FIRST-FILE AT END 
        CLOSE FIRST-FILE 
        OPEN INPUT SECOND-FILE 
        GO TO 150-READ-SECOND-FILE. 
    ADD 1 TO FILE01-COUNT. 
    IF R1-STATUS-CODE = "D" 
        GO TO 100-READ-FIRST-FILE. 
    RELEASE SORT-REC FROM RECORD1. 
    GO TO 100-READ-FIRST-FILE. 
150-READ-SECOND-FILE. 
    READ SECOND-FILE AT END 
        CLOSE SECOND-FILE 
        GO TO 200-DONE-INPUT-GET. 
    ADD 1 TO FILE02-COUNT. 
    IF R2-STATUS-CODE = "D" 
        GO TO 150-READ-SECOND-FILE. 
    RELEASE SORT-REC FROM RECORD2. 
    GO TO 150-READ-SECOND-FILE. 
200-DONE-INPUT-GET SECTION. 
250-EXIT-PARAGRAPH. 
    EXIT. 
300-CREATE-OUTPUT-FILE SECTION. 
350-OPEN-OUTPUT. 
    OPEN OUTPUT OUTPUT-FILE. 
400-READ-SORT-FILE. 
    RETURN SORT-FILE AT END 
        PERFORM 500-WRITE-THE-OUTPUT 
        CLOSE OUTPUT-FILE 
        GO TO 600-DONE-CREATE. 
    ADD 1 TO SORT-COUNT. 
    IF INITIAL-SORT-READ = "Y" 
        MOVE SORT-REC TO SAVE-SORT-REC 
        MOVE "N" TO INITIAL-SORT-READ 
        GO TO 400-READ-SORT-FILE. 
450-COMPARE-ACCOUNT-NUM. 
    IF S-ACCOUNT-NUM = SR-ACCOUNT-NUM 
        ADD S-AMOUNT TO SR-AMOUNT 
        GO TO 400-READ-SORT-FILE. 
500-WRITE-THE-OUTPUT. 
    MOVE SAVE-SORT-REC TO OUT-REC. 
    WRITE OUT-REC. 
    ADD 1 TO OUTPUT-COUNT. 
550-GET-A-REC. 
    MOVE SORT-REC TO SAVE-SORT-REC. 
    GO TO 400-READ-SORT-FILE. 
600-DONE-CREATE SECTION. 
650-EXIT-PARAGRAPH. 
    EXIT. 

Example 9-10 shows how to use the COLLATING SEQUENCE IS phrase.

Example 9-10 Using the COLLATING SEQUENCE IS Phrase

IDENTIFICATION DIVISION. 
PROGRAM-ID. SORTD. 
************************************************** 
*   This program sorts a file into a non-ASCII   * 
*   collating sequence. The collating sequence   * 
*   is defined by the alphabet-name MYSEQUENCE   * 
*   in the SPECIAL-NAMES paragraph of the        * 
*   ENVIRONMENT DIVISION.                        * 
*   The collating sequence is:                   * 
*       1. The letters A to Z                    * 
*       2. The digits 0 to 9                     * 
************************************************** 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
SPECIAL-NAMES. 
    ALPHABET MYSEQUENCE IS 
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ". 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT INPUT-FILE ASSIGN TO "INPFIL". 
    SELECT OUTPUT-FILE ASSIGN TO "OUTFIL". 
    SELECT SORT-FILE ASSIGN TO "SRTFIL". 
DATA DIVISION. 
FILE SECTION. 
SD  SORT-FILE. 
01  SORT-REC. 
    03  S-KEY-1. 
        05  S-ACCOUNT-NAME     PIC X(23). 
    03  S-KEY-2. 
        05  S-AMOUNT           PIC S9(5)V99. 
FD  INPUT-FILE 
    LABEL RECORDS ARE STANDARD. 
01  IN-REC                     PIC X(30). 
FD  OUTPUT-FILE 
    LABEL RECORDS ARE STANDARD. 
01  OUT-REC                    PIC X(30). 
PROCEDURE DIVISION. 
000-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY 
                      S-KEY-1 
                      S-KEY-2 
         COLLATING SEQUENCE IS MYSEQUENCE 
         USING INPUT-FILE GIVING OUTPUT-FILE. 
************************************************************ 
*   At this point, you could transfer control to another   * 
*   section of the program and continue processing.        * 
************************************************************ 
    DISPLAY "END OF PROGRAM SORTD". 
    STOP RUN. 

Example 9-11 is an example of creating a new sort key.

Example 9-11 Creating a New Sort Key

IDENTIFICATION DIVISION. 
PROGRAM-ID. SORTE. 
************************************************ 
*   This program increases the size of the     * 
*   variable input records by a new six-       * 
*   character field and uses this field        * 
*   as the sort key.                           * 
************************************************ 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT INFILE ASSIGN TO "INFILE". 
    SELECT SORT-FILE ASSIGN TO "SRTFIL". 
    SELECT OUT-FILE ASSIGN TO "OUTFILE". 
DATA DIVISION. 
FILE SECTION. 
FD      INFILE 
        RECORD VARYING FROM 100 TO 490 CHARACTERS 
        DEPENDING ON IN-LENGTH. 
01      INREC. 
        03 ACCOUNT                 PIC 9(5). 
        03 INCOME-FIRST-QUARTER    PIC 9(5)V99. 
        03 INCOME-SECOND-QUARTER   PIC 9(5)V99. 
        03 INCOME-THIRD-QUARTER    PIC 9(5)V99. 
        03 INCOME-FOURTH-QUARTER   PIC 9(5)V99. 
        03 ORDER-COUNT             PIC 9(2). 
        03 ORDERS OCCURS 1 TO 7 TIMES 
            DEPENDING ON ORDER-COUNT. 
              05  ORDER-DATE       PIC 9(6). 
              05  FILLER           PIC X(59). 
SD      SORT-FILE 
        RECORD VARYING FROM 106 TO 496 CHARACTERS 
        DEPENDING ON SORT-LENGTH. 
01     SORT-REC. 
        03  SORT-ANNUAL-INCOME     PIC 9(6). 
        03  SORT-REST-OF-RECORD    PIC X(490). 
FD      OUT-FILE 
        RECORD VARYING FROM 106 TO 496 CHARACTERS 
        DEPENDING ON OUT-LENGTH. 
01      OUT-REC                    PIC X(496). 
WORKING-STORAGE SECTION. 
01  IN-LENGTH                      PIC 9(3) COMP. 
01  SORT-LENGTH                    PIC 9(3) COMP. 
01  OUT-LENGTH                     PIC 9(3) COMP. 
PROCEDURE DIVISION. 
000-START SECTION. 
005-SORT-HERE. 
    SORT SORT-FILE 
           ON DESCENDING SORT-ANNUAL-INCOME 
           INPUT PROCEDURE 010-GET-INPUT 
                      THRU 070-DONE-INPUT 
           OUTPUT PROCEDURE 100-WRITE-OUTPUT. 
    DISPLAY "END OF PROGRAM SORTE". 
    STOP RUN. 
010-GET-INPUT SECTION. 
020-OPEN-INPUT. 
    OPEN INPUT INFILE. 
030-READ-INPUT. 
    READ INFILE AT END 
        CLOSE INFILE 
        GO TO 070-DONE-INPUT. 
040-ADD-INCOME. 
    ADD INCOME-FIRST-QUARTER 
        INCOME-SECOND-QUARTER 
        INCOME-THIRD-QUARTER 
        INCOME-FOURTH-QUARTER 
        GIVING SORT-ANNUAL-INCOME. 
050-CREATE-SORT-REC. 
    ADD 6 IN-LENGTH GIVING SORT-LENGTH. 
    MOVE INREC TO SORT-REST-OF-RECORD. 
    RELEASE SORT-REC. 
    GO TO 030-READ-INPUT. 
070-DONE-INPUT SECTION. 
080-EXIT. 
    EXIT. 
100-WRITE-OUTPUT SECTION. 
110-OPEN. 
    OPEN OUTPUT OUT-FILE. 
120-WRITE. 
    RETURN SORT-FILE AT END 
        CLOSE OUT-FILE 
        GO TO 130-DONE. 
    MOVE SORT-LENGTH TO OUT-LENGTH. 
    WRITE OUT-REC. 
    GO TO 120-WRITE. 
130-DONE. 
    EXIT. 

Example 9-12 merges three identically sequenced files into one file.

Example 9-12 Merging Files

IDENTIFICATION DIVISION. 
PROGRAM-ID.    MERGE01. 
****************************************************** 
*   This program merges three identically sequenced  * 
*   regional sales files into one total sales file.  * 
*   The program adds sales amounts and writes one    * 
*   record for each product code.                    * 
****************************************************** 
ENVIRONMENT DIVISION. 
CONFIGURATION SECTION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT REGION1-SALES ASSIGN TO "REG1SLS". 
    SELECT REGION2-SALES ASSIGN TO "REG2SLS". 
    SELECT REGION3-SALES ASSIGN TO "REG3SLS". 
    SELECT MERGE-FILE    ASSIGN TO "MRGFILE". 
    SELECT TOTAL-SALES   ASSIGN TO "TOTLSLS". 
DATA DIVISION. 
FILE SECTION. 
FD  REGION1-SALES 
    LABEL RECORDS ARE STANDARD. 
01  REGION1-RECORD            PIC X(100). 
FD  REGION2-SALES 
    LABEL RECORDS ARE STANDARD. 
01  REGION2-RECORD            PIC X(100). 
FD  REGION3-SALES 
    LABEL RECORDS ARE STANDARD. 
01  REGION3-RECORD            PIC X(100). 
SD  MERGE-FILE. 
    01  MERGE-REC. 
        03  M-REGION-CODE     PIC XX. 
        03  M-PRODUCT-CODE    PIC X(10). 
        03  M-SALES-AMT       PIC S9(7)V99. 
        03  FILLER            PIC X(79). 
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. 
050-TALLY-AMOUNTS. 
    ADD M-SALES-AMT TO PRODUCT-AMT TOTAL-AMT. 
    IF M-REGION-CODE = "01" 
           ADD M-SALES-AMT TO REGION1-AMT. 
    IF M-REGION-CODE = "02" 
           ADD M-SALES-AMT TO REGION2-AMT. 
    IF M-REGION-CODE = "03" 
           ADD M-SALES-AMT TO REGION3-AMT. 
100-DONE-TOTAL-SALES SECTION. 
120-DONE. 
    EXIT. 


Previous Next Contents Index