Reading COBOL Layouts

This tutorial on how to read
a COBOL layout was written specifically for our customers who have had a conversion
performed at Disc Interchange and have received a COBOL layout with the data.
It is intended to give you enough information to read most
simple layouts. It does not cover all topics or everything you would
find in a complex layout, and it is intended to explain COBOL layouts only
so you can use your converted data, not so you can write COBOL programs.

Part 5: Tables and Occurs

A powerful feature of COBOL
is the use of tables, via the "OCCURS" and "OCCURS DEPENDING
ON" clauses. This section describes COBOL Tables and the OCCURS and OCCURS
DEPENDING ON clauses, both of which cause fields or groups to repeat some number of times.

But there's an easier way in COBOL. You can specify the field once
and declare that it repeats 12 times. You do this with the OCCURS clause, like this:

05 MONTHLY-SALES OCCURS 12 TIMES PIC S9(5)V99.

(By now you should also know this can be written on two lines like this):

05 MONTHLY-SALES OCCURS 12 TIMES
PIC S9(5)V99.

This specifies 12 fields, all
of which have the same PIC, and is called a table (also called an array).
The individual fields are referenced
in COBOL by using subscripts, such as "MONTHLY-SALES(1)".
This table occupies 84 bytes in the record (12 * (5+2)). (The sign is embedded,
not separate, and the decimal is implied.)

The OCCURS can also be at the group level, and this is the most useful application
of OCCURS. For example, all 25 line items on an invoice (75
fields) could be held in this group:

There can be nested occurs
-- an occurs within an occurs. In the next example, suppose we stock
ten products and we want to keep a record of the monthly sales of each
product for the past 12 months. We could do just that with this table:

In this case, "INVENTORY-ITEM"
is a group composed only of "MONTHLY-SALES", which occurs 12 times for
each occurrence of an inventory item. This gives an array (table)
of 10 * 12 fields. The only information in this record are
the 120 monthly sales figures -- 12 months for each of 10 items.

We could also have a description
for each item. The description would go under the 05 level INVENTORY-ITEM
group, at the 10 level, the same as the monthly sales. Further, we
could track, say, the sale price of each item for each month. A record
which will do these things is:

Notice we have made MONTHLY-SALES
a group, which now contains two fields, and the whole group repeats
12 times for each instance of INVENTORY-ITEM. This short layout has 250 fields: two fields (QUANTITY-SOLD
and UNIT-PRICE) that repeat 12 times for each inventory item, times 10 items, plus
the ITEM-DESCRIPTION field for each of the 10 items. Fields and groups
can be nested several levels deep, and it's possible to have thousands
of fields in a layout only a couple pages long.

Occurs
Depending On

One really great feature of
COBOL tables, and a really nasty one to convert to other languages, is
the "OCCURS DEPENDING ON". This is an OCCURS, like above, but the
number of times it occurs in a particular record can vary (between
some limits). The number of times it actually occurs in any particular
record will be given by a value in another field of that record.
This creates records that vary in size from record to record.

The OCCURS-DEPENDING-ON can
include many subordinate fields and groups, all of which occur multiple
times. Further, most compilers allow one or more (fixed) OCCURS to
be nested within an OCCURS-DEPENDING-ON, and some compilers allow multiple
OCCURS-DEPENDING-ON to be nested, or to occur in succession. This
can get pretty involved, so we will only give one simple example, that
of a patient's medical treatment-history record .

The first three fields "PATIENT-NAME",
"PATIENT-SS-NUMBER", and "NUMBER-OF-TREATMENTS" occur in the fixed portion
of every record. This fixed portion is the same for every record.

The TREATMENT-HISTORY group
is the variable portion of the record. It can occur from 0 to 50 times.

""NUMBER-OF-TREATMENTS" is a
number from 0 to 50 that tells us how many times the group TREATMENT-HISTORY
occurs in this record.

The value in NUMBER-OF-TREATMENTS
is stored in a comp-3 packed format. This is very common. Also very common
is comp or binary format. All of these are binary data formats.

TREATMENT-HISTORY is a group
that is comprised of all the lower level fields beneath it. (Down to the
next 05 level, or the end of the record).

All the fields and groups within
TREATMENT-HISTORY occur between 0 and 50 times.

Because 0 is a valid number
of occurrences, it is possible the variable portion of the record is not
present.

The "INDEXED BY TREATMENT-POINTER"
clause may or may not be present. If present it tells the compiler
the name of the variable (TREATMENT-POINTER) to use as the index into the
array. If you don't understand this, you can safely ignore the "indexed
by..." clause, unless you are programming in COBOL.

TREATMENT-DATE is a group that
is comprised of the day, month, and year fields beneath it.

These records vary in size from
41 to 2041 bytes, and would be stored in some type of variable length file.

Our article Occurs...Depending On
discusses this further, and has suggestions for converting variable tables to PC files.

Additional Information

Our COBOL Conversion Services

Disc Interchange Service Company's primary
business is converting mainframe COBOL files. We have the tools to properly convert
both fixed-length tables and variable-length tables, including nested tables, in either EBCDIC
or ASCII.

With over 32 years of experience with thousands of files,
we have the knowledge to do your job right the first time.