Structured COBOL
Programming
Nancy Stern
Hofstra University
Robert A. Stern
Copyright @ 2000 John Wiley & Sons, In. All
rights reserved. Reproduction or translation of
this work beyond that permitted in Section 117
of the 1976 United States Copyright Act without
the express permission of the copyright owner
is unlawful. Request for further information
should be addressed to the permissions
Department , John Wily & Sons, Inc. The
purchaser may make back-up copies for his/her
own use only and not for distribution or resale.
The Publisher assumes no responsibility for
errors, omissions, or damages, caused by the
use of these programs or from the use of the
information contained herein.
Nassau Community
College
PowerPoint Presentation:
Richard H. Baum, Ph.D.
DeVry Institute of Technology
9th Edition
CHAPTER 14
SORTING AND
MERGING
Structured COBOL Programming, Stern &
OBJECTIVES
To Familiarize you with:
1. How files may be sorted within a COBOL
program.
2. How to process a file during a SORT
procedure before it is actually sorted.
3. How to process a file during a SORT
procedure after it is sorted but before it is
created as output.
4. How to use the MERGE verb for
merging files.
Structured COBOL Programming, Stern &
CONTENTS
THE SOFT FEATURE: AN OVERVIEW
Format of the SORT Statement
ASCENDING or DESCENDING KEY
Coding a Simple SORT Procedure with the USING
and GIVING Options
PROCESSING DATA BEFORE AND/OR AFTER
SORTING
INPUT PROCEDURE
OUTPUT PROCEDURE
When to Use INPUT and/or OUTPUT Procedures
THE MERGE STATEMENT
Structured COBOL Programming, Stern &
THE SORT STATEMENT
Records in files frequently must be sorted
into specific sequences for updating,
answering inquiries, or generating reports.
Sorting is a common procedure used for
arranging records into a specific order so that
sequential processing can be performed.
There are two techniques used for sorting
files processed by COBOL programs.
One is to use either a utility or a database
management system's sort program.
This sort program is completely separate from
the COBOL program.
Structured COBOL Programming, Stern &
FORMAT OF THE SORT STATEME
The second alternative is COBOL's SORT verb,
which can make it very useful as part of a
COBOL program.
Often, a COBOL program will SORT a file prior to
processing it. A simplified format follows:
SORT file-name-1
{ON {DESCENDING}{ASCENDING}
KEY data-name-1. .}
USING file-name-2
GIVING file-name-3
Structured COBOL Programming, Stern &
ASCENDING or DESCENDING
Key
The programmer must specify
whether the key field is to be an
ASCENDING KEY or a DESCENDING
KEY, depending on which sequence
is required:
(1) ASCENDING: From lowest to highest
(2) DESCENDING: From highest to lowest
Records may be sorted using either
numeric or nonnumeric key fields.
Structured COBOL Programming, Stern &
ASCENDING or
DESCENDING Key
Collating Sequence
The two major codes used for
representing data in a computer are
EBCDIC primarily used on mainframes,
and ASCII used widely on PCs.
The sequence of characters from
lowest to highest, which is referred to
as the collating sequence, is somewhat
different in EBCDIC and ASCII.
Structured COBOL Programming, Stern &
Collating Sequence
EBCDIC
Low Blank
ASCII
Blank
Special Characters
Special Characters
Lowercase letters a-z
Integers 0-9
Uppercase letters A-Z
Characters
Integers 0-9
A-Z
High
Special
Uppercase letters
Lowercase letters a-z
Structured COBOL Programming, Stern &
Sequencing Records with
More Than One SORT Key
The SORT verb may be used to
sequence records with more than one
key field.
Suppose that we wish to sort fields in
ascending alphabetic sequence by
name, within each office. That is:
Office number is the major sort field
Level number is the intermediate sort field
Name is the minor sort field
Structured COBOL Programming, Stern &
Sequencing Records with
More Than One SORT Key
The following is a SORT statement that
sorts records into ascending alphabetic
NAME sequence within LEVEL-NO within
OFFICE-NO:
SORT SORT-FILE
ON ASCENDING KEY OFFICE-NO
ON ASCENDING KEY LEVEL-NO
ON ASCENDING KEY NAME
USING PAYROLL-FILE-IN
GIVING SORTED-PAYROLL-FILE-OUT
Structured COBOL Programming, Stern &
ASCENDING / DESCENDING
Key
If all key fields are to be sorted in
ascending sequence we can code
using the phrase ON ASCENDING KEY
only once:
SORT SORT-FILE
ON ASCENDING KEY MAJOR-KEY
INTERMEDIATE -KEY
MINOR-KEY
Structured COBOL Programming, Stern &
Coding a Simple SORT
Procedure with the USING
and
GIVING
Options
There
are three major files used in a
sort:
1. Input file: File of unsorted input
records.
2. Work or sort file: File used to store
records temporarily during the sorting
process.
3. Output file: File of sorted output
records.
Structured COBOL Programming, Stern &
Coding a Simple SORT
Procedure with the USING
and
GIVING
Options
A sort file is usually assigned to a
special work device--in the
ENVIRONMENT DIVISION--indicated as
SYSWORK in the following:
SELECT UNSORTED-MASTERN-FILE
ASSIGN
TO DISK1.
SELECT SORT-FILE ASSIGN TO SYSWORK.
SELECT SORTED-MASTERN-FILE ASSIGN
TO
DISK2.
Structured COBOL Programming, Stern &
Coding a Simple SORT
Procedure with the
USING/GIVING Options
The SORT-FILE is actually assigned to a
temporary work area that is used during
processing but not saved.
Only the unsorted disk file and the sorted
output disk file are assigned standard filenames so that they can be permanently
stored.
FDs are used in the DATA DIVISION to
define and describe the input and output
files in the usual way.
The sort or work file is described with an
SD entry.
Structured COBOL Programming, Stern &
QUESTIONS! ??
Structured COBOL Programming, Stern &
SELF-TEST
1. Suppose we want EMPLOYEE-FILE records in
alphabetic order by NAME within DISTRICT
within TERRITORY, all in ascending sequence.
The output file is called SORTED-EMPLOYEEFILE. Complete the following SORT
statement: SORT WORK-FILE ...
Solution:
ON ASCENDING KEY TERRITORY
ON ASCENDING KEY DISTRICT
ON ASCENDING KEY NAME
USING EMPLOYEE-FILE
GIVING SORTED-EMPLOYEE-FILE
Structured COBOL Programming, Stern &
SELF-TEST
2. How many files are required in a
SORT routine? Describe these files.
Solution:
Three; input--unsorted;
file--temporary;
output--sorted
Structured COBOL Programming, Stern &
work or sort
SELF-TEST
3. The work or sort file is defined as an
______ in the DATA DIVISION.
Solution: SD (SORT DESCRIPTION)
Structured COBOL Programming, Stern &
SELF-TEST
4. Suppose we have an FD called NET-FILEIN, an SD called NET-FILE, and an FD called
NET-FILE-OUT.
We want NET-FILE-OUT sorted into ascending
DEPT-NO sequence. Code the PROCEDURE
DIVISION entry.
Solution:
SORT NET-FILE
ON ASCENDING KEY DEPT-NO
USING NET-FILE-IN
GIVING NET-FILE-OUT
Structured COBOL Programming, Stern &
SELF-TEST
5. In Question 4, DEPT-NO must be a
field defined within the (SD/FD) file
Solution: SD
Structured COBOL Programming, Stern &
PROCESSING DATA BEFORE
AND/OR AFTER SORTING
The SORT statement can be used in
conjunction with procedures that process
records before and after they are sorted.
The SORT statement is used to perform some
processing of incoming records just before
they are sorted.
For incoming records, this is
accomplished with an INPUT PROCEDURE
clause in place of the USING clause using
the following format:
Structured COBOL Programming, Stern &
INPUT PROCEDURE
Expanded Format:
SORT file-name-1
{ON {ASCENDING}{DESCENDING} KEY dataname-1 . . . .}. . .
{INPUT PROCEDURE IS procedure-name-1
[{THRU}{THROUGH} procedure-name-2]
{USING file-name-2 . . . .}
GIVING file-name-3
The INPUT PROCEDURE processes data
from the incoming file prior to sorting.
Structured COBOL Programming, Stern &
INPUT PROCEDURE
We must release records to the sort file in
an INPUT PROCEDURE.
With a USING option, this is done for us
automatically.
Note that the RELEASE verb is followed
by a record-name, just like the WRITE
statement.
With COBOL 85, the INPUT PROCEDURE
may reference either a section or a
paragraph.
Structured COBOL Programming, Stern &
INPUT PROCEDURE
Regardless of whether you are using
COBOL 85 or COBOL 74, an INPUT
PROCEDURE :
opens the input file
processes input records, and
releases them to the sort file.
After all input records are processed, the
input file is closed.
The format for the RELEASE is:
RELEASE sort-record-name-1
[FROM identifier-1]...
Structured COBOL Programming, Stern &
INPUT PROCEDURE
RELEASE is the verb to write records
to a sort file:
Examples:
MOVE IN-REC TO SORT-REC
RELEASE SORT-REC.
OR
RELEASE SORT-REC FROM IN-REC.
Structured COBOL Programming, Stern &
MORE QUESTIONS
Structured COBOL Programming, Stern &
SELF-TEST
The following illustrates the problem definition
for a program that is to sort a disk file.
UNSORTED-FILE Record Layout (Input)
Field Size
Type
PART-NO-IN
Alphanumeric
QTY-IN
Alphanumeric
DEPT-IN
Alphanumeric
-----------------------------------------------------------------
SORTED-FILE Record Layout (Output)
DEPT-OUT
PART-NO-OUT
QTY-OUT
2
5
5
Alphanumeric
Alphanumeric
Alphanumeric
Structured COBOL Programming, Stern &
SELF-TEST: Consider the
following FILE SECTION:
DATA DIVISION.
FILE SECTION.
FD UNSORTED-FILE
LABEL RECORDS ARE STANDARD.
01 REC-1.
05 PART-NO-IN
PIC X(5).
05 QTY-IN PIC X(5).
05 DEPT-IN PIC X(2).
SD SORT-FILE.
01 SORT-REC.
05 S-DEPT PIC X(2).
05 S-PART-NO
PIC X(5).
05 S-QTY
PIC X(5).
FD SORTED-FILE
LABEL RECORDS ARE STANDARD.
01 REC-2
PIC X(12).
Structured COBOL Programming, Stern &
SELF TEST
1. (T or F) It would be possible, although
inefficient, to
(1) first sort the input and produce a sorted
master, and
(2) then code a separate module to read
from the sorted master, moving the data in
a rearranged format to a new sorted
master.
SOLUTION: T
Structured COBOL Programming, Stern &
SELF TEST
2. (T or F) It would be more efficient to
use an INPUT PROCEDURE for this
problem.
SOLUTION: T
Structured COBOL Programming, Stern &
SELF TEST
3. Code the SORT Statement.
SOLUTION:
SORT SORT-FILE
ON ASCENDING KEY S-DEPT
INPUT PROCEDURE A000-REARRANGE
GIVING SORTED-FILE
STOP RUN.
Structured COBOL Programming, Stern &
SELF TEST
4. Code the INPUT PROCEDURE SECTION
assuming that you are using a COBOL 85
compiler.
SOLUTION:
A00-REARRANGE.
OPEN INPUT UNSORTED-FILE
PERFORM UNTIL NO-MORE-RECORDS
READ UNSORTED-FILE
AT END
MOVE NO TO ARE-THERE-MORENOT AT END
PERFORM A200-CALC-RTN
END-READ
END-PERFORM
CLOSE UNSORTED-FILE.
Structured COBOL Programming, Stern &
RECORDS
SELF TEST
SOLUTION 4 (continued)
A200-CALC-RTN.
MOVE PART-NO-IN TO S-PART-NO
MOVE QTY-IN TO S-QTY
MOVE DEPT-IN TO S-DEPT
RELEASE SORT-REC.
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
After records have been sorted, they are
placed in the sort file in the sequence
required.
If the GIVING option is used, then the sorted
records are automatically written onto the
output file after they are sorted.
In order to process the sorted records prior
to, or perhaps even instead of, placing them
in the output file, we would use an OUTPUT
PROCEDURE instead of the GIVING option.
This OUTPUT PROCEDURE is very similar to the
INPUT PROCEDURE.
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
The full format for the SORT, including both
INPUT and OUTPUT PROCEDURE options, is
as follows:
SORT file-name-1 {ON{DESCENDING}
{ASCENDING} KEY data-name-1 . . . }
{INPUT PROCEDURE IS procedure-name-1
{USING file-name-2 [{THROUGH} {THRU}
procedure-name-2]}
{OUTPUT PROCEDURE IS procedure-name-3
{GIVING file-name-3 . [{THROUGH} {THRU
procedure-name-4]} .
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
As indicated, an INPUT PROCEDURE, if
used, is processed prior to sorting.
When the SORT verb is encountered,
control goes to the INPUT PROCEDURE.
When the INPUT PROCEDURE is complete,
the file is then sorted.
An OUTPUT PROCEDURE processes all
sorted records in the sort file and
handles the transfer of these records to
the output file.
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
Format:
RETURN sort-file-name-1
AT END imperative statement-1
[NOT AT END imperative
statement-2]*
[END-RETURN] *
* Valid with COBOL 85 only
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
SUMMARY:
COBOL 85
1. The OUTPUT PROCEDURE of the SORT
should refer to a paragraph-name, but it
could refer to a section name.
Example:
100-MAIN-MODULE.
SORT WORK-FILE
USING IN-FILE
OUTPUT PROCEDURE 200-AFTERMODULE
STOP RUN.
200-AFTER-SORT-MAIN-MODULE.
Structured COBOL Programming, Stern &
...
SORT-MAIN-
OUTPUT PROCEDURE
SUMMARY: COBOL 85
2. In the paragraph specified in the
OUTPUT PROCEDURE:
a. OPEN the output file.
b. PERFORM a paragraph that will
RETURN and process records from the
sort file until there is no more data.
c. After all records have been processed,
CLOSE the output file.
d. When the OUTPUT PROCEDURE
paragraph has-been fully executed,
control will then return to the SORT.
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
SUMMARY: COBOL 85
Example of the OUTPUT Procedure:
200-AFTER-SORT-MAIN-MODULE.
OPEN OUTPUT SORTED-FILE
PERFORM UNTIL NO-MORE-RECS
RETURN WORK-FILE
AT END MOVE NO TO MORE-RECS
NOT AT END PERFORM 300-PROCESSSORT-RECS
END-RETURN
END-PERFORM
CLOSE SORTED-FILE.
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
SUMMARY: COBOL 85
3. At the paragraph that processes the
sort records after they have been
sorted but before they are output:
a. Perform any operations the work or
sort records.
b. MOVE the work or sort record to the
output area.
c. WRITE each sort record to the output
file. (A WRITE....FROM can be used in
place of a MOVE and WRITE.)
Structured COBOL Programming, Stern &
OUTPUT PROCEDURE
SUMMARY: COBOL 85
Example:
300-PROCESS-SORT-RECS.
.
.
file]
[Process records in the sort
.
WRITE SORTED-REC FROM WORKREC.
Structured COBOL Programming, Stern &
When To Use INPUT and/or
OUTPUT PROCEDURES
Sometimes it is more efficient to process
data before it is sorted in an INPUT
PROCEDURE .
Other times it is more efficient to process
data after it is sorted in an OUTPUT
PROCEDURE.
Keep in mind that you must use either an
INPUT or an OUTPUT PROCEDURE if the
unsorted and sorted files have differentsized fields or have fields in different
order.
Structured COBOL Programming, Stern &
THE MERGE STATEMENT
COBOL has a MERGE statement that will
combine two or more files into a single
file. Its format is similar to that of the
SORT:
MERGE file-name-1 {ON {ASCENDING}
{DESCENDING} KEY data-name-1. .}
USING file-name-2 {file-name-3} . . .
{OUTPUT PROCEDURE IS procedure-name-1
GIVING {file-name-4 [{THROUGH}{THRU}
procedure-name-2]}
Structured COBOL Programming, Stern &
THE MERGE STATEMENT
File-name-1 is a work file designated as an
SD.
The key field specified as data-name-1, and any
subsequent key fields, are defined within the SD.
The first key field indicated in the
ASCENDING or DESCENDING KEY clause of
the MERGE is the major one, followed by
intermediate and minor key fields.
Rules for ASCENDING/DESCENDING KEY,
USING, GIVING, and OUTPUT PROCEDURE
are the same as for the SORT.
Structured COBOL Programming, Stern &
THE MERGE STATEMENT
With the USING clause, we indicate the files
to be merged.
Unlike the SORT, however, an INPUT
PROCEDURE may not be specified with a
MERGE statement: you may only process
records after they have been merged, not
before.
The OUTPUT PROCEDURE has the same
format as the SORT and may be used with a
MERGE to:
1. Flag duplicate records as errors.
2. Ensure duplicate records.
Structured COBOL Programming, Stern &
THE MERGE STATEMENT
The MERGE statement automatically
handles the opening, closing, and
input/output (READ/WRITE functions)
associated with the files.
The files to be merged must each be in
sequence by the key field.
If ASCENDING KEY is specified, then the
merged output file will have records in
increasing order by key field.
If DESCENDING KEY is specified, the merged
output file will have key fields from high to
low.
Structured COBOL Programming, Stern &
CHAPTER SLIDES END HERE
CHAPTER SUMMARY COMES
NEXT
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
A. The SORT is used for sorting records in
either ascending or descending order.
1. A program can simply sort a file on key
fields:
SORT file-name-1
{ON {ASCENDING}{DESCENDING} KEY
data-name1...}...
USING file-name-2
GIVING file-name-3
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
a. File-name-1 is a work or sort file that is
described with an SD (sort file description)
in the FILE SECTION.
b. The KEY field(s) to be sorted are datanames defined within the SD or sort file.
c. Files can be sorted into ascending or
descending sequence.
d. Files can be sorted using more than one
key field. The first field specified is the
main sort field followed by intermediate
and/or minor ones.
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
2. A program can include an entirely
separate routine that processes an
unsorted file prior to performing the
SORT and/or an entirely separate
routine that processes the sorted file
after the SORT is executed:
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
3. An INPUT PROCEDURE that is part of
the SORT statement permits
processing of the unsorted file just
before the sort is performed, yet
under the control of the SORT itself:
SORT file-name-1
...
INPUT PROCEDURE procedure-name-1
GIVING file-name-2
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
a. COBOL 85 uses a paragraph-name
when specifying an INPUT PROCEDURE.
b. With COBOL 85 paragraph-names can
be substituted for section-names so
there is no need for a GO TO to branch
to the end of a section.
c. RELEASE sort-rec FROM unsorted-rec is
necessary in an INPUT PROCEDURE to
make input records available for sorting.
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
4. An OUTPUT PROCEDURE that is part
of the SORT statement permits
processing of the sorted work (or sort)
file records before they are written to
the sorted file:
SORT file-name-1...
{USING file-name-2}
{INPUT PROCEDURE procedure-name-1}
OUTPUT PROCEDURE procedure-name-2
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
a. As with the INPUT PROCEDURE, the
procedure-name specified must be a
section-name for standard COBOL 74 but
can be either a section- or paragraph-name
with COBOL 85.
Using paragraph-names simplifies the coding
and eliminates the need for GO TOs.
b. An OUTPUT PROCEDURE:
(1) Opens the output file.
(2) Includes a RETURN sort-file-name AT END
... which is like
a READ.
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
(3) Processes all records from the sort file
before writing them to the sorted- filename.
(4) Uses a RETURN in place of a READ for
all inputting of sort-file records.
(5) Closes the output sorted-file after all
records have been processed.
c. Both an INPUT and an OUTPUT
PROCEDURE can be used in a program.
Structured COBOL Programming, Stern &
CHAPTER SUMMARY
B. The MERGE statement can be used
to merge two or more files.
It is very similar to the SORT.
It can have a USING and GIVING option
or an OUTPUT PROCEDURE in place of
the GIVING option.
It cannot, however, have an INPUT
PROCEDURE.
Structured COBOL Programming, Stern &