Open In App

COBOL – Copy Statement

Last Updated : 10 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

COPY statement is used to bring into a program a series of prewritten COBOL entries that have been stored in a library. A copy statement inserts the copybook inside a COBOL program, copybook refers to a member which holds all the variables declared inside it. The COPY statement is a library statement that places the prewritten text in a COBOL compilation unit. 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.

Syntax:

COPY copybook-name
   [REPLACING "string_1" BY "string_2"]

Where,

  1. The copybook-name refers to a file containing the source string.
  2. The string_1 refers to the string to be replaced.
  3. The string_2 refers to the replacing string.

Advantages of using COPY:

Following are the advantages of using the COPY statement in COBOL:

  • A single copybook could be used inside many programs, no need to write variable declarations again.
  • A copybook could be used with the dataset to view the data in label form as defined inside the copybook.
  • Reduce time, effort, and cost during changes because when the changes are made in a single copybook it will reflect in other places as well where the copybook is used with the help of the COPY statement. 
  • Library entities are extensively annotated so that they are meaningful to all users; this annotation results in better-documented programs and systems.
  • With the COPY statement, you may include prewritten ENVIRONMENT, DATA, or PROCEDURE division entries

Let us take the example of Copy in COBOL.

Example:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DEMO3.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
     
    COPY COPYBOOK.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
     
    ACCEPT ST_ID
    ACCEPT FIRST_NAME
    ACCEPT LAST_NAME
    DISPLAY STUDENT.
    STOP RUN.


The above code is using the below copybook with the help of keyword COPY inside the Working storage section when the COBOL is compiled the below code is inserted in the place of COPY COPYBOOK.

Cobol




01 STUDENT.
 20 ST_ID         PIC 9(5).
 20 FIRST_NAME    PIC X(20).
 20 LAST_NAME     PIC X(20).


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads