Compaq COBOL
User Manual


Previous Contents Index


Chapter 9
Using the SORT and MERGE Statements

This chapter includes the following information about using the SORT and MERGE statements to sort and merge records for sequential, line sequential (Alpha only), relative, and indexed files:

9.1 Sorting Data with the SORT Statement

The SORT statement provides a wide range of sorting capabilities and options. To establish a SORT routine, you do the following:

  1. Declare the sort file with an Environment Division SELECT statement.
  2. Use a Data Division Sort Description (SD) entry to define the sort file's characteristics.
  3. Use a Procedure Division SORT statement.

The following program segments demonstrate SORT program coding:

SELECT Statement (Environment Division)


SELECT SORT-FILE ASSIGN TO "SRTFIL" 

An SD File Description Entry (Data Division)


SD  SORT-FILE. 
01  SORT-RECORD. 
     05 SORT-KEY1    PIC X(5). 
     05 SOME-DATA    PIC X(25). 
     05 SORT-KEY2    PIC XX. 

Note

You can place the sort file anywhere in the FILE SECTION, but you must use a Sort Description (SD) level indicator, not a File Description (FD) level indicator. Also, you cannot use the SD file for any other purpose in the COBOL program.

SORT Statement (Procedure Division)


SORT SORT-FILE 
     ASCENDING KEY S-NAME 
     USING NAME-FILE 
     GIVING NEW-FILE. 

The SORT statement names a sort file, sort keys, an input file, and an output file. An explanation of sort keys follows.

Sorting Concepts

Records are sorted based on the data values in the sort keys. Sort keys identify the location of a record or the ordering of data. The following example depicts unsorted employee name and address records used for creating mailing labels:
Smith, Joe 234 Ash St. New Boston NH 04356
Jones, Bill 12 Birch St. Gardner MA 01430
Baker, Tom 78 Oak St. Ayer MA 01510
Thomas, Pete 555 Maple St. Maynard MA 01234
Morris, Dick 21 Harris St. Acton ME 05670

If you sort the addresses in the previous example in ascending order using the zip code as the sort key, the mailing labels are printed in the order shown in the following example:
          SORT KEY
Thomas, Pete 555 Maple St. Maynard MA 01234
Jones, Bill 12 Birch St. Gardner MA 01430
Baker, Tom 78 Oak St. Ayer MA 01510
Smith, Joe 234 Ash St. New Boston NH 04356
Morris, Dick 21 Harris St. Acton ME 05670

Also, records can be sorted on more that one key at a time. If you need an alphabetical listing of all employees within each state, you can sort on the state code first (major sort key) and employee name second (minor sort key).

For example, if you sort the file in ascending order by state and last name, the employee names and addresses appear in the order shown in the following example:
SORT KEY
(minor)
      SORT KEY
(major)
 
Baker, Tom 78 Oak St. Ayer MA 01510
Jones, Bill 12 Birch St. Gardner MA 01430
Thomas, Pete 555 Maple St. Maynard MA 01234
Morris, Dick 21 Harris St. Acton ME 05670
Smith, Joe 234 Ash St. New Boston NH 04356

9.1.1 File Organization Considerations for Sorting

You can sort any file regardless of its organization; furthermore, the organization of the output file can differ from that of the input file. For example, a sort can have a sequential input file and a relative output file. In this case, the relative key for the first record returned from the sort is 1; the second record's relative key is 2; and so forth. However, if an indexed file is described as output in the GIVING or OUTPUT PROCEDURE phrases, the first sort key associated with the ASCENDING phrase must specify the same character positions specified by the RECORD KEY phrase for that file.

Sections 9.1.2, 9.1.3, and 9.1.4 describe the ASCENDING and DESCENDING KEY phrases, the USING and GIVING phrases, and the INPUT PROCEDURE and OUTPUT PROCEDURE phrases for sorting.

9.1.2 Specifying Sort Parameters with the ASCENDING and DESCENDING KEY Phrases

Use the Data Division ASCENDING and DESCENDING KEY phrases to specify your sort parameters. The order of data names determines the sort hierarchy; that is, the major sort key is the first data name entered, while the minor sort key is the last data name entered.

In the following example, the hierarchy of the sort is SORT-KEY-1, SORT-KEY-2, SORT-KEY-3.


SORT SORT-FILE 
    ASCENDING KEY SORT-KEY-1 SORT-KEY-2 
    DESCENDING KEY SORT-KEY-3 

9.1.3 Resequencing Files with the USING and GIVING Phrases

If you only need to resequence a file, use the USING and GIVING phrases of the SORT statement. The USING phrase opens the input file, then reads and releases its records to the sort. The GIVING phrase opens and writes sorted records to the output file.

Note that you cannot manipulate data with either the USING or the GIVING phrases.

Consider this SORT statement:


SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1 
     USING INPUT-FILE GIVING OUTPUT-FILE. 

It does the following:

  1. Opens INPUT-FILE
  2. Reads all records in INPUT-FILE and releases them to the sort
  3. Sorts the records in ascending sequence using the data in SORT-KEY-1
  4. Opens the output file and writes the sorted records to OUTPUT-FILE
  5. Closes all files used in the SORT statement

9.1.4 Manipulating Data Before and After Sorting with the INPUT PROCEDURE and OUTPUT PROCEDURE Phrases

You can manipulate data before and after sorting by using the INPUT PROCEDURE and OUTPUT PROCEDURE phrases, and sort only some of the information in a file. For example, these phrases allow you to use only those input records and/or input data fields you need.

The INPUT PROCEDURE phrase replaces the USING phrase when you want to manipulate data entering the sort. The SORT statement transfers control to the sections or paragraphs named in the INPUT PROCEDURE phrase. You then use COBOL statements to open and read files, and manipulate the data. You use the RELEASE statement to transfer records to the sort. After the last statement of the input procedure executes, control is given to the sort, and the records are subsequently sorted.

After the records are sorted, the SORT statement transfers control to the sections or paragraphs named in the OUTPUT PROCEDURE phrase. This phrase replaces the GIVING phrase when you want to manipulate data in the sort. You can use COBOL statements to open files and manipulate data. You use the RETURN statement to transfer records from the sort. For example, you can use the RETURN statement to retrieve the sorted records for printing a report.

Example 9-1 shows a sample sort using the INPUT and OUTPUT procedures.

Example 9-1 INPUT and OUTPUT PROCEDURE Phrases

IDENTIFICATION DIVISION. 
PROGRAM-ID.  EX0901. 
ENVIRONMENT DIVISION. 
INPUT-OUTPUT SECTION. 
 
FILE-CONTROL. 
    SELECT INPUT-FILE   ASSIGN TO "input.dat". 
    SELECT OUTPUT-FILE  ASSIGN TO "output.dat". 
    SELECT SORT-FILE    ASSIGN TO "sort.dat". 
 
DATA DIVISION. 
FILE SECTION. 
FD  INPUT-FILE. 
01  INPUT-RECORD     PIC X(100). 
FD  OUTPUT-FILE. 
01  OUTPUT-RECORD    PIC X(100). 
SD  SORT-FILE. 
01  SORT-RECORD      PIC X(100). 
01  SORT-KEY-1       PIC XXX. 
01  SORT-KEY-2       PIC XXX. 
 
WORKING-STORAGE SECTION. 
 
PROCEDURE DIVISION. 
000-SORT SECTION. 
010-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1 
                   ON DESCENDING KEY SORT-KEY-2 
                   INPUT PROCEDURE IS 050-RETRIEVE-INPUT 
                                THRU 100-DONE-INPUT 
                   OUTPUT PROCEDURE IS 200-WRITE-OUTPUT 
                                THRU 230-DONE-OUTPUT. 
    DISPLAY "END OF SORT". 
    STOP RUN. 
050-RETRIEVE-INPUT SECTION. 
060-OPEN-INPUT. 
    OPEN INPUT INPUT-FILE. 
070-READ-INPUT. 
    READ INPUT-FILE AT END 
        CLOSE INPUT-FILE 
        GO TO 100-DONE-INPUT. 
    MOVE INPUT-RECORD TO SORT-RECORD. 
*********************************************************** 
* You can add, change, or delete records before sorting   * 
* using COBOL data manipulation                           * 
* techniques.                                             * 
*********************************************************** 
    RELEASE SORT-RECORD. 
    GO TO 070-READ-INPUT. 
100-DONE-INPUT SECTION. 
110-EXIT-INPUT. 
    EXIT. 
200-WRITE-OUTPUT SECTION. 
210-OPEN-OUTPUT. 
    OPEN OUTPUT OUTPUT-FILE. 
220-GET-SORTED-RECORDS. 
    RETURN SORT-FILE AT END 
        CLOSE OUTPUT-FILE 
        GO TO 230-DONE-OUTPUT. 
    MOVE SORT-RECORD TO OUTPUT-RECORD. 
*********************************************************** 
* You can add, change, or delete sorted records           * 
* using COBOL data manipulation                           * 
* techniques.                                             * 
*********************************************************** 
    WRITE OUTPUT-RECORD. 
    GO TO 220-GET-SORTED-RECORDS. 
230-DONE-OUTPUT SECTION. 
240-EXIT-OUTPUT. 
    EXIT. 

You can combine the INPUT PROCEDURE with the GIVING phrases, or the USING with the OUTPUT PROCEDURE phrases. In Example 9-2, the USING phrase replaces the INPUT PROCEDURE phrase used in Example 9-1.

Note

You cannot access records released to the sort-file after execution of the SORT statement ends.

Example 9-2 USING Phrase Replaces INPUT PROCEDURE Phrase

. 
. 
. 
PROCEDURE DIVISION. 
000-SORT SECTION. 
010-DO-THE-SORT. 
    SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1 
                   ON DESCENDING KEY SORT-KEY-2 
                   USING INPUT-FILE 
                   OUTPUT PROCEDURE IS 200-WRITE-OUTPUT 
                                  THRU 230-DONE-OUTPUT. 
    DISPLAY "END OF SORT". 
    STOP RUN. 
200-WRITE-OUTPUT SECTION. 
210-OPEN-OUTPUT. 
    OPEN OUTPUT OUTPUT-FILE. 
220-GET-SORTED-RECORDS. 
    RETURN SORT-FILE AT END 
        CLOSE OUTPUT-FILE 
        GO TO 230-DONE-OUTPUT. 
    MOVE SORT-RECORD TO OUTPUT-RECORD. 
    WRITE OUTPUT-RECORD. 
    GO TO 220-GET-SORTED-RECORDS. 
230-DONE-OUTPUT SECTION. 
240-EXIT-OUTPUT. 
    EXIT. 

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 (Alpha)

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, refer to the SORT statement description in the Procedure Division chapter of the Compaq COBOL Reference Manual. <>


Previous Next Contents Index