Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Subprograms
Subprograms | Intermediate | COBOL

COPY Statement

Problem Description

Use COPY to include copybook members.

Expected Output

Code reuse with copybooks

Hints

COPY member-name brings in external code.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. COPYDEMO. * * COPYBOOK: EMPREC.CPY would contain: * 01 EMP-RECORD. * 05 EMP-ID PIC 9(5). * 05 EMP-NAME PIC X(30). * 05 EMP-DEPT PIC X(10). * 05 EMP-SAL PIC 9(7)V99. * DATA DIVISION. WORKING-STORAGE SECTION. * In real program: COPY EMPREC. * For demo, defining inline: 01 EMP-RECORD. 05 EMP-ID PIC 9(5). 05 EMP-NAME PIC X(30). 05 EMP-DEPT PIC X(10). 05 EMP-SAL PIC 9(7)V99. * COPY with REPLACING * COPY EMPREC REPLACING ==EMP-== BY ==WS-EMP-==. 01 WS-EMP-RECORD. 05 WS-EMP-ID PIC 9(5). 05 WS-EMP-NAME PIC X(30). 05 WS-EMP-DEPT PIC X(10). 05 WS-EMP-SAL PIC 9(7)V99. PROCEDURE DIVISION. MOVE 10001 TO EMP-ID. MOVE "JOHN SMITH" TO EMP-NAME. MOVE "IT" TO EMP-DEPT. MOVE 75000.00 TO EMP-SAL. DISPLAY "ID: " EMP-ID. DISPLAY "NAME: " EMP-NAME. DISPLAY "DEPT: " EMP-DEPT. DISPLAY "SAL: " EMP-SAL. STOP RUN.

Explanation:

COPY includes copybook code. REPLACING substitutes text during copy.