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

COBOL - Sort Output File


If the output from sorting or merging is a file, describe the file by following the procedure below.

Code as follows:

  • Write a SELECT clause in the FILE-CONTROL paragraph of the ENVIRONMENT DIVISION to name the output file. For example:

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT Output-File ASSIGN TO OutFile.
    

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

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

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


Example: Let us see example below,

IDENTIFICATION DIVISION.
DATA DIVISION.
FILE SECTION.
SD  OUR-FILE.
01  OUR-SORT-REC.
    03  SORT-KEY                PIC X(10).
    03  FILLER                  PIC X(70).
. . .
WORKING-STORAGE SECTION.
01  WS-SORT-REC                 PIC X(80).
01  END-OF-SORT-FILE-INDICATOR  PIC X VALUE 'N'.
    88  NO-MORE-SORT-RECORDS          VALUE 'Y'.
. . .
PROCEDURE DIVISION.
A-CONTROL SECTION.
    SORT OUR-FILE ON ASCENDING KEY SORT-KEY
      INPUT PROCEDURE IS B-INPUT
      OUTPUT PROCEDURE IS C-OUTPUT.
    . . .
B-INPUT SECTION.
    MOVE . . .. . .. TO WS-SORT-REC.
    RELEASE OUR-SORT-REC FROM WS-SORT-REC.
    . . .
C-OUTPUT SECTION.
    DISPLAY 'STARTING READS OF SORTED RECORDS: '.
    RETURN OUR-FILE
      AT END
        SET NO-MORE-SORT-RECORDS TO TRUE.
    PERFORM WITH TEST BEFORE UNTIL NO-MORE-SORT-RECORDS
      IF SORT-RETURN = 0 THEN
        DISPLAY 'OUR-SORT-REC = ' OUR-SORT-REC
        RETURN OUR-FILE
          AT END
            SET NO-MORE-SORT-RECORDS TO TRUE
      END-IF
    END-PERFORM.


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