A SESSION ON SORT
BY B.THANGASAMY
There are two types of SORT 1. Internal SORT 2. External SORT
Internal SORT can be done by using COBOL language. External SORT can be done by using IBM utility program SORT.
The SORT program is used to sort data into a certain sequence. The sort sequence can be in Ascending or Descending.
DATA DIVISION. FILE SECTION. FD UNSORTED-FILE RECORD CONTAINS 80 CHARACTERS. 01 UNSORTED-REC PIC X(80). FD SORTED-FILE RECORD CONTAINS 80 CHARACTERS. 01 SORTED-REC PIC X(80). SD SORT-FILE RECORD CONTAINS 80 CHARACTERS. 01 SORT-REC. 05 S-DEPT-NO PIC X(2). 05 FILLER PIC X(78). PROCEDURE DIVISION. SORT SORT-FILE ON ASCENDING KEY S-DEPT-NO USING UNSORTED-FILE GIVING SORTED-FILE.
Input Procedure:The data will be processed before It is sorted. Output Procedure:The data will be processed after It is sorted.
100-MAIN MODULE. SORT SORT-FILE ON ASCENDING KEY DEPT-NO INPUT PROCEDURE 200-COUNT-RTN GIVING SORTED-FILE STOP RUN. 200-COUNT-RTN. MOVE 0 TO CTR OPEN INPUT UNSORTED-FILE PERFORM UNTIL NO-MORE-RECS READ UNSORTED-FILE AT END MOVE ‘NO’ TO MORE-RECS NOT AT END PERFORM 300-PROCESS-RTN END-READ END-PERFORM CLOSE UNSORTED-FILE. 300-PROCESS-RTN. ADD 1 TO CTR RELEASE SORT-REC FROM UNSORTED-REC.
100-MAIN-MODULE. SORT SORT-FILE ON ASCENDING KEY DEPT-NO USING UNSORTED-FILE OUTPUT PROCEDURE 200-COUNT-RTN STOP RUN. 200-COUNT-RTN. MOVE 0 TO CTR OPEN OUTPUT SORTED-FILE PERFORM UNTIL NO-MORE-RECS RETURN SORT-FILE AT END MOVE ‘NO’ TO MORE-RECS NOT AT END PERFORM 300-PROCESS-RTN END-RETURN END-PERFORM CLOSE SORTED-FILE. 300-PROCESS-RTN. ADD 1 TO CTR WRITE SORTED-REC FROM SORT-REC.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=(52,9,CH,A) /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // SORTIN INPUT FILE SORTOUT OUTPUT FILE SYSIN DD * CONTROL STATEMENTS 52 Starting position in the input file 9 sort key length CH The type of data stored in the file A Ascending Order
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=(52,9,CH,A),EQUALS /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // EQUALS keyword will preserve the order of Records with identical sort fields. NOEQUALS is the default, and causes equal-keyed Records to be written in a random order.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY,SKIPREC=1000 /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The keyword SKIPREC causes SORT to skip over 1000 Records in the input file before starting a sorting or Copying operation.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY,STOPAFT=1000 /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The keyword STOPAFT causes SORT to stop after 1000 Records in the input file have been sorted or copied.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=(52,9,CH,A) SUM FIELDS=NONE /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The keyword SUM FIELDS=NONE is used to eliminate The duplicate records from the input file.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY INCLUDE COND=(52,9,CH,EQ,C’123456789’) /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The INCLUDE control statement is used to establish Selection criteria for the records to be included in the Output dataset.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY OMIT COND=(52,9,CH,EQ,C’123456789’) /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The OMIT control statement is used to establish Selection criteria for the records to be omitted From the output dataset.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY OUTREC FIELDS=(1:1,113, 114:X'04B5', 116:116,1061, 1177:X'0000000000000C', 1184:X'0000000000000C', 1191:15Z, 1206:X'000C', 1208:2893X) /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The OUTREC control statement reformats the output file Records after SORT operation.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * INREC FIELDS=(1:1,113, 114:X'04B5', 116:116,1061, 1177:X'0000000000000C', 1184:X'0000000000000C', 1191:15Z, 1206:X'000C', 1208:2893X) SORT FIELDS=(52,9,CH,A) /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The INREC control statement reformats the input file Records before SORT operation.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY OUTREC FIELDS=(1:5,4096),CONVERT /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SYSOUT DD SYSOUT=* // The CONVERT control statement is used to convert the File format from VB to FB.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=COPY OUTFIL FILES=HE, INCLUDE=(5,4,CH,EQ,C'9991') OUTFIL FILES=TR, OMIT=(5,4,CH,EQ,C'9992') /* //SORTIN DD DSN=…,DISP=SHR //SORTOFHE DD DSN=… //SORTOFTR DD DSN=… //SYSOUT DD SYSOUT=* // The OUTFIL control statement is used to create Multiple output files.
//J344087M JOB (B,J,67FT00,C),’THANGASAMY’, // NOTIFY=&SYSUID //SORTTA EXEC PGM=SORT //SYSIN DD * SORT FIELDS=(29,9,CH,A) SUM FIELDS=NONE,XSUM /* //SORTIN DD DSN=…,DISP=SHR //SORTOUT DD DSN=… //SORTXSUM DD DSN=… //SYSOUT DD SYSOUT=* // The XSUM control statement is used to create The output file with the duplicate records alone.
THANK YOU