TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - COPY Statement


The COPY statement is a library statement that places prewritten text in a COBOL compilation unit.

In otherwords, COPY provides the facility to include the layouts (copybooks) to the program in a very simple manner by using COPY command.

Prewritten source code entries can be included in a compilation unit at compile time. Thus, an installation can use standard file descriptions, record descriptions, or procedures without recoding them. These entries and procedures can then be saved in user-created libraries; they can then be included in programs and class definitions by means of the COPY statement.

Compilation of the source code containing COPY statements is logically equivalent to processing all COPY statements before processing the resulting source text.

Syntax:

COPY copybook-name
   [REPLACING string-to-be-replaced BY replacing-string]

The effect of processing a COPY statement is that the library text associated with text-name is copied into the compilation unit, logically replacing the entire COPY statement, beginning with the word COPY and ending with the period, inclusive. When the REPLACING phrase is not specified, the library text is copied unchanged.


Example 1

In this example, the library text PAYLIB consists of the following DATA DIVISION entries:

01  A.
  02  B    PIC S99.
  02  C    PIC S9(5)V99.
  02  D    PIC S9999 OCCURS 1 TO 52 TIMES
      DEPENDING ON B OF A.

You can use the COPY statement in the DATA DIVISION of a program as follows:

COPY PAYLIB.

In this program, the library text is copied. The resulting text is treated as if it were written as follows:

01  A.
  02  B    PIC S99.
  02  C    PIC S9(5)V99.
  02  D    PIC S9999 OCCURS 1 TO 52 TIMES
      DEPENDING ON B OF A.

Example 2

To change some or all of the names within the library text, you can use the REPLACING phrase:

COPY PAYLIB REPLACING A BY PAYROLL
                      B BY PAY-CODE
                      C BY GROSS-PAY
                      D BY HOURS.

In this program, the library text is copied. The resulting text is treated as if it were written as follows:

01  PAYROLL.
  02  PAY-CODE    PIC S99.
  02  GROSS-PAY   PIC S9(5)V99.
  02  HOURS       PIC S9999 OCCURS 1 TO 52 TIMES
      DEPENDING ON PAY-CODE OF PAYROLL.

The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.


Example 3

If the following conventions are followed in the library text, parts of names (for example, the prefix portion of data names) can be changed with the REPLACING phrase.

In this example, the library text PAYLIB consists of the following DATA DIVISION entries.

01  :TAG:.
  02  :TAG:-WEEK          PIC S99.
  02  :TAG:-GROSS-PAY     PIC S9(5)V99.
  02  :TAG:-HOURS         PIC S999  OCCURS 1 TO 52 TIMES
      DEPENDING ON :TAG:-WEEK OF :TAG:.

You can use the COPY statement in the DATA DIVISION of a program as follows.

COPY PAYLIB REPLACING ==:TAG:== BY ==Payroll==.

Usage Note: In this example, the required use of colons or parentheses as delimiters in the library text. Colons are recommended for clarity because parentheses can be used for a subscript, for instance in referencing a table element.

In this program, the library text is copied. The resulting text is treated as if it were written as follows.

01  PAYROLL.
  02  PAYROLL-WEEK        PIC S99.
  02  PAYROLL-GROSS-PAY   PIC S9(5)V99.
  02  PAYROLL-HOURS       PIC S999  OCCURS 1 TO 52 TIMES
      DEPENDING ON PAYROLL-WEEK OF PAYROLL.

The changes that are shown are made only for this program. The text remains unchanged as it appears in the library.


Example 4:

This example shows how to selectively replace level numbers without replacing the numbers in the PICTURE clause.

COPY xxx REPLACING ==(01)== BY ==(01)==
                   == 01 == BY == 05 ==.

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal