MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL USAGE Clause Quick Reference

The USAGE clause in COBOL specifies how data is stored internally in computer memory. It determines the storage format and representation of data items, affecting storage efficiency, arithmetic performance, and data handling. Understanding USAGE is essential for optimizing COBOL programs, especially those performing extensive calculations or working with large amounts of numeric data.

USAGE Clause Syntax

The USAGE clause is specified in the data definition:

cobol
1
2
3
4
level-number data-name [PICTURE IS] picture-string [USAGE IS] usage-type [VALUE IS] value

Common USAGE types:

  • DISPLAY - Character format (default)
  • BINARY or COMPUTATIONAL - Binary format
  • COMPUTATIONAL-3 or PACKED-DECIMAL - Packed decimal format
  • INDEX - For table indexing

DISPLAY USAGE

DISPLAY is the default USAGE. Data is stored in character format (ASCII or EBCDIC), with each character occupying one byte.

Characteristics

  • Human-readable in memory/storage
  • One byte per character/digit
  • Default if USAGE not specified
  • Ideal for input/output operations
  • Less efficient for arithmetic

Example

cobol
1
2
3
4
5
6
7
WORKING-STORAGE SECTION. 01 CUSTOMER-ID PIC 9(5) USAGE IS DISPLAY. 01 CUSTOMER-NAME PIC X(30) USAGE IS DISPLAY. 01 AMOUNT PIC 9(8)V99 USAGE IS DISPLAY. *> DISPLAY is default, so these are equivalent: 01 AMOUNT-2 PIC 9(8)V99. *> Also DISPLAY

BINARY / COMPUTATIONAL USAGE

BINARY (or COMPUTATIONAL, COMP) stores numeric data in binary format for efficient arithmetic operations.

Characteristics

  • Efficient for arithmetic operations
  • Storage size depends on PICTURE: typically 2, 4, or 8 bytes
  • Not human-readable in memory
  • Faster calculations than DISPLAY
  • More storage-efficient for large numbers

Storage Sizes

BINARY USAGE Storage Sizes
PICTURE RangeTypical BytesValue Range
PIC S9(1) to S9(4)2 bytes-32,768 to 32,767
PIC S9(5) to S9(9)4 bytes-2,147,483,648 to 2,147,483,647
PIC S9(10) to S9(18)8 bytesVery large range

Example

cobol
1
2
3
4
5
6
7
WORKING-STORAGE SECTION. 01 COUNTER PIC S9(9) USAGE IS BINARY. 01 TOTAL-AMOUNT PIC S9(9)V99 USAGE IS COMPUTATIONAL. 01 QUANTITY PIC S9(5) USAGE IS COMP. *> COMP, COMPUTATIONAL, and BINARY are equivalent 01 AMOUNT-COMP PIC S9(7) USAGE IS COMP.

PACKED-DECIMAL / COMPUTATIONAL-3

PACKED-DECIMAL (COMP-3) stores two decimal digits per byte, with the sign in the rightmost half-byte.

Characteristics

  • Very storage-efficient for decimal numbers
  • Two digits per byte (plus sign)
  • Maintains decimal precision
  • Commonly used in financial applications
  • Not human-readable

Storage Calculation

Storage bytes = (number of digits + 1) / 2, rounded up. The "+1" accounts for the sign.

Examples:

  • PIC 9(5) → (5+1)/2 = 3 bytes
  • PIC 9(10) → (10+1)/2 = 6 bytes
  • PIC S9(7)V99 → (7+2+1)/2 = 5 bytes

Example

cobol
1
2
3
4
5
6
WORKING-STORAGE SECTION. 01 ACCOUNT-BALANCE PIC S9(9)V99 USAGE IS PACKED-DECIMAL. 01 PRICE PIC S9(7)V99 USAGE IS COMPUTATIONAL-3. 01 AMOUNT PIC S9(5) USAGE IS COMP-3. *> COMP-3, COMPUTATIONAL-3, and PACKED-DECIMAL are equivalent

INDEX USAGE

INDEX is used for table indexing, storing positions or offsets for efficient table access.

Characteristics

  • Used for table element references
  • Cannot be used in arithmetic
  • Manipulated with SET statements
  • More efficient than numeric subscripts
  • Cannot be displayed directly

Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
WORKING-STORAGE SECTION. 01 TABLE-INDEX USAGE IS INDEX. 01 CUSTOMER-TABLE. 05 CUSTOMER-ENTRY OCCURS 100 TIMES INDEXED BY TABLE-INDEX. 10 CUSTOMER-ID PIC 9(5). 10 CUSTOMER-NAME PIC X(30). PROCEDURE DIVISION. SET TABLE-INDEX TO 1 SET TABLE-INDEX UP BY 1 *> Cannot do: ADD 1 TO TABLE-INDEX

USAGE Comparison Table

USAGE Types Comparison
USAGE TypeStorage EfficiencyArithmetic PerformanceHuman ReadableBest For
DISPLAYLess efficient (1 byte/digit)Slower (requires conversion)YesInput/output, text data
BINARYEfficient (2-8 bytes)FastestNoCalculations, counters
PACKED-DECIMALVery efficient (2 digits/byte)FastNoFinancial data, decimals
INDEXEfficientN/A (not used)NoTable indexing

Choosing the Right USAGE

Guidelines for selecting USAGE:

  • Use DISPLAY for: Input/output data, text fields, data that needs to be readable, default choice
  • Use BINARY for: Counters, accumulators, fields used extensively in calculations, performance-critical numeric operations
  • Use PACKED-DECIMAL for: Financial amounts, decimal numbers requiring precision, storage-constrained environments
  • Use INDEX for: Table subscripts, efficient table access, when using SET statements for indexing

USAGE in Calculations

When mixing USAGE types in calculations, COBOL performs automatic conversion:

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-STORAGE SECTION. 01 AMOUNT-DISPLAY PIC 9(5) USAGE IS DISPLAY VALUE 10000. 01 AMOUNT-BINARY PIC S9(5) USAGE IS BINARY VALUE 5000. 01 RESULT PIC S9(6) USAGE IS BINARY. PROCEDURE DIVISION. *> COBOL converts DISPLAY to binary for calculation COMPUTE RESULT = AMOUNT-DISPLAY + AMOUNT-BINARY *> Result is stored in BINARY format DISPLAY "Result: " RESULT STOP RUN.

Note: Mixing USAGE types adds conversion overhead. For best performance, use consistent USAGE types (preferably BINARY) for fields in the same calculations.

Best Practices

  • Use BINARY for calculation fields: Fields used in arithmetic should typically be BINARY for performance
  • Use DISPLAY for I/O fields: Fields used for input/output should be DISPLAY for compatibility
  • Use PACKED-DECIMAL for financial data: Financial amounts benefit from PACKED-DECIMAL's precision and efficiency
  • Be consistent: Use consistent USAGE types for related fields to avoid conversion overhead
  • Document USAGE choices: Document why specific USAGE types were chosen for maintainability

Common Examples

Example 1: Mixed USAGE Types

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
WORKING-STORAGE SECTION. 01 INPUT-AMOUNT PIC 9(8)V99 USAGE IS DISPLAY. 01 CALC-AMOUNT PIC S9(8)V99 USAGE IS BINARY. 01 FINAL-BALANCE PIC S9(9)V99 USAGE IS PACKED-DECIMAL. 01 COUNTER PIC S9(5) USAGE IS BINARY VALUE ZERO. PROCEDURE DIVISION. MOVE 1000.50 TO INPUT-AMOUNT MOVE INPUT-AMOUNT TO CALC-AMOUNT COMPUTE CALC-AMOUNT = CALC-AMOUNT * 1.1 MOVE CALC-AMOUNT TO FINAL-BALANCE ADD 1 TO COUNTER STOP RUN.

Example 2: Financial Application

cobol
1
2
3
4
5
6
7
8
9
10
11
12
WORKING-STORAGE SECTION. 01 ACCOUNT-BALANCE PIC S9(9)V99 USAGE IS PACKED-DECIMAL. 01 TRANSACTION-AMOUNT PIC S9(7)V99 USAGE IS PACKED-DECIMAL. 01 NEW-BALANCE PIC S9(9)V99 USAGE IS PACKED-DECIMAL. 01 RECORD-COUNT PIC S9(5) USAGE IS BINARY VALUE ZERO. PROCEDURE DIVISION. MOVE 10000.00 TO ACCOUNT-BALANCE MOVE 250.50 TO TRANSACTION-AMOUNT COMPUTE NEW-BALANCE = ACCOUNT-BALANCE + TRANSACTION-AMOUNT ADD 1 TO RECORD-COUNT STOP RUN.

Explain Like I'm 5: USAGE Clause

Think of USAGE like different ways to store your toys:

  • DISPLAY is like putting toys in labeled boxes you can read - easy to see what's inside, but takes more space
  • BINARY is like a compact storage system - takes less space and is faster to work with, but you can't read the labels
  • PACKED-DECIMAL is like a special compact box for numbers - very space-efficient and keeps numbers precise
  • INDEX is like a map pointer - it tells you where things are but isn't the thing itself

Just like you choose different storage methods for different toys, COBOL uses different USAGE types for different kinds of data!

Test Your Knowledge

1. What is the default USAGE in COBOL if not specified?

  • BINARY
  • DISPLAY
  • PACKED-DECIMAL
  • INDEX

2. Which USAGE type is most efficient for arithmetic operations?

  • DISPLAY
  • BINARY
  • PACKED-DECIMAL
  • They are all equally efficient

3. What is PACKED-DECIMAL USAGE used for?

  • Storing text data
  • Storing decimal numbers efficiently with precision
  • Storing binary data
  • Storing table indexes

4. Which USAGE type should you use for data that needs to be human-readable?

  • BINARY
  • DISPLAY
  • PACKED-DECIMAL
  • INDEX

5. What is INDEX USAGE used for?

  • Storing numeric values
  • Referencing table elements efficiently
  • Storing text data
  • Performing calculations

6. Can you perform arithmetic with INDEX USAGE items?

  • Yes, directly
  • No, INDEX items cannot be used in arithmetic
  • Only addition and subtraction
  • Only with conversion

Related Concepts

Related Pages