SORT with USING and GIVING

Every control-break report needs a sorted input, and COBOL has the sort in the language. SORT is not a library call: the work file is declared in the FILE SECTION like any other, the keys are named in the verb, and the compiler writes the sorting code into the program.

SORT with USING and GIVING

Three files take part: the input file, the work file described by an SD, and the output file. The keys are fields of the SD record, and the sort reads the input, sorts and writes the output without a single READ in the program:

IDENTIFICATION DIVISION. PROGRAM-ID. SORT1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IN-F ASSIGN TO "unsorted.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-IN-ST. SELECT SORT-F ASSIGN TO "sortw.dat" FILE STATUS IS WS-SORT-ST. SELECT OUT-F ASSIGN TO "sorted.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-OUT-ST. DATA DIVISION. FILE SECTION. FD IN-F. 01 IN-REC. 05 IN-REGION PIC X(2). 05 IN-NAME PIC X(8). 05 IN-AMOUNT PIC 9(5). SD SORT-F. 01 SORT-REC. 05 S-REGION PIC X(2). 05 S-NAME PIC X(8). 05 S-AMOUNT PIC 9(5). FD OUT-F. 01 OUT-REC. 05 OUT-REGION PIC X(2). 05 OUT-NAME PIC X(8). 05 OUT-AMOUNT PIC 9(5). WORKING-STORAGE SECTION. 01 WS-IN-ST PIC XX. 01 WS-SORT-ST PIC XX. 01 WS-OUT-ST PIC XX. PROCEDURE DIVISION. OPEN OUTPUT IN-F. MOVE "S1" TO IN-REGION. MOVE "pear" TO IN-NAME. MOVE 300 TO IN-AMOUNT. WRITE IN-REC. MOVE "N1" TO IN-REGION. MOVE "fig" TO IN-NAME. MOVE 40 TO IN-AMOUNT. WRITE IN-REC. MOVE "N1" TO IN-REGION. MOVE "apple" TO IN-NAME. MOVE 25 TO IN-AMOUNT. WRITE IN-REC. MOVE "S1" TO IN-REGION. MOVE "kiwi" TO IN-NAME. MOVE 500 TO IN-AMOUNT. WRITE IN-REC. MOVE "N2" TO IN-REGION. MOVE "date" TO IN-NAME. MOVE 75 TO IN-AMOUNT. WRITE IN-REC. CLOSE IN-F. SORT SORT-F ON ASCENDING KEY S-REGION ON ASCENDING KEY S-NAME USING IN-F GIVING OUT-F. DISPLAY "sort " WS-SORT-ST. OPEN INPUT OUT-F. PERFORM UNTIL WS-OUT-ST = "10" READ OUT-F AT END MOVE "10" TO WS-OUT-ST NOT AT END DISPLAY OUT-REGION " " OUT-NAME " " OUT-AMOUNT END-READ END-PERFORM. CLOSE OUT-F. STOP RUN.

cobc -x -free -Wall -o sort1 sort1.cob
./sort1

sort 00
N1 apple 00025
N1 fig 00040
N2 date 00075
S1 kiwi 00500
S1 pear 00300

Nothing in the program reads or writes a record: the verb does the three steps, and WS-SORT-ST carries the status of the whole operation. The SD record must describe the same bytes as the input and the output records, because the sort moves those bytes; the names on the two sides may differ, as they do here.

GnuCOBOL asks for an ASSIGN clause on the sort file as well, and it writes nothing you can see: the work file is temporary and is removed when the verb ends, so a sort of a large file needs free space in the run folder and not a file name you maintain.

Several sort keys

A second key breaks ties of the first, a third breaks ties of the second, and each key says its own direction. Here the regions stay in ascending order and the amounts inside a region fall:

