Michael Coughlan
Page 9
The type of a group item is always assumed to be alphanumeric (PIC X, even if it contains only numeric data items) because a group item may have several different data items and types subordinate to it, and an alphanumeric picture is the only one that can support such collections.
Level Numbers
Level numbers 01 through 49 are the general level numbers used to express data hierarchy. There are also special level numbers such as 66, 77, and 88:
• Level 66 is used with the RENAMES clause. The RENAMES clause allows you to apply a new name
to a data-name or group of contiguous data-names. It is similar to the REDEFINES clause; but
because of the maintenance problems associated with it, the RENAMES clause has largely fallen
into disuse and in some programming shops is banned. You learn more about the operation
and shortcomings of the RENAMES clause when the REDEFINES clause is examined later in
the book.
• Level 77 is used to identify a noncontiguous, single data item in the WORKING-STORAGE or
LINKAGE sections; it cannot be subdivided, and it cannot be part of a group item. In the past,
77s were used for efficiency purposes (77s used less memory than the same items defined as
a level 01). Nowadays level 01 is often used instead of 77. Some programming shops take the
view that, instead of declaring large numbers of indistinguishable 77s, it is better to collect the
individual items into named groups for documentation purposes even if the group, as a group,
has no practical purpose. For instance, you might group individual totals such as ShopTotal,
CityTotal, StateTotal, and CountryTotal under the group item Totals for documentation
purposes. You could not create such a grouping if the items were declared using level 77s.
• Level 88 is used to implement condition names. Whereas level 66 and level 77 are not used
in modern COBOL, level 88s and condition names are very important, useful, and unique
weapons in COBOL’s armory. Chapter 5 provides a detailed examination of the declaration
and use of level 88s and condition names.
47
Chapter 3 ■ Data DeClaration in CoBol
Data Hierarchy
Level numbers are used to express data hierarchy. The best way to understand the data hierarchy and the data-manipulation opportunities afforded by this organization is by means of an example.
Suppose you want to store information about students. You can create a data item called StudentRec and
describe it as follows:
01 StudentRec PIC X(44).
You can load some data into StudentRec using this statement:
MOVE "1205621William Fitzpatrick 19751021LM051385" TO StudentRec.
Once you have done this, the StudentRec area of storage is instantiated with the data as shown in Example 3-5.
Example 3-5. StudentRec as an Elementary Data Item
WORKING-STORAGE SECTION.
01 StudentRec PIC X(44).
StudentRec
1 2 0 5 6 2 1 W I l l i a m F i t z p a t r i c k
1 9 7 5 1 0 2 1 L M 0 5 1 3 8 5
You can see that the data in StudentRec consists of a number of pieces of information: the student’s ID, the student’s name, the date of birth, the course ID, and the student’s grade point average (GPA). But because you have defined StudentRec as an elementary item (an undifferentiated string of characters), you have no easy way of getting at the individual pieces of data. You can move or display the contents of the entire string, but you cannot easily, for instance, display only the date of birth or the student’s name.
What you need to do is to describe StudentRec as a group item that is subdivided into StudentId, StudentName, DateOfBirth, CourseId, and GPA. The revised StudentRec description and the way it appears in storage are shown in Example 3-6.
Example 3-6. StudentRec as a Group Data Item
WORKING-STORAGE SECTION.
01 StudentRec.
02 StudentId PIC 9(7).
02 StudentName PIC X(21).
02 DateOfBirth PIC X(8).
02 CourseId PIC X(5).
02 GPA PIC 9V99.
StudentRec
StudentId
StudentName
DateOfBirth
CourseId
GPA
1 2 0 5 6 2 1 W I l l i a m
F i t z p a t r i c k
1 9 7 5 1 0 2 1 L M 0 5 1 3 8 5
StudentRec has been subdivided into a number of individual data items. Because StudentRec is now a group
item, it has no PICTURE clause. Its size is the sum of the sizes of the elementary items, and its type is alphanumeric.
48
Chapter 3 ■ Data DeClaration in CoBol
Defining StudentRec this way presents you with a number of data-manipulation opportunities. You can still
manipulate the whole record. For instance, you can flush the entire 44-character area with spaces using a statement like this:
MOVE SPACES TO StudentRec
Or you can move the entire contents to another data item with a statement such as
MOVE StudentRec to StudentRecCopy
But now you can manipulate the individual pieces of data with statements such as these:
DISPLAY "Student name = " StudentName
MOVE ZEROS TO StudentId
MOVE "LM067" TO CourseId
DISPLAY "Current GPA = " GPA
MOVE 2.55 TO GPA
MOVE 19751022 TO DateOfBirth
It is useful to be able to access the individual pieces of data that constitute StudentRec, but the structure is still not quite granular enough. For instance, you can only access the entire student name. It would be nice to be able to manipulate the forename and the surname individually. Similarly, you would like to be able to access the year, month, and day of birth as separate items.
To make these changes, you need to describe StudentName and DateOfBirth as group items. The revised
StudentRec description and the effect of this restructuring on the data storage are shown in Example 3-7.
Example 3-7. StudentRec as a Group Data Item
WORKING-STORAGE SECTION.
01 StudentRec.
02 StudentId PIC 9(7).
02 StudentName.
03 Forename PIC X(9).
03 Surname PIC X(12).
02 DateOfBirth.
03 YOB PIC 9(4).
03 MOB PIC 99.
03 DOB PIC 99.
02 CourseId PIC X(5).
02 GPA PIC 9V99.
StudentRec
StudentName
DateOfBirth
StudentId
CourseId
GPA
Forename
Surname
YOB
MOB DOB
1 2 0 5 6 2 1 W I l l i a m
F i t z p a t r i c k
1 9 7 5 1 0 2 1 L M 0 5 1 3 8 5
49
Chapter 3 ■ Data DeClaration in CoBol
With this new structure, many more data-manipulation opportunities become available. You can still access
StudentRec, StudentId, StudentName, DateOfBirth, CourseId, and GPA as before, but now you can also manipulate the data at a more granular level using statements like these:
DISPLAY "Student date of birth is " DOB "/" MOB "/" YOB
DISPLAY "Student name = " Surname "," SPACE Forename
MOVE "Billy" TO Forename
MOVE 22 TO DOB
The first statement displays the date of birth on the computer screen:
Student date of birth is 21/10/1975
The second statement displays the student’s name:
Student name = Fitzpatrick, William
■ COBOL Detail When a figurative constant is used with the DISPLAY verb, it inserts only one character; it does not matter if SPACE or SPACES is used, because they are synonyms. if you wanted to insert two space
s in the previous statement, you would have to write the statement as follows:
DISPLAY "Student name = " Surname "," SPACE SPACE Forename
Level-Number Relationships Govern Hierarchy
Level numbers express the data hierarchy. A data hierarchy starts with level number 01; the subdivisions then use any number between 02 and 49 to express the hierarchy. The rule to remember is this: a data item with a level number higher than the preceding data item is a subdivision of that data item; a data item with the same level number as the preceding item is at the same level as that item; a data item with a level number lower than the preceding item is at the same level as its first matching preceding level number. For instance, in Example 3-7, Forename has a level number higher than StudentName, so Forename is a subdivision of StudentName. Surname has the same level number as Forename, so it too is a subdivision of StudentName.
In a hierarchical data description, what is important is the relationship of the level numbers to one another, not the actual level numbers used. For instance, the record descriptions shown in Example 3-8, Example 3-9, and Example 3-10 are equivalent.
Example 3-8 shows my preferred organization and the one used in this book. This organization seems logical and offers benefits of clarity by making the level numbers coincidental with the levels in the structure.
Example 3-8. Preferred Format
01 StudentRec.
02 StudentId PIC 9(7).
02 StudentName.
03 Forename PIC X(9).
03 Surname PIC X(12).
02 DateOfBirth.
03 YOB PIC 9(4).
03 MOB PIC 99.
03 DOB PIC 99.
50
Chapter 3 ■ Data DeClaration in CoBol
02 CourseId PIC X(5).
02 GPA PIC 9V99.
Example 3-9 shows that level numbers are used only to show the relationship with the immediately preceding items. For instance, the fact that YOB, MOB, and DOB use level number 14 and Forename and Surname use level 12 is not relevant. What is relevant is that the level numbers show that the items are subordinate to their respective preceding items (StudentName in the first case and DateOfBirth in the second).
Example 3-9. Arbitrary Format
01 StudentRec.
07 StudentId PIC 9(7).
07 StudentName.
12 Forename PIC X(9).
12 Surname PIC X(12).
07 DateOfBirth.
14 YOB PIC 9(4).
14 MOB PIC 99.
14 DOB PIC 99.
07 CourseId PIC X(5).
07 GPA PIC 9V99.
Example 3-10 uses a layout often found in programming shops and COBOL books. This organization lets you
create group items by slipping a group item into the description without otherwise disturbing the layout (see the MOBandDOB data item in Example 3-11). This seems like a good idea, and it is is part of the coding standards for some programming shops; but over time, this approach causes the structure’s clarity to deteriorate. In my view, if the data hierarchy needs to be reorganized, then it should be reorganized properly, so that future maintenance programmers will have no difficulty in comprehending the structure created.
Example 3-10. Common Format
01 StudentRec.
05 StudentId PIC 9(7).
05 StudentName.
10 Forename PIC X(9).
10 Surname PIC X(12).
05 DateOfBirth.
10 YOB PIC 9(4).
10 MOB PIC 99.
10 DOB PIC 99.
05 CourseId PIC X(5).
05 GPA PIC 9V99.
Example 3-11. Common Format in Use
01 StudentRec.
05 StudentId PIC 9(7).
05 StudentName.
10 Forename PIC X(9).
10 Surname PIC X(12).
05 DateOfBirth.
08 YOB PIC 9(4).
51
Chapter 3 ■ Data DeClaration in CoBol
08 MOBandDOB.
10 MOB PIC 99.
10 DOB PIC 99.
05 CourseId PIC X(5).
05 GPA PIC 9V99.
Summary
This chapter provided an introduction to data in COBOL. It introduced the different types of data used, and it showed how you can create and use variable data in the form of elementary data items. The chapter examined the assignment operation and discussed the data-manipulation opportunities afforded by the hierarchical structure of group item data declarations.
But this is just an introduction to data declaration in COBOL. In the rest of this book, you expand your knowledge of data declaration by exploring such topics as the implicit redefinition of data items in the FILE SECTION, the operation of the REDEFINES clause, the preparation of data for output using edited pictures, the USAGE clause, and the declaration of tabular information.
LaNGUaGe KNOWLeDGe eXerCISe
Using a 2B pencil, write your answer to each exercise question in the area provided.
1. Create an elementary data item called TaxAmount to hold a value between 0 and 99,999.99.
_____________________________________________________________________
2. Create an alphanumeric elementary data item called VideoName large enough to hold 35
characters. Define VideoName so that it is initialized with spaces when the program starts.
_____________________________________________________________________
3. a data item called MinimumWage is defined as PIC 9V99. Show what happens to the data
after execution of the statement
MOVE 123.5 TO MinimumWage
MinimumWage
4. a group item called CustomerRec is defined
01 CustomerRec.
02 CustId PIC 9(5) VALUE ZEROS.
02 CustName.
03 Initials PIC XX VALUE SPACES.
03 Surname PIC X(4) VALUE SPACES.
02 Gender PIC X. VALUE SPACES.
02 Payment PIC 9(5)V99 VALUE ZEROS.
52
Chapter 3 ■ Data DeClaration in CoBol
a. a partial diagram representing the CustomerRec is provided. Complete the diagram by
showing how the subordinate data items map to the 19 characters of storage reserved
for CustomerRec.
b. in the first row of the diagram, show how each VALUE clause initializes the data items in
CustomerRec.
c. For each statement in the following program, show what happens to the data in
CustomerRec. Use a row for each statement.
PROCEDURE DIVISION.
10-BEGIN.
MOVE "45145MCRyanF23445.67" TO CustomerRec
MOVE "Male" TO Gender
MOVE "GSPower" TO CustName
MOVE "Fitzroy" TO Surname
MOVE 34 TO Cust-Payment
STOP RUN.
CustomerRec
CustName
CustId
Gender
Payment
Initials�
Surname
LaNGUaGe KNOWLeDGe eXerCISe—aNSWerS
1. Create an elementary data item called TaxAmount to hold a value between 0 and 99,999.99.
01 TaxAmount PIC 9(5)V99.
2. Create an alphanumeric elementary data item called VideoName large enough to hold 35
characters. Define VideoName so that it is initialized with spaces when the program starts.
01 VideoName PIC X(35).
53
Chapter 3 ■ Data DeClaration in CoBol
3. a data item called MinimumWage is defined as PIC 9V99. Show what happens to the data
after execution of the statement
MOVE 123.5 TO MinimumWage
MinimumWage
3
5
0
•
4. a group item called CustomerRec is defined as
01 CustomerRec.
02 CustId PIC 9(5) VALUE ZEROS.
02 CustName.
03 Initials
PIC XX VALUE SPACES.
03 Surname PIC X(4) VALUE SPACES.
02 Gender PIC X. VALUE SPACES.
02 Payment PIC 9(5)V99 VALUE ZEROS.
a. a partial diagram representing the CustomerRec is provided. Complete the diagram by
showing how the subordinate data items map to the 19 characters of storage reserved
for CustomerRec.
b. in the first row of the diagram, show how each VALUE clause initializes the data items in
CustomerRec.
c. For each statement in the following program, show what happens to the data in
CustomerRec. Use a row for each statement.
PROCEDURE DIVISION.
10-BEGIN.
MOVE "45145MCRyanF23445.67" TO CustomerRec
MOVE "Male" TO Gender
MOVE "GSPower" TO CustName
MOVE "Fitzroy" TO Surname
MOVE 34 TO Cust-Payment
STOP RUN.
CustomerRec
CustName
CustId
Gender
Payment
Initials
Surname
0
0
0
0
0
*
*
*
*
*
*
*
0
0
0
0
0
0
0
4
5
1
4
5
M
C
R
y
a
n
F
2
3
4
4
5
6
7
4
5
1
4
5
M
C
R
y
a
n
M
2
3
4
4
5
6
7
4