COBOL Manual for TNS/E Programs (H06.08+, J06.03+)
If infile specifies a file whose SELECT clause does not include the OPTIONAL phrase, the
file must be present at execution time.
If an instance of outfile identifies an indexed or queue file, the major key must be associated
with the ASCENDING phrase and the first instance of key must specify the same character
positions in its record as are specified for the prime record key for that file.
A COBOL program can sort to and from these types of files:
◦ Disk files
◦ Tape files
◦ Multiple-reel tape files
◦ Tape files on a multiple-file reel
No more than one file name from a multiple-file reel can be in a SORT statement.
• Sort Input Procedures
The INPUT PROCEDURE phrase defines a sort input procedure that extends from inproc-1
to inproc-2, each of which must identify a Procedure Division section or paragraph. If
THROUGH or THRU is omitted, the compiler assumes an inproc-2 that specifies the same
section or paragraph as inproc-1.
• Sort Output Procedures
The OUTPUT PROCEDURE phrase defines a sort output procedure that extends from outproc-1
to outproc-2, each of which must identify a Procedure Division section or paragraph. If
THROUGH or THRU is omitted, the compiler assumes an outproc-2 that specifies the
same section or paragraph as outproc-1.
• Execution Phases
The execution of the SORT statement consists of three phases:
◦ The input phase transfers records to the sort file sd-name, either from one or more
infile files specified in a USING phrase or from a sort input procedure specified in an
INPUT PROCEDURE phrase.
◦ The sort phase reorders these records according to the sort keys and the applicable
collating sequence.
◦ The output phase either transfers the sorted records to a set of one or more files specified
by outfile in the GIVING phrase or returns them to the sort output procedure specified
in an OUTPUT PROCEDURE phrase.
• Input Phase of Execution
USING phrase
If the USING phrase appears, the input phase transfers all of the records in each input
file infile to the sort file sd-name. When the SORT statement begins executing, each
◦
input file must be either closed but not locked, or open but not in conflict with a following
open for protected input (that is, the same file connector cannot be open).
For each input file, the input phase implicitly opens it in the input mode, executes implicit
“READ infileNEXT AT END …” statements to retrieve the records and implicit release
operations to release them to the sort file, then it implicitly closes the input file. The open
and close operations are equivalent to OPEN INPUT and CLOSE statements without any
optional phrases.
All of these implicit functions are performed such that any associated declarative
procedures are executed; however, the execution of a declarative procedure must not
446 Procedure Division Verbs










