Menu
CoddyTech

COPY

Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 1 of 16.

Many programs in a bank read the same customer record. Writing its layout in every program means changing every program when the layout changes. A copybook holds the layout once, in its own file:

01 CUSTOMER-REC.
    05 CUST-ID       PIC 9(5).
    05 CUST-NAME     PIC X(12).
    05 CUST-BALANCE  PIC 9(5)V99.

COPY pastes the copybook's text into the program where the statement stands, before the program is compiled. The program then uses CUST-ID as if it had declared it:

WORKING-STORAGE SECTION.
    COPY CUSTREC.

The name after COPY is the copybook's name. Here it is the file CUSTREC.cpy; on a mainframe it is a member of a copy library. A copybook is written in the same columns as the program: 01 in Area A, 05 in Area B.

A copybook is never compiled or run on its own: it is only text that COPY brings in. If the copybook is missing, the compiler stops with an error.

challenge icon

Challenge

Easy

The copybook CUSTREC.cpy holds the customer record. Bring it into the WORKING-STORAGE SECTION so the program prints 10001 Ada Lovelace  1,250.50

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CUSTSHOW.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
           *> Copy the customer record here
       01 SHOW-BAL     PIC ZZ,ZZ9.99.
       PROCEDURE DIVISION.
           MOVE 10001 TO CUST-ID
           MOVE "Ada Lovelace" TO CUST-NAME
           MOVE 1250.50 TO CUST-BALANCE
           MOVE CUST-BALANCE TO SHOW-BAL
           DISPLAY CUST-ID " " CUST-NAME " " SHOW-BAL
           STOP RUN.

All lessons in Programs That Work Together