Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

Module 12: SORT AND MERGE


SORT

  • In many business applications sometimes there is a need to sort files. In COBOL, using SORT verb, sequential files can be sorted in ascending/descending order of the provided key
  • Sorting done through COBOL program is referred to as “internal sort”
  • Sorting done through JCL referred to as “external sort”
  • “Internal sort”, in turn invokes the SORT product of your system (Example. DFSORT).
  • While performing SORT in COBOL program, input file is not changed; only output file filled with the records in sorted order
  • In sort process three files are used:-
    • Input-file: File that contains unsorted records. The FD entry for input file should be defined in FILE SECTION
    • Work-file: It is temporary file, which will be used during the sorting process. The SD (Sort Description) entry for file should be defined in FILE SECTION.
    • Output-file: File on which sorted records will be written
  • Basic syntax:-

    SORT work-file ON {ASCENDING/DESCENDING}      KEY sd-rec-key-1 [,sd-rec-key-2]… USING input-file GIVING output-file.

  • Note :- For work-file, SD entry should be defined in FILE SECTION
  • Format of SD entry:-

    SD work-file    [RECORD CONTIANS integer CHARACTERS]    [DATA RECORD IS file-rec-1]

  • Important consideration about SORT:-
    • It accepts input from a file that is not in sequence, and produces a output file in a requested sequence
    • Each sd-rec-keys (shown in SORT syntax) identifies a field in the record of work-file, upon which the file will be sorted
    • When more than one sd-file-key is specified, the keys decrease in significance from left to right (leftmost key is most major key i.e. most significant rightmost is least significant)
    • All three files are opened by SORT statement itself and closes them on sort completion. File should not be kept open before SORT begins.
    • It is possible to use both the words ASCENDING and DESCENDING in single SORT statement but for different keys. If all keys to be sorted in same order, then ASCENDING or DESCENDING needs to be mentioned only once before the most major key
    • Input and output file must be PS (physical sequential) files
    • The order of appearance of keys in SORT statement has no relevance in their position in the record.
  • Visual representation of SORT:-

Example depciting use of SORT

Need to sort EMP-FILE based on employee id(ascending order).

000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. SORTPGM. 000300 ENVIRONMENT DIVISION. 000400 INPUT-OUTPU SECTION. 000500 FILE-CONTROL. 000600 SELECT EMP-INP ASSIGN TO EMPIN. 000700 SELECT EMP-OUT ASSIGN TO EMPOUT. 000800 SELECT WORK-FILE ASSIGN TO WRKDD. 000900 DATA DIVISION. 001000 FILE-SECTION. 001100 FD EMP-IN. 001200 01 EMP-INPUT. 001300 05 EMP-ID PIC 9(05). 001400 05 EMP-NAME PIC X(10). 001500 FD EMP-OUT. 001600 01 EMP-OUTPUT. 001700 05 EMP-ID-OP PIC 9(05). 001800 05 EMP-NAME-OP PIC X(10). 001900 SD WRK-FILE. 002000 01 EMP-WORK. 002100 05 EMP-ID-WK PIC 9(05). 002200 05 EMP-NAME-WK PIC X(10). 002200 PROCEDURE DIVISION. 002300 MAIN-PARA. 002400 SORT EMP-WORK. 002500 ON ASCENDING KEY EMP-ID-WK 002600 USING EMP-INP 002700 GIVINS EMP-OUT. 002800 STOP RUN.

JCL Step to execute above COBOL

//STEP001 EXEC PGM=SORTPGM //EMPIN DD DSN=USER.DATA.EMPIN,DISP=SHR //EMPOUT DD DSN=USER.DATA.OUT, // DISP=(NEW,CATLG,DELETE), // UNIT=TEST,SPACE=(CYL,(25,5),RLSE), // DCB=(LRECL=15,BLKSIZE=1500,RECFM=FB) //WRKDD DD DSN=USER.DATA.TEMP, // DISP=(,DELETE,DELETE), // UNIT=WORK,SPACE=(CYL,(25,5),RLSE), // DCB=(LRECL=15,BLKSIZE=1500,RECFM=FB)






© copyright mainframebug.com
Privacy Policy