Showing posts with label SORT USING. Show all posts
Showing posts with label SORT USING. Show all posts

Monday, 28 July 2014

COBOL SORT Procedure: USING, GIVING, INPUT, and OUTPUT Examples

A COBOL program can sort records inside the program with the SORT statement. That is useful when the program must select records before sorting or handle sorted records one at a time after sorting. For a simple production file sort, a separate JCL DFSORT step is often easier to tune and support, but internal COBOL SORT still has a place.

COBOL SORT procedure diagram showing input file, sort work file, output file, RELEASE, and RETURN
Sort, then process records.

What does COBOL SORT do?

The COBOL SORT statement arranges records or table elements in a sequence defined by one or more keys. For file sorting, COBOL uses an SD sort-file description, receives records from an input file or input procedure, sorts them, and then writes them to an output file or passes them to an output procedure.

Simple SORT USING GIVING example

The simplest form uses USING for the input file and GIVING for the output file. COBOL opens, reads, writes, and closes those files as part of the SORT. IBM notes that the input and output files named in these phrases must not already be open when the SORT executes.

SELECT INPUT-FILE  ASSIGN TO INFILE.
SELECT SORT-FILE   ASSIGN TO SORTWK.
SELECT OUTPUT-FILE ASSIGN TO OUTFILE.

SD  SORT-FILE.
01  SORT-REC.
    05 SORT-ACCOUNT-NO     PIC X(10).
    05 SORT-DATE           PIC X(8).
    05 SORT-AMOUNT         PIC S9(9)V99 COMP-3.

PROCEDURE DIVISION.
    SORT SORT-FILE
       ON ASCENDING KEY SORT-ACCOUNT-NO
       USING INPUT-FILE
       GIVING OUTPUT-FILE
    GOBACK.

This form fits a direct file-to-file sort where every input record should appear in the sorted output and no custom record logic is needed inside the COBOL program.

When to use INPUT PROCEDURE

Use INPUT PROCEDURE when records must be selected, edited, or built before the sort begins. The input procedure reads or creates records and sends each accepted record to the sort file with RELEASE.

SORT SORT-FILE
   ON ASCENDING KEY SORT-ACCOUNT-NO
   INPUT PROCEDURE 2000-BUILD-SORT-RECS
   GIVING OUTPUT-FILE.

2000-BUILD-SORT-RECS.
    PERFORM UNTIL END-OF-FILE
       READ INPUT-FILE
          AT END
             SET END-OF-FILE TO TRUE
          NOT AT END
             IF IN-STATUS = 'A'
                MOVE INPUT-REC TO SORT-REC
                RELEASE SORT-REC
             END-IF
       END-READ
    END-PERFORM.

Use this pattern when the program must drop inactive records, change field layouts, or combine working-storage values before the sort sees the record.

When to use OUTPUT PROCEDURE

Use OUTPUT PROCEDURE when sorted records must be processed before they are written. The output procedure gets each sorted record with RETURN. IBM recommends coding RETURN ... AT END and running until the end condition is reached.

SORT SORT-FILE
   ON ASCENDING KEY SORT-ACCOUNT-NO
   USING INPUT-FILE
   OUTPUT PROCEDURE 3000-WRITE-REPORT.

3000-WRITE-REPORT.
    PERFORM UNTIL NO-MORE-SORT-RECS
       RETURN SORT-FILE
          AT END
             SET NO-MORE-SORT-RECS TO TRUE
          NOT AT END
             MOVE SORT-REC TO REPORT-REC
             WRITE REPORT-REC
       END-RETURN
    END-PERFORM.

This form fits sorted reports, grouped totals, break processing, and cases where the program must inspect the sorted stream before writing the final output.

RELEASE and RETURN in plain English

Statement Where it is used Purpose
RELEASE Input procedure Sends one record to the sort file before sorting starts.
RETURN Output procedure Gets one sorted record from the sort file after sorting finishes.
AT END RETURN statement Detects that no more sorted records are available.

COBOL SORT vs JCL SORT

