In this section, we will explore how to sort and merge data in COBOL. Sorting and merging are essential operations when dealing with large datasets, and COBOL provides robust features to handle these tasks efficiently.

Objectives

By the end of this section, you will be able to:

  • Understand the SORT statement in COBOL.
  • Use the MERGE statement to combine multiple sorted files.
  • Implement practical examples of sorting and merging data.
  • Solve exercises to reinforce the learned concepts.

Sorting Data

The SORT Statement

The SORT statement in COBOL is used to arrange records in a specified order. The basic syntax of the SORT statement is as follows:

SORT file-name
    ON ASCENDING/DESCENDING KEY key-name
    USING input-file
    GIVING output-file.
  • file-name: The name of the file to be sorted.
  • ASCENDING/DESCENDING KEY: Specifies the order of sorting.
  • key-name: The field on which the sorting is based.
  • USING input-file: The file containing the unsorted records.
  • GIVING output-file: The file where the sorted records will be stored.

Example: Sorting Records

Let's consider an example where we have an input file containing employee records, and we want to sort these records by employee ID in ascending order.

Input File (EMPLOYEE-IN):

1003 John Doe
1001 Jane Smith
1002 Alice Johnson

COBOL Program:

IDENTIFICATION DIVISION.
PROGRAM-ID. SortExample.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT EMPLOYEE-IN ASSIGN TO 'employee-in.dat'.
    SELECT EMPLOYEE-OUT ASSIGN TO 'employee-out.dat'.

DATA DIVISION.
FILE SECTION.
FD  EMPLOYEE-IN.
01  EMPLOYEE-RECORD-IN.
    05  EMP-ID-IN        PIC 9(4).
    05  EMP-NAME-IN      PIC X(20).

FD  EMPLOYEE-OUT.
01  EMPLOYEE-RECORD-OUT.
    05  EMP-ID-OUT       PIC 9(4).
    05  EMP-NAME-OUT     PIC X(20).

WORKING-STORAGE SECTION.
01  SORT-RECORD.
    05  EMP-ID           PIC 9(4).
    05  EMP-NAME         PIC X(20).

PROCEDURE DIVISION.
    OPEN INPUT EMPLOYEE-IN
    OPEN OUTPUT EMPLOYEE-OUT

    SORT SORT-RECORD
        ON ASCENDING KEY EMP-ID
        USING EMPLOYEE-IN
        GIVING EMPLOYEE-OUT

    CLOSE EMPLOYEE-IN
    CLOSE EMPLOYEE-OUT
    STOP RUN.

Output File (EMPLOYEE-OUT):

1001 Jane Smith
1002 Alice Johnson
1003 John Doe

Explanation

  1. File Declarations: We declare the input and output files in the FILE SECTION.
  2. SORT Statement: The SORT statement sorts the records in EMPLOYEE-IN based on EMP-ID in ascending order and writes the sorted records to EMPLOYEE-OUT.

Merging Data

The MERGE Statement

The MERGE statement in COBOL is used to combine multiple sorted files into a single sorted file. The basic syntax of the MERGE statement is as follows:

MERGE file-name
    ON ASCENDING/DESCENDING KEY key-name
    USING input-file-1 input-file-2 ...
    GIVING output-file.
  • file-name: The name of the file to be merged.
  • ASCENDING/DESCENDING KEY: Specifies the order of merging.
  • key-name: The field on which the merging is based.
  • USING input-file-1 input-file-2 ...: The sorted input files to be merged.
  • GIVING output-file: The file where the merged records will be stored.

Example: Merging Records

Let's consider an example where we have two sorted input files containing employee records, and we want to merge these records into a single sorted file.

Input File 1 (EMPLOYEE-IN1):

1001 Jane Smith
1003 John Doe

Input File 2 (EMPLOYEE-IN2):

1002 Alice Johnson
1004 Bob Brown

COBOL Program:

IDENTIFICATION DIVISION.
PROGRAM-ID. MergeExample.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT EMPLOYEE-IN1 ASSIGN TO 'employee-in1.dat'.
    SELECT EMPLOYEE-IN2 ASSIGN TO 'employee-in2.dat'.
    SELECT EMPLOYEE-OUT ASSIGN TO 'employee-out.dat'.

DATA DIVISION.
FILE SECTION.
FD  EMPLOYEE-IN1.
01  EMPLOYEE-RECORD-IN1.
    05  EMP-ID-IN1       PIC 9(4).
    05  EMP-NAME-IN1     PIC X(20).

FD  EMPLOYEE-IN2.
01  EMPLOYEE-RECORD-IN2.
    05  EMP-ID-IN2       PIC 9(4).
    05  EMP-NAME-IN2     PIC X(20).

FD  EMPLOYEE-OUT.
01  EMPLOYEE-RECORD-OUT.
    05  EMP-ID-OUT       PIC 9(4).
    05  EMP-NAME-OUT     PIC X(20).

WORKING-STORAGE SECTION.
01  MERGE-RECORD.
    05  EMP-ID           PIC 9(4).
    05  EMP-NAME         PIC X(20).

PROCEDURE DIVISION.
    OPEN INPUT EMPLOYEE-IN1 EMPLOYEE-IN2
    OPEN OUTPUT EMPLOYEE-OUT

    MERGE MERGE-RECORD
        ON ASCENDING KEY EMP-ID
        USING EMPLOYEE-IN1 EMPLOYEE-IN2
        GIVING EMPLOYEE-OUT

    CLOSE EMPLOYEE-IN1 EMPLOYEE-IN2
    CLOSE EMPLOYEE-OUT
    STOP RUN.

Output File (EMPLOYEE-OUT):

1001 Jane Smith
1002 Alice Johnson
1003 John Doe
1004 Bob Brown

Explanation

  1. File Declarations: We declare the input and output files in the FILE SECTION.
  2. MERGE Statement: The MERGE statement merges the records from EMPLOYEE-IN1 and EMPLOYEE-IN2 based on EMP-ID in ascending order and writes the merged records to EMPLOYEE-OUT.

Practical Exercises

Exercise 1: Sorting Student Records

Task: Write a COBOL program to sort student records by student ID in descending order.

Input File (STUDENT-IN):

2003 Alice Brown
2001 Charlie Davis
2002 Bob Evans

Expected Output File (STUDENT-OUT):

2003 Alice Brown
2002 Bob Evans
2001 Charlie Davis

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. SortStudentRecords.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT STUDENT-IN ASSIGN TO 'student-in.dat'.
    SELECT STUDENT-OUT ASSIGN TO 'student-out.dat'.

DATA DIVISION.
FILE SECTION.
FD  STUDENT-IN.
01  STUDENT-RECORD-IN.
    05  STUDENT-ID-IN    PIC 9(4).
    05  STUDENT-NAME-IN  PIC X(20).

FD  STUDENT-OUT.
01  STUDENT-RECORD-OUT.
    05  STUDENT-ID-OUT   PIC 9(4).
    05  STUDENT-NAME-OUT PIC X(20).

WORKING-STORAGE SECTION.
01  SORT-RECORD.
    05  STUDENT-ID       PIC 9(4).
    05  STUDENT-NAME     PIC X(20).

PROCEDURE DIVISION.
    OPEN INPUT STUDENT-IN
    OPEN OUTPUT STUDENT-OUT

    SORT SORT-RECORD
        ON DESCENDING KEY STUDENT-ID
        USING STUDENT-IN
        GIVING STUDENT-OUT

    CLOSE STUDENT-IN
    CLOSE STUDENT-OUT
    STOP RUN.

Exercise 2: Merging Product Records

Task: Write a COBOL program to merge two sorted product files into a single sorted file by product ID.

Input File 1 (PRODUCT-IN1):

3001 Laptop
3003 Mouse

Input File 2 (PRODUCT-IN2):

3002 Keyboard
3004 Monitor

Expected Output File (PRODUCT-OUT):

3001 Laptop
3002 Keyboard
3003 Mouse
3004 Monitor

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. MergeProductRecords.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT PRODUCT-IN1 ASSIGN TO 'product-in1.dat'.
    SELECT PRODUCT-IN2 ASSIGN TO 'product-in2.dat'.
    SELECT PRODUCT-OUT ASSIGN TO 'product-out.dat'.

DATA DIVISION.
FILE SECTION.
FD  PRODUCT-IN1.
01  PRODUCT-RECORD-IN1.
    05  PRODUCT-ID-IN1   PIC 9(4).
    05  PRODUCT-NAME-IN1 PIC X(20).

FD  PRODUCT-IN2.
01  PRODUCT-RECORD-IN2.
    05  PRODUCT-ID-IN2   PIC 9(4).
    05  PRODUCT-NAME-IN2 PIC X(20).

FD  PRODUCT-OUT.
01  PRODUCT-RECORD-OUT.
    05  PRODUCT-ID-OUT   PIC 9(4).
    05  PRODUCT-NAME-OUT PIC X(20).

WORKING-STORAGE SECTION.
01  MERGE-RECORD.
    05  PRODUCT-ID       PIC 9(4).
    05  PRODUCT-NAME     PIC X(20).

PROCEDURE DIVISION.
    OPEN INPUT PRODUCT-IN1 PRODUCT-IN2
    OPEN OUTPUT PRODUCT-OUT

    MERGE MERGE-RECORD
        ON ASCENDING KEY PRODUCT-ID
        USING PRODUCT-IN1 PRODUCT-IN2
        GIVING PRODUCT-OUT

    CLOSE PRODUCT-IN1 PRODUCT-IN2
    CLOSE PRODUCT-OUT
    STOP RUN.

Summary

In this section, we covered:

  • The SORT statement to arrange records in a specified order.
  • The MERGE statement to combine multiple sorted files into a single sorted file.
  • Practical examples and exercises to reinforce the concepts of sorting and merging data in COBOL.

By mastering these techniques, you can efficiently manage and manipulate large datasets in your COBOL programs. In the next module, we will delve into subprograms and modular programming techniques to further enhance your COBOL skills.

© Copyright 2024. All rights reserved