COBOL Copy Statement
In the large programming structure, each program can’t able add the large layouts(copybooks) which will increase the length of the program.
So the COPY concept introduced to resolve the above issues.
COPY provides the facility to include the layouts (copybooks) to the program in a very simple manner by using COPY command.
COPY command includes the layout used will be included in the program during the compilation time.
COPY copybook-name [(OF/IN) library name] [REPLACING string-to-be-replaced BY replacing-string]
In the above syntax,
Textname is the elementary group in the Library name. TextName is optional in the above syntax. Library name can be directly mentioned.
TextWord1, TextWord2…TextWordn is the pseudo text which is used in the program.
By default, the copybook library is system library SYSLIB and it can be changed using IN/OF of COPY statement.
If the same copybook is used more than once in the same program, then there will be “duplicate data declaration” error during compilation.
So to avoid this REPLACING option will be helpful to replace the high level qualifier of all the variables.
Ex: COPY CUSTOMER REPLACING ‘EMP-‘ BY ‘EMP1-‘.
If the REPALCING not used, then COPY will include the layout/statements to the program without any changes.
- Copybooks are stored as members in PDS. During the compiling the program, copybooks are included into the program.
- COPY statement can be used in the following paragraphs.
SOURCE-COMPUTER, OBJECT-COMPUTER, SPECIAL-NAMES, FILE-CONTROL, IO-CONTROL, FD SECTION, PARAGRAPHS IN PROCEDURE DIVISION.
- A Copy statement must not existed in another copy statement.
- Master files are used in multiple programs. Their layout can be placed in one copybook and be placed wherever the files are used. All the programs needs the same layout and the same data names. COPY simplifies this solution.
- COPY reduces coding and debugging time.
- Change in layout needs change in copybook is sufficient and all the program using the copybook needs to be recompiled use pickup new layout.
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.
COBOL Continue Statement COBOL Include Statement