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.
The USAGE clause is specified in the data definition:
1234level-number data-name [PICTURE IS] picture-string [USAGE IS] usage-type [VALUE IS] value
Common USAGE types:
DISPLAY is the default USAGE. Data is stored in character format (ASCII or EBCDIC), with each character occupying one byte.
1234567WORKING-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 (or COMPUTATIONAL, COMP) stores numeric data in binary format for efficient arithmetic operations.
| PICTURE Range | Typical Bytes | Value 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 bytes | Very large range |
1234567WORKING-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 (COMP-3) stores two decimal digits per byte, with the sign in the rightmost half-byte.
Storage bytes = (number of digits + 1) / 2, rounded up. The "+1" accounts for the sign.
Examples:
123456WORKING-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 is used for table indexing, storing positions or offsets for efficient table access.
123456789101112WORKING-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 Type | Storage Efficiency | Arithmetic Performance | Human Readable | Best For |
|---|---|---|---|---|
| DISPLAY | Less efficient (1 byte/digit) | Slower (requires conversion) | Yes | Input/output, text data |
| BINARY | Efficient (2-8 bytes) | Fastest | No | Calculations, counters |
| PACKED-DECIMAL | Very efficient (2 digits/byte) | Fast | No | Financial data, decimals |
| INDEX | Efficient | N/A (not used) | No | Table indexing |
Guidelines for selecting USAGE:
When mixing USAGE types in calculations, COBOL performs automatic conversion:
1234567891011WORKING-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.
12345678910111213WORKING-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.
123456789101112WORKING-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.
Think of USAGE like different ways to store your toys:
Just like you choose different storage methods for different toys, COBOL uses different USAGE types for different kinds of data!
1. What is the default USAGE in COBOL if not specified?
2. Which USAGE type is most efficient for arithmetic operations?
3. What is PACKED-DECIMAL USAGE used for?
4. Which USAGE type should you use for data that needs to be human-readable?
5. What is INDEX USAGE used for?
6. Can you perform arithmetic with INDEX USAGE items?