Use internal COBOL SORT when selection, transformation, or report logic belongs naturally in the program. Use JCL SORT when the requirement is only to sort, copy, include, omit, reformat, or split files. A separate DFSORT step is usually easier for operations teams to tune because sort memory, work files, and control statements are visible in JCL.

Common mistakes

Opening files before SORT USING or GIVING

Do not open the files named in USING or GIVING before the SORT. The SORT statement handles those files for that form.

Forgetting RELEASE in the input procedure

An input procedure must release records to the sort file. If no records are released, the output procedure or output file will not receive the expected data.

Forgetting RETURN AT END

An output procedure should keep returning sorted records until the AT END condition is reached. With DFSORT, failing to reach the end condition can lead to an abnormal sort termination.

Using internal SORT for every large file

Large high-volume sorts should be reviewed carefully. If the sort can be done outside the program, a JCL SORT step may be simpler to monitor and change.

Review checklist

  • Define the sort file with an SD entry.
  • Choose one input path: USING or INPUT PROCEDURE.
  • Choose one output path: GIVING or OUTPUT PROCEDURE.
  • Use RELEASE for records passed into an input procedure.
  • Use RETURN ... AT END for records pulled from an output procedure.
  • Compare internal COBOL SORT with a separate DFSORT step for high-volume batch work.

Related Mainframe Forum guides

For related topics, read JCL SORT examples, SORT INREC and OUTREC examples, COBOL sequential file organization, COBOL READ statement, and COBOL PERFORM statement.

External references

IBM documents the Enterprise COBOL SORT statement, coding the output procedure, and coding the input procedure.

FAQ

What is COBOL SORT used for?

COBOL SORT arranges records or table elements by one or more keys. For files, it can take input from files or an input procedure and send sorted records to files or an output procedure.

What is INPUT PROCEDURE in COBOL SORT?

INPUT PROCEDURE runs before sorting. It selects or builds records and passes them to the sort file with RELEASE.

What is OUTPUT PROCEDURE in COBOL SORT?

OUTPUT PROCEDURE runs after sorting. It gets each sorted record with RETURN and can write, total, or report the record.

Should I use COBOL SORT or JCL SORT?

Use COBOL SORT when program logic must run before or after sorting. Use JCL SORT when the task is mainly file sorting, copying, filtering, or formatting.

Sunday, 11 August 2013

COBOL FASTSRT Compiler Option: When DFSORT Handles Sort I/O

A COBOL program that sorts a million records with SORT ... USING and SORT ... GIVING can spend extra CPU passing control back to COBOL for file input and output. The FASTSRT compiler option changes that path for eligible sorts: DFSORT, or a comparable sort product, performs the input and output instead of Enterprise COBOL.

COBOL FASTSRT compiler option diagram showing COBOL I/O, FASTSRT eligibility, and DFSORT I/O
Let DFSORT handle eligible I/O.

What is the COBOL FASTSRT compiler option?

FASTSRT controls whether the sort product performs input and output for eligible COBOL SORT and MERGE operations. IBM documents the default as NOFASTSRT. With FASTSRT, the sort product can handle files named in USING or GIVING, which avoids returning to COBOL after each record is read or written.

FASTSRT vs NOFASTSRT

Option Who handles sort file I/O? Best fit
NOFASTSRT Enterprise COBOL Sorts that need COBOL file error semantics, file status behavior, or unsupported file handling.
FASTSRT DFSORT or comparable sort product Eligible direct file sorts using USING and/or GIVING, especially high-volume batch sorts.

Simple eligible SORT example

This direct sort is the kind of pattern where FASTSRT can help. The program gives COBOL an input file and an output file, and no custom input or output procedure is needed.

CBL FASTSRT

SELECT INPUT-FILE  ASSIGN TO INFILE.
SELECT SORT-FILE   ASSIGN TO SORTWK.
SELECT OUTPUT-FILE ASSIGN TO OUTFILE.

SD  SORT-FILE.
01  SORT-REC.
    05 SORT-ACCOUNT-NO     PIC X(10).
    05 SORT-DATE           PIC X(8).
    05 SORT-AMOUNT         PIC S9(9)V99 COMP-3.

