Sorting operations in COBOL arrange records in a specific order (ascending or descending) based on one or more key fields. COBOL provides powerful sorting capabilities through the SORT and MERGE statements, which are essential for organizing data, preparing reports, and optimizing data access. Understanding sorting operations is crucial for processing large volumes of data efficiently in mainframe environments.
This comprehensive guide will teach you how to use the SORT statement to arrange unsorted records, the MERGE statement to combine already-sorted files, sort work files (SD), input and output procedures, multi-key sorting, and best practices for efficient sorting operations. Whether you're preparing data for reports, organizing transaction records, or combining data from multiple sources, mastering sorting operations is essential for effective COBOL programming.
Sorting in COBOL is the process of arranging records in ascending or descending order based on specified key fields. Sorting is used to:
COBOL provides two main statements for sorting:
A sort work file (SD - Sort Description) is a special file used by the SORT and MERGE statements to temporarily store records during the sorting process. The sort work file is defined in the FILE SECTION with SD instead of FD.
123456FILE SECTION. SD SORT-WORK-FILE. 01 SORT-RECORD. 05 SORT-KEY-1 PIC X(10). 05 SORT-KEY-2 PIC 9(8). 05 SORT-DATA PIC X(50).
Important characteristics of sort work files:
The SORT statement arranges records in ascending or descending order based on specified key fields. SORT uses a sort work file to temporarily store records during the sorting process.
123456SORT sort-work-file ON ASCENDING KEY key-field-1 [ON ASCENDING KEY key-field-2 ...] [ON DESCENDING KEY key-field-n ...] USING input-file GIVING output-file
Components:
123456789101112131415161718192021222324252627282930313233343536373839404142IDENTIFICATION DIVISION. PROGRAM-ID. SIMPLE-SORT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "INPUT.DAT" ORGANIZATION IS SEQUENTIAL. SELECT OUTPUT-FILE ASSIGN TO "OUTPUT.DAT" ORGANIZATION IS SEQUENTIAL. SELECT SORT-WORK ASSIGN TO "SORTWORK". DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD. 05 CUSTOMER-ID PIC 9(8). 05 CUSTOMER-NAME PIC X(30). 05 ACCOUNT-BALANCE PIC 9(8)V99. FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 OUT-CUSTOMER-ID PIC 9(8). 05 OUT-CUSTOMER-NAME PIC X(30). 05 OUT-ACCOUNT-BALANCE PIC 9(8)V99. SD SORT-WORK. 01 SORT-RECORD. 05 SORT-CUSTOMER-ID PIC 9(8). 05 SORT-CUSTOMER-NAME PIC X(30). 05 SORT-ACCOUNT-BALANCE PIC 9(8)V99. PROCEDURE DIVISION. MAIN-LOGIC. *> Sort input file by customer ID in ascending order SORT SORT-WORK ON ASCENDING KEY SORT-CUSTOMER-ID USING INPUT-FILE GIVING OUTPUT-FILE DISPLAY "Sort completed successfully" STOP RUN.
This example sorts records from INPUT-FILE by customer ID in ascending order and writes the sorted records to OUTPUT-FILE. The SORT statement handles reading from the input file, sorting the records, and writing to the output file automatically.
You can sort by multiple keys by specifying multiple ON ASCENDING KEY or ON DESCENDING KEY clauses. The first key is the primary sort key, subsequent keys are secondary, tertiary, etc.
12345678910111213PROCEDURE DIVISION. MAIN-LOGIC. *> Sort by customer ID (ascending), then by date (ascending), *> then by amount (descending) SORT SORT-WORK ON ASCENDING KEY SORT-CUSTOMER-ID ON ASCENDING KEY SORT-TRANSACTION-DATE ON DESCENDING KEY SORT-AMOUNT USING INPUT-FILE GIVING OUTPUT-FILE DISPLAY "Multi-key sort completed" STOP RUN.
In this example, records are sorted first by CUSTOMER-ID (ascending), then by TRANSACTION-DATE (ascending) for records with the same CUSTOMER-ID, and finally by AMOUNT (descending) for records with the same CUSTOMER-ID and TRANSACTION-DATE. This creates a hierarchical sort order.
INPUT PROCEDURE allows you to process records before they're sorted. Instead of USING (which uses all records from the input file as-is), INPUT PROCEDURE lets you read from input files, filter records, transform data, or add calculated fields before releasing records to the sort work file.
1234SORT sort-work-file ON ASCENDING KEY key-field INPUT PROCEDURE IS procedure-name GIVING output-file
12345678910111213141516171819202122232425262728293031323334353637PROCEDURE DIVISION. MAIN-LOGIC. SORT SORT-WORK ON ASCENDING KEY SORT-CUSTOMER-ID INPUT PROCEDURE IS PROCESS-INPUT GIVING OUTPUT-FILE DISPLAY "Sort with input procedure completed" STOP RUN. PROCESS-INPUT SECTION. OPEN INPUT INPUT-FILE READ INPUT-FILE AT END MOVE 'Y' TO EOF-FLAG END-READ PERFORM UNTIL EOF-FLAG = 'Y' *> Filter: only process records with balance > 0 IF ACCOUNT-BALANCE > 0 *> Transform: move input record to sort record MOVE CUSTOMER-ID TO SORT-CUSTOMER-ID MOVE CUSTOMER-NAME TO SORT-CUSTOMER-NAME MOVE ACCOUNT-BALANCE TO SORT-ACCOUNT-BALANCE *> Release record to sort work file RELEASE SORT-RECORD END-IF READ INPUT-FILE AT END MOVE 'Y' TO EOF-FLAG END-READ END-PERFORM CLOSE INPUT-FILE.
In this example, the INPUT PROCEDURE reads from INPUT-FILE, filters records (only processes records with balance > 0), and RELEASEs valid records to the sort work file. Only filtered records are sorted and written to the output file.
OUTPUT PROCEDURE allows you to process sorted records after sorting. Instead of GIVING (which writes all sorted records to the output file as-is), OUTPUT PROCEDURE lets you return sorted records from the sort work file and process them (write to output, perform calculations, generate reports, etc.).
1234SORT sort-work-file ON ASCENDING KEY key-field USING input-file OUTPUT PROCEDURE IS procedure-name
123456789101112131415161718192021222324252627282930313233PROCEDURE DIVISION. MAIN-LOGIC. SORT SORT-WORK ON ASCENDING KEY SORT-CUSTOMER-ID USING INPUT-FILE OUTPUT PROCEDURE IS PROCESS-OUTPUT DISPLAY "Sort with output procedure completed" STOP RUN. PROCESS-OUTPUT SECTION. OPEN OUTPUT OUTPUT-FILE PERFORM UNTIL NO-MORE-RECORDS *> Return next sorted record from sort work file RETURN SORT-WORK AT END MOVE 'Y' TO END-FLAG NOT AT END *> Process sorted record MOVE SORT-CUSTOMER-ID TO OUT-CUSTOMER-ID MOVE SORT-CUSTOMER-NAME TO OUT-CUSTOMER-NAME MOVE SORT-ACCOUNT-BALANCE TO OUT-ACCOUNT-BALANCE *> Write processed record to output WRITE OUTPUT-RECORD ADD 1 TO RECORD-COUNT END-RETURN END-PERFORM CLOSE OUTPUT-FILE DISPLAY "Records processed: " RECORD-COUNT.
In this example, the OUTPUT PROCEDURE returns sorted records from the sort work file, processes them (moves to output record structure), writes them to the output file, and tracks the record count. This allows custom processing of sorted records.
RELEASE and RETURN are special statements used with sort work files:
1234567891011121314151617PROCESS-INPUT SECTION. OPEN INPUT INPUT-FILE PERFORM UNTIL EOF-FLAG = 'Y' READ INPUT-FILE AT END MOVE 'Y' TO EOF-FLAG NOT AT END *> Process and transform record MOVE INPUT-RECORD TO SORT-RECORD *> Release to sort work file RELEASE SORT-RECORD END-READ END-PERFORM CLOSE INPUT-FILE.
123456789101112131415PROCESS-OUTPUT SECTION. OPEN OUTPUT OUTPUT-FILE PERFORM UNTIL END-FLAG = 'Y' RETURN SORT-WORK AT END MOVE 'Y' TO END-FLAG NOT AT END *> Process sorted record MOVE SORT-RECORD TO OUTPUT-RECORD WRITE OUTPUT-RECORD END-RETURN END-PERFORM CLOSE OUTPUT-FILE.
The MERGE statement combines two or more already-sorted files into a single sorted file. MERGE is more efficient than sorting all records together because it only merges already-sorted sequences.
1234MERGE sort-work-file ON ASCENDING KEY key-field USING file-1 file-2 [file-3 ...] GIVING output-file
Important: All input files must already be sorted by the same key(s) specified in the MERGE statement.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546IDENTIFICATION DIVISION. PROGRAM-ID. MERGE-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILE-A ASSIGN TO "FILEA.DAT" ORGANIZATION IS SEQUENTIAL. SELECT FILE-B ASSIGN TO "FILEB.DAT" ORGANIZATION IS SEQUENTIAL. SELECT MERGED-FILE ASSIGN TO "MERGED.DAT" ORGANIZATION IS SEQUENTIAL. SELECT SORT-WORK ASSIGN TO "SORTWORK". DATA DIVISION. FILE SECTION. FD FILE-A. 01 RECORD-A. 05 KEY-FIELD-A PIC 9(8). 05 DATA-FIELD-A PIC X(50). FD FILE-B. 01 RECORD-B. 05 KEY-FIELD-B PIC 9(8). 05 DATA-FIELD-B PIC X(50). FD MERGED-FILE. 01 MERGED-RECORD. 05 MERGED-KEY PIC 9(8). 05 MERGED-DATA PIC X(50). SD SORT-WORK. 01 SORT-RECORD. 05 SORT-KEY PIC 9(8). 05 SORT-DATA PIC X(50). PROCEDURE DIVISION. MAIN-LOGIC. *> Merge two sorted files by key field MERGE SORT-WORK ON ASCENDING KEY SORT-KEY USING FILE-A FILE-B GIVING MERGED-FILE DISPLAY "Merge completed successfully" STOP RUN.
This example merges two already-sorted files (FILE-A and FILE-B) into a single sorted file (MERGED-FILE). Both input files must be sorted by the same key field. MERGE reads from both files simultaneously, compares keys, and writes records in order.
Here's a complete example demonstrating sorting with input and output procedures:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132IDENTIFICATION DIVISION. PROGRAM-ID. COMPLETE-SORT-EXAMPLE. AUTHOR. MainframeMaster Tutorial. REMARKS. Complete example demonstrating SORT with INPUT and OUTPUT procedures. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TRANSACTION-FILE ASSIGN TO "TRANS.DAT" ORGANIZATION IS SEQUENTIAL. SELECT SORTED-REPORT ASSIGN TO "REPORT.DAT" ORGANIZATION IS SEQUENTIAL. SELECT SORT-WORK ASSIGN TO "SORTWORK". DATA DIVISION. FILE SECTION. FD TRANSACTION-FILE. 01 TRANSACTION-RECORD. 05 TRANS-CUSTOMER-ID PIC 9(8). 05 TRANS-DATE PIC 9(8). 05 TRANS-AMOUNT PIC S9(8)V99. 05 TRANS-TYPE PIC X(1). 05 TRANS-DESCRIPTION PIC X(40). FD SORTED-REPORT. 01 REPORT-RECORD. 05 RPT-CUSTOMER-ID PIC 9(8). 05 RPT-DATE PIC 9(8). 05 RPT-AMOUNT PIC S9(8)V99. 05 RPT-TYPE PIC X(1). 05 RPT-DESCRIPTION PIC X(40). SD SORT-WORK. 01 SORT-RECORD. 05 SORT-CUSTOMER-ID PIC 9(8). 05 SORT-DATE PIC 9(8). 05 SORT-AMOUNT PIC S9(8)V99. 05 SORT-TYPE PIC X(1). 05 SORT-DESCRIPTION PIC X(40). WORKING-STORAGE SECTION. 01 FLAGS. 05 EOF-FLAG PIC X(1) VALUE 'N'. 88 END-OF-FILE VALUE 'Y'. 05 END-FLAG PIC X(1) VALUE 'N'. 88 NO-MORE-RECORDS VALUE 'Y'. 01 COUNTERS. 05 INPUT-COUNT PIC 9(6) VALUE 0. 05 OUTPUT-COUNT PIC 9(6) VALUE 0. 05 FILTERED-COUNT PIC 9(6) VALUE 0. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Transaction Sorting System ===" DISPLAY " " *> Sort transactions by customer ID, then by date, then by amount (descending) SORT SORT-WORK ON ASCENDING KEY SORT-CUSTOMER-ID ON ASCENDING KEY SORT-DATE ON DESCENDING KEY SORT-AMOUNT INPUT PROCEDURE IS PROCESS-INPUT OUTPUT PROCEDURE IS PROCESS-OUTPUT DISPLAY " " DISPLAY "Sort processing complete:" DISPLAY " Input records read: " INPUT-COUNT DISPLAY " Records filtered: " FILTERED-COUNT DISPLAY " Output records written: " OUTPUT-COUNT STOP RUN. PROCESS-INPUT SECTION. DISPLAY "Processing input records..." OPEN INPUT TRANSACTION-FILE READ TRANSACTION-FILE AT END MOVE 'Y' TO EOF-FLAG END-READ PERFORM UNTIL END-OF-FILE ADD 1 TO INPUT-COUNT *> Filter: only process valid transactions (amount != 0) IF TRANS-AMOUNT NOT = 0 *> Move to sort record MOVE TRANS-CUSTOMER-ID TO SORT-CUSTOMER-ID MOVE TRANS-DATE TO SORT-DATE MOVE TRANS-AMOUNT TO SORT-AMOUNT MOVE TRANS-TYPE TO SORT-TYPE MOVE TRANS-DESCRIPTION TO SORT-DESCRIPTION *> Release to sort work file RELEASE SORT-RECORD ADD 1 TO FILTERED-COUNT ELSE DISPLAY " Filtered zero-amount transaction: " TRANS-CUSTOMER-ID END-IF READ TRANSACTION-FILE AT END MOVE 'Y' TO EOF-FLAG END-READ END-PERFORM CLOSE TRANSACTION-FILE DISPLAY "Input processing complete. Records released: " FILTERED-COUNT. PROCESS-OUTPUT SECTION. DISPLAY "Processing sorted output..." OPEN OUTPUT SORTED-REPORT PERFORM UNTIL NO-MORE-RECORDS RETURN SORT-WORK AT END MOVE 'Y' TO END-FLAG NOT AT END *> Move sorted record to output MOVE SORT-CUSTOMER-ID TO RPT-CUSTOMER-ID MOVE SORT-DATE TO RPT-DATE MOVE SORT-AMOUNT TO RPT-AMOUNT MOVE SORT-TYPE TO RPT-TYPE MOVE SORT-DESCRIPTION TO RPT-DESCRIPTION *> Write to output file WRITE REPORT-RECORD ADD 1 TO OUTPUT-COUNT END-RETURN END-PERFORM CLOSE SORTED-REPORT DISPLAY "Output processing complete. Records written: " OUTPUT-COUNT.
This complete example demonstrates sorting with INPUT PROCEDURE (filters zero-amount transactions) and OUTPUT PROCEDURE (writes sorted records to output file). It sorts by multiple keys and tracks processing statistics.
Follow these best practices for efficient and reliable sorting operations:
1234SORT SORT-WORK ON ASCENDING KEY SORT-KEY USING INPUT-FILE GIVING OUTPUT-FILE
1234SORT SORT-WORK ON ASCENDING KEY SORT-KEY INPUT PROCEDURE IS FILTER-INPUT GIVING OUTPUT-FILE
1234SORT SORT-WORK ON ASCENDING KEY SORT-KEY USING INPUT-FILE OUTPUT PROCEDURE IS PROCESS-OUTPUT
123456SORT SORT-WORK ON ASCENDING KEY PRIMARY-KEY ON ASCENDING KEY SECONDARY-KEY ON DESCENDING KEY TERTIARY-KEY USING INPUT-FILE GIVING OUTPUT-FILE
1234MERGE SORT-WORK ON ASCENDING KEY SORT-KEY USING FILE-A FILE-B FILE-C GIVING MERGED-FILE
Think of sorting like organizing a deck of cards:
So sorting is just a way to organize your data (records) in a specific order, just like organizing cards in a deck!
Complete these exercises to reinforce your understanding:
Create a program that sorts customer records by customer ID in ascending order using SORT with USING and GIVING clauses.
Create a program that sorts transaction records, but only includes transactions with amounts greater than $100. Use INPUT PROCEDURE to filter records before sorting.
Create a program that sorts employee records by department (ascending), then by salary (descending), then by employee ID (ascending).
Create a program that sorts sales records and uses OUTPUT PROCEDURE to calculate totals and write formatted report records.
Create a program that merges three already-sorted customer files (from different regions) into a single sorted file by customer ID.
1. What is a sort work file (SD) used for?
2. What statement is used to write records to a sort work file?
3. What statement is used to read sorted records from a sort work file?
4. What is the difference between SORT and MERGE?
5. How do you sort by multiple keys in COBOL?
6. When would you use INPUT PROCEDURE instead of USING in SORT?
7. When would you use OUTPUT PROCEDURE instead of GIVING in SORT?
8. What must be true about input files when using MERGE?