IDENTIFICATION DIVISION. PROGRAM-ID. SORT2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IN-F ASSIGN TO "orders.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-IN-ST. SELECT SORT-F ASSIGN TO "sortw2.dat" FILE STATUS IS WS-SORT-ST. SELECT OUT-F ASSIGN TO "byamount.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-OUT-ST. DATA DIVISION. FILE SECTION. FD IN-F. 01 IN-REC. 05 IN-REGION PIC X(2). 05 IN-NAME PIC X(8). 05 IN-AMOUNT PIC 9(5). SD SORT-F. 01 SORT-REC. 05 S-REGION PIC X(2). 05 S-NAME PIC X(8). 05 S-AMOUNT PIC 9(5). FD OUT-F. 01 OUT-REC. 05 OUT-REGION PIC X(2). 05 OUT-NAME PIC X(8). 05 OUT-AMOUNT PIC 9(5). WORKING-STORAGE SECTION. 01 WS-IN-ST PIC XX. 01 WS-SORT-ST PIC XX. 01 WS-OUT-ST PIC XX. PROCEDURE DIVISION. OPEN OUTPUT IN-F. MOVE "N1" TO IN-REGION. MOVE "fig" TO IN-NAME. MOVE 40 TO IN-AMOUNT. WRITE IN-REC. MOVE "N1" TO IN-REGION. MOVE "apple" TO IN-NAME. MOVE 250 TO IN-AMOUNT. WRITE IN-REC. MOVE "N1" TO IN-REGION. MOVE "kiwi" TO IN-NAME. MOVE 90 TO IN-AMOUNT. WRITE IN-REC. MOVE "S1" TO IN-REGION. MOVE "pear" TO IN-NAME. MOVE 300 TO IN-AMOUNT. WRITE IN-REC. MOVE "S1" TO IN-REGION. MOVE "date" TO IN-NAME. MOVE 5 TO IN-AMOUNT. WRITE IN-REC. CLOSE IN-F. SORT SORT-F ON ASCENDING KEY S-REGION ON DESCENDING KEY S-AMOUNT USING IN-F GIVING OUT-F. DISPLAY "sort " WS-SORT-ST. OPEN INPUT OUT-F. PERFORM UNTIL WS-OUT-ST = "10" READ OUT-F AT END MOVE "10" TO WS-OUT-ST NOT AT END DISPLAY OUT-REGION " " OUT-NAME " " OUT-AMOUNT END-READ END-PERFORM. CLOSE OUT-F. STOP RUN.

cobc -x -free -Wall -o sort2 sort2.cob
./sort2

sort 00
N1 apple 00250
N1 kiwi 00090
N1 fig 00040
S1 pear 00300
S1 date 00005

The amounts inside N1 are 250, 90, 40 and inside S1 they are 300, 5. The order of the two keys in the verb is the order of their importance, and the direction of each is written with the key, not with the file.

A SORT without a source

The verb needs either USING or an INPUT PROCEDURE, and either GIVING or an OUTPUT PROCEDURE. Leave out the first pair and the compiler says so in two lines:

IDENTIFICATION DIVISION. PROGRAM-ID. BAD6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IN-F ASSIGN TO "orders.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-IN-ST. SELECT SORT-F ASSIGN TO "sortw2.dat" FILE STATUS IS WS-SORT-ST. SELECT OUT-F ASSIGN TO "byamount.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS WS-OUT-ST. DATA DIVISION. FILE SECTION. FD IN-F. 01 IN-REC. 05 IN-REGION PIC X(2). 05 IN-NAME PIC X(8). 05 IN-AMOUNT PIC 9(5). SD SORT-F. 01 SORT-REC. 05 S-REGION PIC X(2). 05 S-NAME PIC X(8). 05 S-AMOUNT PIC 9(5). FD OUT-F. 01 OUT-REC. 05 OUT-REGION PIC X(2). 05 OUT-NAME PIC X(8). 05 OUT-AMOUNT PIC 9(5). WORKING-STORAGE SECTION. 01 WS-IN-ST PIC XX. 01 WS-SORT-ST PIC XX. 01 WS-OUT-ST PIC XX. PROCEDURE DIVISION. OPEN OUTPUT IN-F. MOVE "N1" TO IN-REGION. MOVE "fig" TO IN-NAME. MOVE 40 TO IN-AMOUNT. WRITE IN-REC. CLOSE IN-F. SORT SORT-F ON ASCENDING KEY S-REGION. DISPLAY "sort " WS-SORT-ST. STOP RUN.

