Coding Examples

Sequences of code (such as file and data descriptions, error and exception routines, etc.) that are common to a number of programs can be saved in a library, and then used in conjunction with the COPY statement. If naming conventions are established for such common code, then the REPLACING phrase need not be specified. If the names will change from one program to another, then the REPLACING phrase can be used to supply meaningful names for this program.

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.
The programmer can use the COPY statement in the Data Division of a program as follows:
    COPY PAYLIB.

The library text will be copied unchanged into the COBOL program, immediately after the COPY statement.

Example 2: To change some (or all) of the data names within the library text used in Example 1, the programmer can use the REPLACING phrase:
  COPY PAYLIB REPLACING  A BY PAYROLL
                         B BY PAY-CODE
                         C BY GROSS-PAY
                         D BY HOURS.
When the library text is copied, the resulting text appears as if it had been 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 shown are made only for this program. The text, as it appears in the library, remains unchanged.

Example 3: This example illustrates how part of a data-name can be replaced if certain conventions are followed when creating the library text. In this case, the library text CONTACT contains the following code:
01 (PRFX)-RECORD.
 03 (PRFX)-NAME PIC X(24).
 03 (PRFX)-PHONE PIC X(10).
 03 (PRFX)-EXTN PIC X(4).
The programmer can copy this library text, replacing the text word PRFX and its bounding parentheses by a prefix for the data-names. For example, the following COPY statement can be written in the Data Division of a program:
    COPY CONTACT REPLACING ==(PRFX)== BY ==CUST==.
When the library text is copied, the resulting text appears as if it had been written as follows:
01 CUST-RECORD.
 03 CUST-NAME PIC X(24).
 03 CUST-PHONE PIC X(10).
 03 CUST-EXTN PIC X(4).
Note: Because many of the separators have special significance when processing a COPY statement, the values that can be used for delimiting part of a text word in this way are limited to the parenthesis and colon symbols. In addition, it will be necessary to ignore certain errors flagged by the SEU Syntax Checker when entering the library text.