The SORT statement orders any file that can be processed sequentially.
Use this statement if you want to output a sorted file. If you do not need a sorted output file, but simply want a report to be printed in a specific order, you can accomplish this task through the SEQUENCE statement in the REPORT declaratives, as illustrated in the Sample Program Activity Section shown earlier.
The next exhibit diagrams the SORT statement and associated parameters.
SORT file-name-1 + TO file-name-2 + USING (field-name [D]...) + [BEFORE proc-name] + [NAME sort-name]
This is the name of your input file (the file to be sorted). This name must have been previously coded on a FILE statement in the library section of your program and must reference a file-type that can be accessed sequentially, such as SAM, VSAM, ISAM, or VFM.
This parameter provides the name of the sorted output file. If your SORT activity is the permanent reordering of one file, this name can be the same as file-name-1 (not permitted with VSAM or ISAM files). Otherwise, requirements similar to those for file-name-1 must be met for this TO filename, that is, the name must have been previously provided in a FILE statement in your program's library and the file type must be SAM, or VFM.
USING (field-name [D]...)
The USING parameter identifies data fields within the input file (file-name-1) which you can use as sort keys. You can choose any number of fields for sort keys, up to the limit of your installation's sort program.
These data fields must be DEFINEd in the library section before your program can use them.
The subparameter D, following the field-name, causes that field to be sorted in descending order. If you do not specify D, the sort default is ascending order.
This optional parameter identifies a procedure that prescreens, modifies, and selects input records for the sort. Proc-name is the name that appears on the PROC statement that identifies your procedure. Input records are supplied to your sort procedure one at a time. If you use a BEFORE procedure, a SELECT statement must be executed for each record that you want to sort.
If you SELECT a record more than once, it still appears only once on the SORTed file. The next exhibit illustrates the use of the SELECT statement in a BEFORE procedure.
* FILE PERSNL FB(150 1800) OLD-EMP# 9 5 N PAY-GROSS 94 4 P 2 * FILE SORTPER F 150 VIRTUAL * SORT PERSNL + TO SORTPER + USING OLD-EMP# + BEFORE SCREENER * SCREENER. PROC IF PAY-GROSS LT 29999 SELECT END-IF END-PROC *
The optional NAME parameter names the SORT activity. Sort-name can be up to 40 characters long. The first character must be alphabetic. This parameter is used only for documentation purposes. Activity.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!