cobc -x -free -Wall -o bad6 bad6.cob

bad6.cob:46: error: file SORT requires USING or INPUT PROCEDURE
44 | CLOSE IN-F.
45 | SORT SORT-F
46 > ON ASCENDING KEY S-REGION.
47 | DISPLAY "sort " WS-SORT-ST.
48 | STOP RUN.
bad6.cob:46: error: file SORT requires GIVING or OUTPUT PROCEDURE

The missing output half is reported too, so the program has to be given a source and a sink in one edit. The next lesson gives it procedures instead of files, which is how a sort that also has to transform its records is written.

Exercises

  1. Add the amount as a second key to sort1 and read the names inside N1 again.
  2. Sort sort1 by name only. Which record comes first, and why?
  3. Print WS-IN-ST and WS-OUT-ST in sort2. Which values do they hold after the sort, and what does that say about reading the input file?
  4. Change the SD record of sort2 to a single PIC X(15). What does the output look like, and why?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which division describes the sort work file?

    Answer

    The FILE SECTION of the DATA DIVISION, with an SD entry

    The SD entry describes the record the sort moves, and the keys are its fields.

  2. What does the SORT verb do with USING and GIVING?

    Answer

    Reads the input file, sorts it and writes the output file, with no READ or WRITE in the program

    The program had no READ before the sort and read the result only afterwards.

  3. What does ON DESCENDING KEY change?

    Answer

    The direction of that key only, while the other keys keep theirs

    The regions stayed ascending and the amounts fell inside each region.

  4. Which error does a SORT with neither USING nor INPUT PROCEDURE print?

    Answer

    file SORT requires USING or INPUT PROCEDURE

    That is the first line of the failing build on the page.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC X(2) VALUE "N1". 01 W-B PIC X(2) VALUE "S1". PROCEDURE DIVISION. IF W-A IS LESS THAN W-B DISPLAY "N before S" ELSE DISPLAY "S before N" END-IF. STOP RUN.

    Answer

    N before S

    An alphanumeric comparison follows the character order, and N comes before S.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-S ASSIGN TO "q6s.dat" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-S. 01 R-S. 05 K-S PIC X(2). 05 A-S PIC 9(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. 01 W-EOF PIC X VALUE "N". 01 W-MAX PIC 9(2) VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT F-S. MOVE "AA" TO K-S. MOVE 30 TO A-S. WRITE R-S. MOVE "BB" TO K-S. MOVE 10 TO A-S. WRITE R-S. CLOSE F-S. OPEN INPUT F-S. PERFORM UNTIL W-EOF = "Y" READ F-S AT END MOVE "Y" TO W-EOF NOT AT END IF A-S IS GREATER THAN W-MAX MOVE A-S TO W-MAX END-IF END-READ END-PERFORM. DISPLAY W-MAX. CLOSE F-S. STOP RUN.

    Answer

    30

    The loop keeps the largest amount of the two records.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 T-AMT PIC 9(2) VALUE 25. 01 T-MIN PIC 9(2) VALUE 99. PROCEDURE DIVISION. IF T-AMT IS LESS THAN T-MIN MOVE T-AMT TO T-MIN END-IF. DISPLAY T-MIN. STOP RUN.

    Answer

    25

    The smaller value replaces the minimum, which is the comparison a sort makes.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9 VALUE 0. 01 W-I PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING W-I FROM 1 BY 1 UNTIL W-I IS GREATER THAN 5 IF W-I IS GREATER THAN 3 ADD 1 TO W-N END-IF END-PERFORM. DISPLAY W-N. STOP RUN.

    Answer

    2

    The values 4 and 5 are above three, so the counter ends at two.

  9. Why must the SD record describe the same bytes as the input and output records?

    Answer

    The sort moves the bytes of the record, so only the names on each side may differ

    The three descriptions in the page use S-, IN- and OUT- names over one 15-byte record.

  10. A job sorts a file and then runs a report on the output. What has to be true of the report step?

    Answer

    It reads the GIVING file, because the sorted records went there

    The work file is temporary and gone; the sorted data is in the GIVING file.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev