Microsoft KB Archive/59288

Example and Explanation of the COBOL COPY Statement

PSS ID Number: Q59288 Article last modified on 03-09-1990

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The COPY statement incorporates additional source code (such as record descriptions or parts of the PROCEDURE DIVISION) into a COBOL source program at compile time from a separate file that is external to the program source file. For more information about the COPY statement, see Chapter 8 of the “Microsoft COBOL Compiler 3.0: Language Reference Manual.” This information applies to Microsoft COBOL Compiler Versions 3.00 and 3.00a for MS-DOS and MS OS/2.

More Information: (The COBOL COPY statement is similar to the $INCLUDE metacommand found in QuickBASIC and Microsoft BASIC Compilers.) The effect of the COPY statement is to insert text into the source program at compile time, where it will be treated by the COBOL system as part of the source program. The COPY statement may appear in the ENVIRONMENT DIVISION, DATA DIVISION, and PROCEDURE DIVISION. It can also be used in the CONFIGURATION SECTION and INPUT-OUTPUT SECTION as illustrated below:

Code Example
ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. COPY statement. OBJECT-COMPUTER. COPY statement. SPECIAL-NAMES. COPY statement. INPUT-OUTPUT SECTION. FILE-CONTROL. COPY statement. SELECT file-name COPY statement. DATA DIVISION. FILE SECTION. FD  file-name COPY statement. 01  record-name COPY statement. SD  sort-file-name COPY statement. 01  sort-record-name COPY statement. WORKING STORAGE SECTION. 01  record-name COPY statement. 01  record-name-1 REDEFINES record-name-2 COPY statement. REPORT SECTION. RD  report-name COPY statement. 01  [record-name] COPY statement. LINKAGE SECTION. 01  record-name COPY statement. 01  record-name-1 REDEFINES record-name-2 COPY statement. PROCEDURE DIVISION. section-name SECTION. COPY statement. paragraph-name. COPY statement.

Illustration of How COPY Works
In its simplest form, the COPY statement can appear as follows: COPY file-name The complete contents of the “file-name” source file are inserted at compile time at the location in the source file where the COPY statement appears. To illustrate this point, assume you have developed a filename called COMMON-SALES-RECORD (a sale record), which contains the following: 01 DUMMY-RECORD. 05 ITEM-PURCHASED PIC X(06). 05 QUANTITY-PURCHASED PIC 9(06). 05 DATE-PURCHASED. 10 DP-MONTH PIC X(02). 10 DP-DAY PIC X(02). 10 DP-YEAR PIC X(02). This file would be available to any program that contained the following statement: COPY COMMON-SALES-RECORD. Thus, if your source code appears as follows DATA DIVISION. FILE SECTION. FD SALES-FILE LABEL RECORDS ARE STANDARD. 01 SALES-RECORD COPY COMMON-SALES-RECORD. then as the program is being compiled, the COMMON-SALES-RECORD file is inserted in the program at the point of the COPY statement. This results in the following code being fed into the COBOL compiler (as shown in the compiler output listing file): DATA DIVISION. FILE SECTION. FD SALES-FILE LABEL RECORDS ARE STANDARD. 01 SALES-RECORD. 05 ITEM-PURCHASED PIC X(06). 05 QUANTITY-PURCHASED PIC 9(06). 05 DATE-PURCHASED. 10 DP-MONTH PIC X(02). 10 DP-DAY PIC X(02). 10 DP-YEAR PIC X(02). Note that in this example, the record-name (SALES-RECORD) is retained from the current program, and the record-name (DUMMY-RECORD) from the copied file is eliminated. The use of the COPY statement is recommended where programs must have the same information and you want to avoid unnecessarily retyping the same code.

Copyright Microsoft Corporation 1990.