Microsoft KB Archive/40143

Using the SORT Statement in COBOL Version 3.0

PSS ID Number: Q40143 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: In Microsoft COBOL Versions 3.0 and 3.0a, the SORT statement is used to sort one or more file(s) in a desired collating sequence from within a COBOL program. The SORTing sequence uses record keys specified in the code. These keys can be an entire record, or any number of fields within the record. Each key also can be sorted in ASCENDING or DESCENDING order, independently of the other keys in that record.

More Information: There are four different formats of sorting files, as follows: 1. By specifying the USING and GIVING clauses, the SORT statement processes one file, and produces a new file in the desired sorted sequence. The only processing performed on the file is what is needed to read the file, organize the correct order for the keys specified, and writing the records to a new file. 2. By specifying the INPUT PROCEDURE and USING clauses, the SORT statement allows the program to perform a process on the file first, then RELEASE the processed file to the sort operation. The USING statement writes the processed information, which was RELEASEd to the SORT operation, to a new file prior to the sorting sequence. 3. By specifying the OUTPUT PROCEDURE and GIVING clauses, the SORT operation can first process the file, then RETURN the sorted information back from that sorting process. This allows the program to easily produce reports from a file with a given sorted sequence. 4. By specifying the INPUT PROCEDURE and OUTPUT PROCEDURE clauses, the program can RELEASE pre-processed information (from the INPUT PROCEDURE) to the SORT operation, then RETURN the sorted information back from the SORT operation. This allows a program to process specific information from the file, then process that information even further after being sorted into a desired sequence. Note the following: 1. A simple file SORT utilizes the USING and GIVING statements. 2. A file that needs some pre-processing and no post-processing utilizes the INPUT PROCEDURE, USING, and RELEASE statements. 3. A file that needs no pre-processing and some post-processing utilizes the OUTPUT PROCEDURE, GIVING, and RETURN statements. 4. A file that needs both pre-processing and post-processing utilizes the INPUT PROCEDURE, RELEASE, OUTPUT PROCEDURE, and RETURN statements. If a program is going to SORT an ISAM file, the program should use both the INPUT PROCEDURE and OUTPUT PROCEDURE statements, rather then using the GIVING and USING statements. If the GIVING and USING statements are used, then the ISAM file may become corrupted. Note: ISAM files are already in sorted sequence by key. For information on the COBOL Version 3.0 and 3.0a statements RELEASE, RETURN, and SORT, refer to pages 6-16, 6-17, and 6-20 through 6-29, respectfully, in the “Microsoft COBOL 3.0: Language Reference Manual.”

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.