PROCEDURE DIVISION.
    SORT SORT-FILE
       ON ASCENDING KEY SORT-ACCOUNT-NO
       USING INPUT-FILE
       GIVING OUTPUT-FILE
    GOBACK.

For this shape, the sort product can perform the file I/O when the full list of FASTSRT requirements is met.

When FASTSRT does not help much

FASTSRT does not remove the COBOL logic in an INPUT PROCEDURE or OUTPUT PROCEDURE. If the program must inspect each record, run business rules, and call subprograms before releasing records to the sort, most of the cost may still be in COBOL logic.

SORT SORT-FILE
   ON ASCENDING KEY SORT-ACCOUNT-NO
   INPUT PROCEDURE 2000-BUILD-SORT-RECS
   OUTPUT PROCEDURE 3000-WRITE-REPORT.

Use FASTSRT as a file-I/O improvement for eligible paths, not as a cure for expensive record-level business processing.

Important restrictions

SORTIN and SORTOUT DFSORT options

IBM notes that you cannot use DFSORT SORTIN or SORTOUT options when using FASTSRT. The COBOL USING and GIVING files define the input and output for the statement.

Line-sequential files

FASTSRT does not apply to line-sequential files used as USING or GIVING files. If the program sorts text-style line-sequential data, do not expect this option to change that path.

FILE STATUS behavior

IBM documents that if file status is specified and FASTSRT is used, file status is ignored during the sort. Keep NOFASTSRT when COBOL file error semantics must be preserved for the sort processing.

DCB and sort work files

The compiler can check many FASTSRT eligibility rules, but IBM notes two checks that are not fully verified at compile time: whether sort work files use a device other than direct-access storage, and whether input or output file DCB parameters match the FD. Check the JCL and file definitions before promotion.

How to decide whether to use FASTSRT

  • Use it first on simple SORT ... USING ... GIVING programs.
  • Check whether COBOL file status handling is needed during the sort.
  • Confirm the sort work data sets and DCB information match site standards.
  • Compare CPU time, EXCP counts, elapsed time, and DFSORT messages before and after the change.
  • Keep the compile option visible in the build procedure or compiler listing.

Performance expectation

IBM performance guidance recommends FASTSRT for eligible sorts when COBOL file error handling is not needed. IBM also gives an example where one program processing 100,000 records was faster and used fewer EXCPs with FASTSRT. Treat that as an example, not a fixed promise. Your result depends on record size, sort keys, I/O path, work files, and how much logic remains in COBOL.

Common mistakes

Turning on FASTSRT without checking file status usage

If the program depends on file status during sort I/O, FASTSRT can change the behavior that support teams expect. Review the FD and error handling before changing the compiler option.

Expecting FASTSRT to speed up business logic

FASTSRT targets sort file I/O. It does not make an expensive input procedure cheap if that procedure runs many calls, table scans, or database reads.

Leaving the change undocumented

Record the compiler option change in the build procedure or change record. A future compiler migration is easier when the reason for FASTSRT is visible.

Related Mainframe Forum guides

For related topics, read COBOL SORT procedure, JCL SORT examples, SORT INREC and OUTREC examples, COBOL OPTIMIZE compiler option, and COBOL performance tuning.

External references

IBM documents the Enterprise COBOL FASTSRT compiler option, improving sort performance with FASTSRT, and COBOL 6 performance guidance for FASTSRT.

FAQ

What does FASTSRT do in COBOL?

FASTSRT lets DFSORT or a comparable sort product perform I/O for eligible COBOL sort and merge operations instead of Enterprise COBOL doing that I/O.

What is the default FASTSRT setting?

IBM documents the default as NOFASTSRT, meaning Enterprise COBOL performs the input and output for the sort or merge.

Does FASTSRT work with INPUT PROCEDURE and OUTPUT PROCEDURE?

FASTSRT does not remove the COBOL logic inside input or output procedures. It applies to eligible USING and GIVING file I/O portions.

When should I avoid FASTSRT?

Avoid it when the program needs COBOL file status behavior during sort processing, uses unsupported file types, or cannot meet the FASTSRT requirements.

New In-feed ads