Lesson 4: Data Division, Data Fields, and PICTURE Clauses
Learning goals
After studying this lesson, learners should be able to:
- Form valid COBOL data names.
- Explain files, records, and fields.
- Distinguish numeric, alphabetic, and alphanumeric data fields.
- Use the File Section and Working-Storage Section.
- Apply common
PICcharacters and editing characters.
Naming rules for data names
A COBOL data name should:
- Use letters, digits, and hyphens only.
- Avoid embedded spaces.
- Contain at least one letter.
- Not begin or end with a hyphen.
- Not be a reserved word.
- Be meaningful and specific.
Good examples:
STUDENT-NO
STUDENT-NAME
TOTAL-SALES
NET-PAY
EOF-SW
Weak examples:
A1 too vague
SID unclear abbreviation
JOHN person-specific and not descriptive
TOTAL too broad if several totals exist
Variable data and constant data
COBOL programs use both changing and fixed data.
| Type | Meaning | Example |
|---|---|---|
| Variable data | Data that can change during processing. | TOTAL-SALES, COUNTER, AVERAGE-GRADE |
| Constant data | Fixed data coded into the program. | VALUE 0, VALUE "YES", report headings |
Data organization: file, record, field
Business programs usually organize data in layers:
- File - a collection of related records.
- Record - one complete unit inside a file, such as one student or one employee.
- Field - one piece of data inside a record, such as name, ID number, or grade.
Example:
File: STUDENT-FILE
Record: STUDENT-RECORD
Fields: STUDENT-NO, STUDENT-NAME, MIDTERM-GRADE, FINAL-GRADE
Types of data fields
COBOL commonly uses three field categories:
| Field type | PIC symbol | Use |
|---|---|---|
| Numeric | 9 | Numbers used for arithmetic. |
| Alphabetic | A | Letters and spaces. |
| Alphanumeric | X | Letters, digits, spaces, and symbols. |
Examples:
05 STUDENT-NAME PIC X(30).
05 AGE PIC 99.
05 SECTION-CODE PIC X(10).
05 LAST-NAME PIC A(20).
Numeric literals
Rules for numeric literals:
- Use digits
0through9. - Do not use commas in the stored numeric literal.
- A sign may appear on the left, such as
-125or+50. - A decimal point may appear inside a numeric literal but not as the final character.
Examples:
| Valid | Invalid | Reason invalid |
|---|---|---|
10.50 | 10,000 | Commas are not allowed in numeric literals. |
-387.58 | $225 | Currency symbol is not part of the numeric literal. |
+10000 | 175- | Sign must not appear at the right. |
Group and elementary fields
A group field contains smaller fields. An elementary field cannot be subdivided further.
Example:
01 STUDENT-RECORD.
05 STUDENT-NO PIC X(10).
05 STUDENT-NAME.
10 LAST-NAME PIC X(15).
10 FIRST-NAME PIC X(15).
10 MIDDLE-INITIAL PIC X.
05 FINAL-GRADE PIC 9V99.
Here, STUDENT-RECORD and STUDENT-NAME are group items. STUDENT-NO, LAST-NAME, FIRST-NAME, MIDDLE-INITIAL, and FINAL-GRADE are elementary items.
Data Division structure
A common Data Division contains two major sections:
DATA DIVISION.
FILE SECTION.
FD file-name.
01 record-name.
05 field-name PIC clause.
WORKING-STORAGE SECTION.
01 variable-name PIC clause VALUE initial-value.
File Section
The File Section describes input and output files and their records.
FILE SECTION.
FD STUDENT-FILE
LABEL RECORD IS STANDARD
RECORD CONTAINS 41 CHARACTERS
DATA RECORD IS STUDENT-RECORD.
01 STUDENT-RECORD.
05 STUDENT-NO PIC X(10).
05 STUDENT-NAME PIC X(25).
05 MIDTERM-GRADE PIC 9V99.
05 FINAL-GRADE PIC 9V99.
Common File Description entries:
| Entry | Meaning |
|---|---|
FD | Begins the file description and names the file. |
LABEL RECORD | Describes whether system labels are standard or omitted. |
RECORD CONTAINS | Indicates record size. |
DATA RECORD IS | Names the record associated with the file. |
01 record-name | Starts the record description. |
Working-Storage Section
The Working-Storage Section stores variables, counters, switches, report headings, and intermediate results that are not directly part of an input file.
WORKING-STORAGE SECTION.
01 EOF-SW PIC 9 VALUE 0.
01 RECORD-COUNT PIC 9(4) VALUE 0.
01 TOTAL-GRADE PIC 9(5)V99 VALUE 0.
01 AVERAGE-GRADE PIC 9V99 VALUE 0.
Common uses:
- Counters
- Totals
- End-of-file switches
- Report headings
- Display/output formatting fields
- Temporary calculation storage
PICTURE clause
The PICTURE or PIC clause defines the type and size of an elementary data item.
05 CUSTOMER-NAME PIC X(30).
05 QUANTITY PIC 9(3).
05 PRICE PIC 9(5)V99.
Rules:
- A group item should not have a
PICclause. - An elementary item normally needs a
PICclause. PIC 9999andPIC 9(4)mean the same thing.- Numeric fields used in arithmetic should use
9, notX.
Common PICTURE characters
| Character | Meaning | Example |
|---|---|---|
9 | Numeric digit | PIC 9(3) stores three digits. |
V | Assumed decimal point | PIC 999V99 treats stored 12345 as 123.45. |
X | Alphanumeric character | PIC X(20) stores up to 20 characters. |
A | Alphabetic character | PIC A(15) stores letters/spaces. |
S | Signed numeric value | PIC S9(4) allows positive/negative values. |
P | Decimal scaling position | Used for special scaling cases. |
Assumed decimal point with V
The V character marks a decimal location without storing an actual decimal point.
| Stored digits | PIC clause | Interpreted value |
|---|---|---|
12050 | PIC 999V99 | 120.50 |
020050 | PIC 999V999 | 020.050 |
125 | PIC 99V9 | 12.5 |
FILLER
FILLER reserves positions that do not need a unique name.
Example:
01 PRINT-LINE.
05 FILLER PIC X(10) VALUE SPACES.
05 PRINT-NAME PIC X(25).
05 FILLER PIC X(5) VALUE SPACES.
05 PRINT-GRADE PIC ZZ9.99.
Use FILLER for spacing, labels, or unused areas in report records.
VALUE clause
The VALUE clause gives an initial value to a Working-Storage item.
05 WS-DISCOUNT PIC V99 VALUE .15.
05 WS-LAB-FEE PIC 9(3) VALUE 350.
05 WS-MORE-RECORDS PIC X(3) VALUE "YES".
05 WS-TITLE PIC X(14) VALUE "MONTHLY REPORT".
Data editing
Data editing formats values for output. It is usually done by moving a raw numeric value into an edited field.
Common editing symbols:
| Symbol | Use |
|---|---|
. | Actual decimal point in displayed output. |
, | Comma insertion. |
Z | Suppress leading zeroes. |
B | Insert blank. |
0 | Insert zero. |
/ | Insert slash, often for dates. |
$ | Insert currency symbol. |
+ or - | Show sign. |
Examples:
05 AMOUNT-IN PIC 9(5)V99 VALUE 0250025.
05 AMOUNT-OUT PIC ZZ,ZZ9.99.
05 DATE-IN PIC 9(6) VALUE 011591.
05 DATE-OUT PIC 99/99/99.
After moving DATE-IN to DATE-OUT, the displayed date is formatted as 01/15/91.