TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Sort Input File


Describe the input file or files for sorting or merging by following the procedure below.

Code as follows:
  • Write one or more SELECT clauses in the FILE-CONTROL paragraph of the ENVIRONMENT DIVISION to name the input files. For example:

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT Input-File ASSIGN TO InFile.
    

    Input-File is the name of the file in your program. Use this name to refer to the file.

  • Describe the input file (or files when merging) in an FD entry in the FILE SECTION of the DATA DIVISION. For example:

    DATA DIVISION.
    FILE SECTION.
    FD  Input-File
        LABEL RECORDS ARE STANDARD
        BLOCK CONTAINS 0 CHARACTERS
        RECORDING MODE IS F
        RECORD CONTAINS 100 CHARACTERS.
    01  Input-Record   PIC X(100).
    

Example: Let see an example oof COBOL code below,

ID Division.
Program-ID. SmplSort.
Environment Division.
Input-Output Section.
File-Control.
*
* Assign name for a working file is treated as documentation.
*
    Select Sort-Work-1 Assign To SortFile.
    Select Sort-Work-2 Assign To SortFile.
    Select Input-File  Assign To InFile.
. . .
Data Division.
File Section.
SD  Sort-Work-1
    Record Contains 100 Characters.
01  Sort-Work-1-Area.
    05  Sort-Key-1    Pic  X(10).
    05  Sort-Key-2    Pic  X(10).
    05  Filler        Pic  X(80).

SD  Sort-Work-2
    Record Contains 30 Characters.
01  Sort-Work-2-Area.
    05  Sort-Key      Pic  X(5).
    05  Filler        Pic  X(25).

FD  Input-File
    Label Records Are Standard
    Block Contains 0 Characters
    Recording Mode is F
    Record Contains 100 Characters.
01  Input-Record      Pic  X(100).
. . .
Working-Storage Section.
01  EOS-Sw            Pic  X.
01  Filler.
    05  Table-Entry Occurs 100 Times
            Indexed By X1    Pic X(30).
    . . .


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal