SORT with USING and GIVING
Batch logic depends on order. A control-break report needs its input
grouped by region; a match-merge needs both files in key order; loading a
KSDS sequentially needs ascending keys. The SORT verb gives you that
order inside the program.
The pieces
IDENTIFICATION DIVISION.
PROGRAM-ID. SALESORT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-IN ASSIGN TO SALESIN
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SALES-OUT ASSIGN TO SALESOUT
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-WORK ASSIGN TO SORTWK1.
DATA DIVISION.
FILE SECTION.
FD SALES-IN.
01 SALES-IN-REC PIC X(22).
FD SALES-OUT.
01 SALES-OUT-REC PIC X(22).
SD SORT-WORK.
01 SORT-REC.
05 SRT-REGION PIC X(5).
05 SRT-REP PIC X(10).
05 SRT-AMOUNT PIC 9(5)V99.
PROCEDURE DIVISION.
SORT SORT-WORK
ON ASCENDING KEY SRT-REGION
ON ASCENDING KEY SRT-REP
USING SALES-IN
GIVING SALES-OUT
STOP RUN.
SELECT SORT-WORKnames a work file for the sort. On z/OS the sort product allocates its own work space (SORTWK01...), so the ASSIGN name hardly matters.SD(sort description) replacesFD. Its record defines the key fields by position. The input records are laid over it, so the SD record must match the input layout.- Keys are listed most significant first. Each can be
ASCENDINGorDESCENDING, and you can mix them: region ascending, amount descending gives each region's biggest sales first. USINGnames the input file(s);GIVINGthe output. SORT opens, reads, writes and closes them itself, so they must not be open when the SORT starts.
For this input:
SOUTHKELLY 0012550
NORTHBYRNE 0004000
NORTHWALSH 0015000
the program above writes:
NORTHBYRNE 0004000
NORTHWALSH 0015000
SOUTHKELLY 0012550
Records with equal keys come out in an unspecified order. If input order
must be kept for ties, add WITH DUPLICATES IN ORDER after the keys.
MERGE
MERGE combines files that are already sorted on the same key into one
file, without re-sorting them. Typical use: each branch sends a sorted
transaction file and head office needs one.
IDENTIFICATION DIVISION.
PROGRAM-ID. TXMERGE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DUBLIN-TX ASSIGN TO DUBTX
ORGANIZATION IS LINE SEQUENTIAL.
SELECT CORK-TX ASSIGN TO CRKTX
ORGANIZATION IS LINE SEQUENTIAL.
SELECT ALL-TX ASSIGN TO ALLTX
ORGANIZATION IS LINE SEQUENTIAL.
SELECT MERGE-WORK ASSIGN TO MRGWK.
DATA DIVISION.
FILE SECTION.
FD DUBLIN-TX.
01 DUBLIN-REC PIC X(12).
FD CORK-TX.
01 CORK-REC PIC X(12).
FD ALL-TX.
01 ALL-REC PIC X(12).
SD MERGE-WORK.
01 MERGE-REC.
05 MRG-ACCT PIC X(6).
05 MRG-AMOUNT PIC X(6).
PROCEDURE DIVISION.
MERGE MERGE-WORK
ON ASCENDING KEY MRG-ACCT
USING DUBLIN-TX CORK-TX
GIVING ALL-TX
STOP RUN.
With Dublin holding accounts 100100 and 300300 and Cork holding
100200 and 200200, ALL-TX gets all four in account order. If an
input is out of order, the result is out of order too: MERGE trusts you.
On the job
Many shops sort in JCL with DFSORT or SYNCSORT (PGM=SORT with
SORT FIELDS=(1,5,CH,A,16,7,ZD,D)) rather than in COBOL. Use the COBOL
SORT when the program needs to filter or reshape records around the
sort, which is the next lesson. After a COBOL SORT, check the special
register SORT-RETURN: zero means success.
Your task
The sales extract arrives in random order. The reporting step needs it grouped by region, with each region's biggest sales first.
DD SALESIN and DD SALESOUT — line sequential, 22-byte records:
| Field | PIC |
|---|---|
REGION |
X(5) |
REP |
X(10) |
AMOUNT |
9(5)V99 |
Sort SALESIN into SALESOUT: region ascending, then amount
descending within a region. Records are copied unchanged. Use an SD and
SORT ... USING ... GIVING; don't open either file yourself.