MainframeMaster

COBOL Data Definition

Data definition declares variables, data structures, and their characteristics in COBOL programs. Learn to use level numbers, PICTURE clauses, and data type specifications for effective data management.

Basic Data Definition

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(6). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(9)V99. 05 CUSTOMER-STATUS PIC X. *> Independent data items 77 COUNTER PIC 9(4) VALUE 0. 77 EOF-FLAG PIC X VALUE 'N'. 88 END-OF-FILE VALUE 'Y'.

Define data structures using level numbers: 01 for major groups, 05-49 for subgroups, and 77 for independent items. Use PICTURE clauses to specify data format and size. Include VALUE clauses for initialization.

PICTURE Clauses

cobol
1
2
3
4
5
6
7
8
9
10
WORKING-STORAGE SECTION. 01 PICTURE-EXAMPLES. 05 NUMERIC-FIELD PIC 9(6). *> 6 digits 05 DECIMAL-FIELD PIC 9(6)V99. *> 6 digits, 2 decimal places 05 ALPHANUMERIC PIC X(20). *> 20 characters 05 ALPHABETIC PIC A(15). *> 15 letters only 05 SIGNED-NUMERIC PIC S9(5). *> Signed 5-digit number 05 ZERO-SUPPRESSED PIC Z(5)9. *> Suppress leading zeros 05 EDITED-AMOUNT PIC $,$$9.99. *> Currency format 05 PHONE-NUMBER PIC 999-999-9999. *> Formatted phone

Use PICTURE clauses to define data formats: 9 for numeric digits, X for alphanumeric, A for alphabetic, V for decimal point, S for sign, Z for zero suppression, and editing characters for formatting.

Level Numbers and Hierarchy

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WORKING-STORAGE SECTION. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(6). 05 EMPLOYEE-NAME. 10 FIRST-NAME PIC X(15). 10 LAST-NAME PIC X(20). 05 EMPLOYEE-ADDRESS. 10 STREET PIC X(30). 10 CITY PIC X(20). 10 STATE PIC X(2). 10 ZIP PIC X(10). 05 EMPLOYEE-SALARY PIC 9(7)V99. *> Accessing hierarchical data MOVE 'JOHN' TO FIRST-NAME MOVE 'SMITH' TO LAST-NAME DISPLAY 'Full name: ' EMPLOYEE-NAME

Create hierarchical data structures using level numbers. Higher-level items (01) contain lower-level items (05-49). Access individual fields by their names or group items by their level numbers.

Data Types and Usage

cobol
1
2
3
4
5
6
7
8
9
WORKING-STORAGE SECTION. 01 DATA-TYPES. 05 DISPLAY-NUMERIC PIC 9(6). *> Character format 05 COMP-NUMERIC PIC 9(6) COMP. *> Binary format 05 COMP3-NUMERIC PIC 9(6) COMP-3. *> Packed decimal 05 COMP5-NUMERIC PIC 9(6) COMP-5. *> Native binary 05 INDEX-NUMERIC PIC 9(6) INDEX. *> Index value 05 POINTER-VALUE USAGE POINTER. *> Memory address 05 PROCEDURE-POINTER USAGE PROCEDURE-POINTER.

Specify data usage with USAGE clauses: DISPLAY (default), COMP (binary), COMP-3 (packed decimal), COMP-5 (native binary), INDEX (for table indexing), POINTER, and PROCEDURE-POINTER for different storage and processing requirements.

Group Items vs Elementary Items

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WORKING-STORAGE SECTION. 01 GROUP-ITEM. 05 ELEMENTARY-ITEM-1 PIC X(10). 05 ELEMENTARY-ITEM-2 PIC 9(5). 05 SUB-GROUP. 10 SUB-ELEMENT-1 PIC X(5). 10 SUB-ELEMENT-2 PIC 9(3). *> Group items have no PICTURE clause *> Elementary items have PICTURE clauses *> Group items can contain other groups or elementary items PROCEDURE DIVISION. MOVE 'HELLO' TO ELEMENTARY-ITEM-1 MOVE 12345 TO ELEMENTARY-ITEM-2 MOVE 'WORLD' TO SUB-ELEMENT-1 MOVE 999 TO SUB-ELEMENT-2.

Distinguish between group items (no PICTURE clause, can contain other items) and elementary items (have PICTURE clauses, contain actual data). Group items provide structure and organization for related data.

Condition Names (88-levels)

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-STORAGE SECTION. 01 STATUS-FIELD PIC X VALUE 'A'. 88 ACTIVE-STATUS VALUE 'A'. 88 INACTIVE-STATUS VALUE 'I'. 88 SUSPENDED-STATUS VALUE 'S'. 01 GRADE-FIELD PIC 9 VALUE 5. 88 EXCELLENT-GRADE VALUE 9 8. 88 GOOD-GRADE VALUE 7 6. 88 FAIR-GRADE VALUE 5 4. 88 POOR-GRADE VALUE 3 2 1. PROCEDURE DIVISION. IF ACTIVE-STATUS DISPLAY 'Customer is active' END-IF IF EXCELLENT-GRADE DISPLAY 'Excellent performance' END-IF.

Use 88-level condition names to create readable boolean conditions over data items. Define multiple values for the same condition and use meaningful names that reflect business logic.

Data Initialization

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
WORKING-STORAGE SECTION. 01 INITIALIZED-DATA. 05 COUNTER PIC 9(4) VALUE 0. 05 FLAG PIC X VALUE 'N'. 05 AMOUNT PIC 9(6)V99 VALUE 1000.50. 05 NAME PIC X(20) VALUE 'DEFAULT NAME'. 05 DATE-FIELD PIC 9(8) VALUE 20231225. 05 ARRAY OCCURS 10 TIMES PIC 9(3) VALUE 100. *> Initialize at runtime PROCEDURE DIVISION. INITIALIZE COUNTER REPLACING NUMERIC DATA BY 0 ALPHABETIC DATA BY SPACES.

Initialize data using VALUE clauses in data definition or INITIALIZE statements in procedure division. Use appropriate initial values for different data types and consider runtime initialization for dynamic values.

Data Definition Best Practices

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-STORAGE SECTION. 01 WELL-DEFINED-RECORD. 05 RECORD-HEADER. 10 RECORD-TYPE PIC X VALUE 'C'. 10 RECORD-SIZE PIC 9(4) COMP. 05 CUSTOMER-DATA. 10 CUSTOMER-ID PIC 9(6) COMP. 10 CUSTOMER-NAME PIC X(30). 10 CUSTOMER-BALANCE PIC 9(9)V99 COMP-3. 05 VALIDATION-FLAGS. 10 ID-VALID PIC X VALUE 'N'. 88 VALID-ID VALUE 'Y'. 10 NAME-VALID PIC X VALUE 'N'. 88 VALID-NAME VALUE 'Y'.

Follow best practices: use meaningful names, group related data together, choose appropriate data types and usage clauses, include validation flags, and document complex data structures with comments.