COPY Statement
The COPY statement is used to include predefined copybooks (usually file record structures) from an external source into the current COBOL program.
The COPY statement copies the copybook declarations into the module during the compilation time and logically replaces the COPY statement with its content.
The copybook is a PDS member and contains only variable declarations in it. COPYLIB DD statement is used to map the copybook library in JCL to insert the copybook into the program during compilation.
Syntax -
COPY copybook-name
[[REPLACING "source-string" BY "target-string"]...]
Parameters -
- copybook-name - Refers to the name (1-8 characters) of the copybook to be copied.
- REPLACING phrase - It is used to replace the source-string with the target-string. It is optional.
- Source-string, Target-String - Specifies source and target strings. A delimiter (==) is needed when two or more character strings (separated by a comma).
Advantages -
- Using a COPY statement is helpful when the file structure is too big to include directly in the program.
- The COPY statement is useful when saving the file structures centrally and is used in multiple programs. So that file structure modifications are easy, and recompilation of programs is sufficient to pick up the latest copybook updates.
- COPY statement reduces the coding and debugging time.
Examples -
Scenario1 - Insert a copybook into the program.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
COPY EMPREC.
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
* COPY EMPREC.
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Scenario2 - Insert a copybook into the program by replacing EMP by INP.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
COPY EMPREC REPLACING "EMP" BY "INP".
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
* COPY EMPREC REPLACING "EMP" BY "INP".
05 INP-NUM PIC 9(05).
05 INP-NAME PIC X(10).
05 INP-DESG PIC X(15).
05 INP-SALARY PIC 9(10).
Scenario3 - Insert a copybook into the program by replacing EMP with delimiter by INPUT.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----
01 :EMP:-REC.
05 :EMP:-NUM PIC 9(05).
05 :EMP:-NAME PIC X(10).
05 :EMP:-DESG PIC X(15).
05 :EMP:-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----+
COPY EMPREC REPLACING "==:EMP:==" BY "==INPUT==".
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----+
* COPY EMPREC REPLACING "==:EMP:==" BY "==INPUT==".
01 INPUT-REC.
05 INPUT-NUM PIC 9(05).
05 INPUT-NAME PIC X(10).
05 INPUT-DESG PIC X(15).
05 INPUT-SALARY PIC 9(10).
Scenario4 - Insert a copybook into the program with 2 replacements.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----+
01 EMP-REC.
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----+
COPY EMPREC REPLACING ==01 EMP-REC== BY ==01 CUST-INP==
==05 EMP== BY ==03 CUST-INP==.
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----+
* COPY EMPREC REPLACING ==01 EMP-REC== BY ==01 CUST-INP==
* ==05 EMP== BY ==03 CUST-INP==.
01 CUST-INP.
05 CUST-INP-NUM PIC 9(05).
05 CUST-INP-NAME PIC X(10).
05 CUST-INP-DESG PIC X(15).
05 CUST-INP-SALARY PIC 9(10).