COPY Statement


  • COPY statement includes predefined copybooks (usually file record structures) from the library that is outside of the program.
  • It copies the copybook declarations into the program during the compilation and replaces the COPY statement with its content.
  • The copybook is a PDS member and contains only variable declarations.
  • SYSLIB | COPYLIB DD statement in the compile JCL is used to map the copybook library to insert the copybook into the program.

Syntax -

COPY copybook-name
[[REPLACING "source-string" BY "target-string"]...]
Note! All statements coded in [ ] are optional.

Parameters -

  • copybook-name - Refers to copybook name (1-8 characters) to be copied.
  • REPLACING phrase - Replace the source-string with the target string. It is optional.
  • Source-string, Target-String - Specifies source and target strings.
Note! Lines containing EJECT, SKIP1, SKIP2, or SKIP3 statements are treated as comments during COPY statement processing.

Advantages -

COPY statement is useful when -

  • The file structure is too big to include directly in the program.
  • We can easily manage changes by saving the file structures separately and using them in multiple programs. We can update the file structure quickly, and a simple program recompilation is sufficient to incorporate the latest copybook updates.
  • One copybook can create multiple file structures using REPALCING phrases.

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).