The COPY statement is a library statement that places prewritten text in a COBOL compilation unit. Prewritten text can be included in a compilation unit at compile time.

Prewritten text can be a standard file description, record descriptions or procedures.These can be saved in user-created libraries and can be included in programs by COPY statement without recoding them.

The compiling of a COPY statement copies the library text associated with text-name into the compilation unit that logically replaces 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.

Syntax -

COPY Syntax

text-name, library-name -

text-name specifies the copy text existing member name in library. library-name specifies the library where the copy member exists.

  • Both names can be from 1-30 characters in length.
  • Both names can contain Latin uppercase letters A-Z, Latin lowercase letters a-z, digits 0-9 and hyphen.

text-name and library-name can be the same. text-name need not be qualified. If text-name is not qualified, a library-name of SYSLIB is assumed.

literal-1, literal-2 -

Must be alphanumeric literals. literal-1 identifies the copy text. literal-2 identifies where the copy text exists. The literal can be from 1-160 characters in length.

operand-1, operand-2 -

Can be either pseudo-text, an identifier, a function-identifier, a literal or a COBOL word (except the word COPY). Library text and pseudo-text can consist of any words (except COPY), identifiers, or literals that written in the source text.

SUPPRESS phrase -

Specifies the library text is not to be printed on the source listing.

REPLACING phrase -

Each operand can consist of -

  • Pseudo-text
  • An identifier
  • A literal
  • A COBOL word (except the word COPY)

When the REPLACING phrase is specified, the library text is copied and each properly matched occurrence of operand-1 is replaced by the associated operand-2.

pseudo-text -

A sequence of character-strings or separators or both but not including pseudo-text delimiters (==). Both characters of each pseudo-text delimiter (==) must appear on one line; however, character-strings within pseudo-text can be continued.

Individual character-strings within pseudo-text can be up to 322 characters long. However, the character-string must be delimited by separators.

pseudo-text-1 refers to pseudo-text used for operand-1 and pseudo-text-2 refers to pseudo-text used for operand-2. Pseudo-text must not contain the word COPY.

Pseudo-text can consist of or include any words (except COPY), identifiers or literals that can written in the source text.

identifier -

Can be defined in any section of the data division.

literal -

Can be numeric or alphanumeric.

word -

Can be any single COBOL word (except COPY). Each COPY statement must be preceded by a space and ended with a separator period. A COPY statement can appear in the source text anywhere a character string or a separator can appear. COPY statements can be nested. However, nested COPY statements cannot contain the REPLACING phrase.

A COPY member can be named only once in a set of nested COPY statements until the end-of-file for that COPY member is reached. Debugging lines are allowed within library text and pseudo-text.

If the word COPY appears in a comment-entry or in the place where a comment-entry appears, it is considered part of the comment-entry. After all COPY and REPLACE statements have been processed, a debugging line considered as a comment line, if WITH DEBUGGING MODE clause is not specified in the SOURCE-COMPUTER paragraph.

Comment lines or blank lines can code in library text. However, those are not copied if that comment line or blank line appears within the sequence of text words.

Lines containing EJECT, SKIP1, SKIP2, SKIP3 or TITLE statements can occur in library text. Such lines are treated as comment lines during COPY statement processing. The syntactic correctness of the entire COBOL source text cannot be determined until all COPY and REPLACE statements have been completely processed.

Uses -

  • Master files are required to use in multiple programs and their layout is too big to include directly in the program. COPY statement resolves the issue.
  • If any file record layout coded in the multiple programs and required to add one field to the file record layout, all programs that had the record layout needs to be modified. If COPY statement used, modify the copybook and recompilation is enough to resolve the issue.
  • COPY reduces coding and debugging time.

Practicle Example:

A copybook declared like below.

Copybook name: STDRECD
    02  STD-NUM                PIC 9(3).
    02  STD-NAME               PIC X(60).
    02  STD-PERCENT            PIC 9(03).

A sample program which uses the above copy book.

Practical Example - COPY:

IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPCOPY.
AUTHOR.  MTH.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  STD-REC.
COPY STDRECD.

PROCEDURE DIVISION.
   ACCEPT STD-NUM.
   ACCEPT STD-NAME.
   ACCEPT STD-PERCENT.
   DISPLAY β€˜STUDENT DETAILS : β€˜ STD-REC.
   STOP RUN.

After the Compilation process, the program will be like below.

IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPCOPY.
AUTHOR.  MTH.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  STD-REC.
  *COPY STDRECD.
    02  STD-NUM                PIC 9(3).
    02  STD-NAME               PIC X(60).
    02  STD-PERCENT            PIC 9(03).

PROCEDURE DIVISION.
   ACCEPT STD-NUM.
   ACCEPT STD-NAME.
   ACCEPT STD-PERCENT.
   DISPLAY β€˜STUDENT DETAILS : β€˜ STD-REC.
   STOP RUN.