MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Sorting Operations

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.

What is Sorting in COBOL?

Sorting in COBOL is the process of arranging records in ascending or descending order based on specified key fields. Sorting is used to:

  • Organize data: Arrange records in a logical order for processing or display
  • Prepare reports: Sort data before generating reports to present information in a meaningful order
  • Optimize access: Sort data to enable efficient searching and processing
  • Combine data: Merge multiple sorted files into a single sorted file
  • Group related records: Sort by key fields to group related records together

COBOL provides two main statements for sorting:

  • SORT: Arranges unsorted records into sorted order
  • MERGE: Combines two or more already-sorted files into a single sorted file

Sort Work File (SD)

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.

Defining a Sort Work File

cobol
1
2
3
4
5
6
FILE 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:

  • Defined with SD: Use SD (Sort Description) instead of FD (File Description)
  • Same structure: Must have the same record structure as records being sorted
  • Managed by SORT: You don't open, close, read, or write to it directly
  • Temporary storage: Used internally by SORT/MERGE for temporary storage during sorting
  • RELEASE/RETURN: Use RELEASE to write to it, RETURN to read from it

SORT Statement

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.

Basic SORT Syntax

cobol
1
2
3
4
5
6
SORT 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:

  • sort-work-file: The sort work file (SD) used for temporary storage
  • ON ASCENDING/DESCENDING KEY: Specifies sort keys and order (ascending = A-Z, 0-9; descending = Z-A, 9-0)
  • USING input-file: The input file containing unsorted records
  • GIVING output-file: The output file that will contain sorted records

Simple SORT Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
IDENTIFICATION 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.

Multi-Key Sorting

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.

Multi-Key SORT Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
PROCEDURE 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

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.

INPUT PROCEDURE Syntax

cobol
1
2
3
4
SORT sort-work-file ON ASCENDING KEY key-field INPUT PROCEDURE IS procedure-name GIVING output-file

INPUT PROCEDURE Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
PROCEDURE 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

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.).

OUTPUT PROCEDURE Syntax

cobol
1
2
3
4
SORT sort-work-file ON ASCENDING KEY key-field USING input-file OUTPUT PROCEDURE IS procedure-name

OUTPUT PROCEDURE Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
PROCEDURE 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 Statements

RELEASE and RETURN are special statements used with sort work files:

  • RELEASE: Writes a record to the sort work file during an INPUT PROCEDURE. Syntax: RELEASE sort-work-record
  • RETURN: Reads the next sorted record from the sort work file during an OUTPUT PROCEDURE. Syntax: RETURN sort-work-file AT END ... NOT AT END ... END-RETURN

RELEASE Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PROCESS-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.

RETURN Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PROCESS-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.

MERGE Statement

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.

MERGE Syntax

cobol
1
2
3
4
MERGE 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.

MERGE Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
IDENTIFICATION 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.

Complete Sorting Example

Here's a complete example demonstrating sorting with input and output procedures:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
IDENTIFICATION 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.

Best Practices for Sorting Operations

Follow these best practices for efficient and reliable sorting operations:

  • Define sort work file correctly: Use SD (not FD), ensure record structure matches records being sorted
  • Use INPUT PROCEDURE for filtering: Filter invalid records before sorting to improve performance
  • Use OUTPUT PROCEDURE for custom processing: Process sorted records as needed (calculations, formatting, etc.)
  • Sort by most selective key first: In multi-key sorts, use the most selective (most unique values) key first
  • Ensure files are sorted for MERGE: All input files for MERGE must be sorted by the same key(s)
  • Handle AT END conditions: Always handle AT END in RETURN statements
  • Close all files: Close input and output files properly in procedures
  • Use meaningful key names: Name sort key fields clearly to indicate their purpose
  • Test with various data volumes: Test sorting with small and large datasets
  • Consider performance: Large sorts may require significant resources; plan accordingly
  • Validate input data: Ensure data is in expected format before sorting
  • Document sort logic: Comment complex sort operations to explain business logic

Common Sorting Patterns

Pattern 1: Simple Sort

cobol
1
2
3
4
SORT SORT-WORK ON ASCENDING KEY SORT-KEY USING INPUT-FILE GIVING OUTPUT-FILE

Pattern 2: Sort with Filtering

cobol
1
2
3
4
SORT SORT-WORK ON ASCENDING KEY SORT-KEY INPUT PROCEDURE IS FILTER-INPUT GIVING OUTPUT-FILE

Pattern 3: Sort with Custom Output

cobol
1
2
3
4
SORT SORT-WORK ON ASCENDING KEY SORT-KEY USING INPUT-FILE OUTPUT PROCEDURE IS PROCESS-OUTPUT

Pattern 4: Multi-Key Sort

cobol
1
2
3
4
5
6
SORT SORT-WORK ON ASCENDING KEY PRIMARY-KEY ON ASCENDING KEY SECONDARY-KEY ON DESCENDING KEY TERTIARY-KEY USING INPUT-FILE GIVING OUTPUT-FILE

Pattern 5: Merge Sorted Files

cobol
1
2
3
4
MERGE SORT-WORK ON ASCENDING KEY SORT-KEY USING FILE-A FILE-B FILE-C GIVING MERGED-FILE

Explain Like I'm 5: Sorting Operations

Think of sorting like organizing a deck of cards:

  • SORT is like shuffling a messy deck and organizing it by number (Ace, 2, 3, 4...)
  • MERGE is like taking two already-organized decks and combining them into one organized deck
  • Sort work file is like a table where you temporarily put cards while organizing them
  • RELEASE is like putting a card on the sorting table
  • RETURN is like picking up the next card from the sorted pile
  • Multi-key sort is like organizing by suit first, then by number within each suit

So sorting is just a way to organize your data (records) in a specific order, just like organizing cards in a deck!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Simple Sort

Create a program that sorts customer records by customer ID in ascending order using SORT with USING and GIVING clauses.

Exercise 2: Sort with Filtering

Create a program that sorts transaction records, but only includes transactions with amounts greater than $100. Use INPUT PROCEDURE to filter records before sorting.

Exercise 3: Multi-Key Sort

Create a program that sorts employee records by department (ascending), then by salary (descending), then by employee ID (ascending).

Exercise 4: Sort with Output Processing

Create a program that sorts sales records and uses OUTPUT PROCEDURE to calculate totals and write formatted report records.

Exercise 5: Merge Operation

Create a program that merges three already-sorted customer files (from different regions) into a single sorted file by customer ID.

Test Your Knowledge

1. What is a sort work file (SD) used for?

  • To store sorted output permanently
  • To temporarily store records during the sorting process
  • To read input records
  • To write output records

2. What statement is used to write records to a sort work file?

  • WRITE
  • RELEASE
  • RETURN
  • READ

3. What statement is used to read sorted records from a sort work file?

  • READ
  • WRITE
  • RETURN
  • RELEASE

4. What is the difference between SORT and MERGE?

  • There is no difference
  • SORT arranges unsorted records, MERGE combines already-sorted files
  • SORT is faster than MERGE
  • MERGE requires more memory

5. How do you sort by multiple keys in COBOL?

  • Use multiple SORT statements
  • Specify multiple ON ASCENDING/DESCENDING KEY clauses
  • Use nested SORT statements
  • You cannot sort by multiple keys

6. When would you use INPUT PROCEDURE instead of USING in SORT?

  • When you want to filter or transform records before sorting
  • When input is already sorted
  • When you have multiple input files
  • INPUT PROCEDURE is never used

7. When would you use OUTPUT PROCEDURE instead of GIVING in SORT?

  • When you want to process sorted records before writing output
  • When output is already sorted
  • When you have multiple output files
  • OUTPUT PROCEDURE is never used

8. What must be true about input files when using MERGE?

  • They must be unsorted
  • They must already be sorted by the same key(s) as specified in MERGE
  • They must have different record structures
  • They must be empty

Related Concepts

Related Pages