Use SORT ... GIVING if you want DFSORT to transfer the sorted records directly from the sort work file into another file without any further processing. With SORT . . . GIVING file-name, the compiler generates an output procedure to open the file, return the records, write the records, and close the file. At the time the SORT statement is executed, the file named with the GIVING option must not be open. If, however, you want to select, edit, or otherwise modify the sorted records before writing them from the sort work file into another file, use the OUTPUT PROCEDURE option of the SORT statement. In the output procedure, you must use the RETURN statement to make each sorted record available to your program (the RETURN statement for a sort file is similar to a READ statement for an input file). Your output procedure can then contain any statements necessary to process the records that are made available, one at a time, by the RETURN statement.

You can use RETURN INTO, instead of RETURN, to return and process the records into Working Storage or to an output area. You can also use the AT END and END-RETURN phrases with the RETURN statement. The imperative statements on the AT END phrase will execute after all the records, have been returned from the sort file. The END-RETURN explicit scope terminator serves to delimit the scope of the RETURN statement.

When you code output procedures, remember that each output procedure must include at least one RETURN or RETURN INTO statement. Also, each output procedure must be contained in either a section or a paragraph.

Restrictions on Input/Output Procedures

The following restrictions apply to the procedural statements within input and output procedures:

  • The input/output procedure must not contain any SORT or MERGE statements.
  • The input/output procedure must not contain any STOP RUN, EXIT PROGRAM, or GOBACK statements.
  • The execution of a CALL statement to another program that follows standard linkage conventions is permitted; the called program cannot execute a SORT or MERGE statement.
  • You can use ALTER, GO TO, and PERFORM statements in the input/output procedure to refer to procedure-names outside the input/output procedure. However, you must return to the input/output procedure after a GO TO or PERFORM statement.
  • The remainder of the Procedure Division must not contain any transfers of control to points inside the input/output procedure (with the exception of the return of control from a Declarative Section).
  • During a SORT or MERGE operation, the SD data item is used. You must not use it in the OUTPUT PROCEDURE before the first RETURN statement executes. If data is moved into this record area before the first RETURN statement, the first record to be returned will be overwritten.

Determining Whether the Sort or Merge was Successful

The DFSORT program returns a completion code after execution of a sort. The codes are:

    • 0 - Successful completion of sort/merge
    • 16 - Unsuccessful completion of sort/merge

The return code or completion code is stored in a SORT-RETURN special register. The contents of SORT-RETURN change with the execution of each SORT or MERGE statement. You should test for successful completion after each SORT or MERGE statement. For example,

Determining Whether the Sort or Merge was Successful

Premature Termination of a Sort or Merge Operation

The SORT-RETURN special register can also be used to terminate the DFSORT product operation. Move the integer 16 into the register in input or output procedure or in a declarative section entered during sort or merge processing. In an input or output procedure, sort or merge processing will be terminated immediately after the execution of the next RELEASE or RETURN statement. In a declarative section entered during processing of a USING or GIVING file, sort or merge processing will be terminated immediately after the execution of the next implicit RELEASE or RETURN, which will occur after a record has been read from or written to the USING or GIVING file. Control then returns to the statement following the SORT or MERGE statement.

If you do not reference SORT-RETURN anywhere in your program, COBOL will test the return code and, if the code is 16, issue a run-time diagnostic message. If you test SORT-RETURN for one or more (but not necessarily all) SORT or MERGE statements, COBOL will not check the return code.

By default, DFSORT diagnostic messages are sent to the SYSOUT data set. If you want to change this default, you can use the MSGDDN = parameter of the DFSORT OPTION control card or the SORT-MESSAGE special register.

If both the COBOL program (via DISPLAY or example) and DFSORT write to SYSOUT, uncertain printing results will occur. You can do one of the following:

  • Change the DFSORT diagnostic message data set via MSGDDN= or the SORT-MESSAGE special register.
  • Redirect the COBOL program display output data set via the OUTDD compiler option.

Performing More Than One Operation in a Program

You can perform more than one sort or merge in your COBOL program, including:

    • Multiple executions of the same sort or merge
    • Multiple sorts and/or merges

However, one operation must be completed before another can begin.

Preserving the Original Sequence of Records with Equal Keys

The order of identical collating records can be preserved from input to output in one of three ways:

  • Install DFSORT with the EQUALS option as the default.
  • Provide, at run time, an OPTION card in the ICZSRTCD data set with the EQUALS keyword.
  • Use the WITH DUPLICATES IN ORDER phrase in the SORT statement; this adds the EQUALS keyword to the OPTION card of the ICZSRTCD data set. Note that if the NOEQUALS keyword is used on the OPTION card and the DUPLICATES phrase is specified, an error condition exists, and the run unit will be terminated.

Run-Time JCL for Sort

You need DD statements in the run-time JCL to describe the following data sets:

  • Sort work data sets (where nn = 99 or less). Define a minimum of three sort work data sets. SORTWK01, SORTWK02, SORTWK.03 SORTWK nn
  • SYSOUT - For sort diagnostic messages, unless the name is changed via the MSGDDN = keyword of the OPTION control statement in the SORT-CONTROL data set or in the SORT-MESSAGE special register.
  • SORTCKPT - Needed if sort will take checkpoints.
  • Input and output data sets, if any.
  • SORTLIB (DFSORT Library) - To define the library containing the sort modules, for example, SYS1.SORTLIB.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

IBM Mainframe Topics