Can we use same copy book for different files in COBOL?

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Ashalata M
New Member
Posts: 5
Joined: Mon Nov 24, 2014 3:59 pm

Can we use same copy book for different files in COBOL?

Post by Ashalata M »

Hi,

I have a copybook which I want to use for two input files and one output file.I have defined the copy book in FD section of each file, like this:

Input file 1:

Code: Select all

FD File1
Copy copybook 
Input file 2:

Code: Select all

FD File2
Copy copybook


Output file:

Code: Select all

FD File3 
Copy copybook


I have rad that we can use it this way using BY REPLACING clause but will be possible to use same copybook name with out using replacing option? As in copybook 01 level item name is defined as VA-RECORD and 05 cariables are starting with X1-. If replacing option is used then we can replace X1- for input-file1,input-file2 and output. But 01 level item name is not starting with X1-. Please suggest, will replace option works for above situation.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Can we use same copy book for different files in COBOL?

Post by Robert Sample »

The same copy book can be used for multiple files AS LONG AS you do not include the 01-level variable in the copy book. If you have a unique 01-level name for each variable and the copy statement under that, you do not need to change the names of the variables -- you can use qualification (VAR-A OF LEVEL-01-VARIABLE) to uniquely identify the variable.

If you want to include the 01-level variable in the copy book, each COPY will need a REPLACING option - period. Every 01-level variable in a program must be unique to allow every variable name to be uniquely identified (through qualification if nothing else). If you do go this route, read carefully about REPLACING in the Enterprise COBOL Language Reference and Language Guide manuals as the rules for doing the replace are not obvious nor simple.
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: Can we use same copy book for different files in COBOL?

Post by William Collins »

You can qualify by the FD if the duplicate 01s are both/all in the FILE SECTION. If one ore more are elsewhere in the DATA DIVISION, then you have a problem as Robert has suggested.

Qualification is horrible anyway. Use the COPY with REPLACING. You can "nest" COPY statements, with or without REPLACING.

It is much more flexible to not have 01-levels in a copybook.
Ashalata M
New Member
Posts: 5
Joined: Mon Nov 24, 2014 3:59 pm

Re: Can we use same copy book for different files in COBOL?

Post by Ashalata M »

Thanks. So with the current copybook structure using it for multiple files is not advisable? As it has got 01 level in it.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Can we use same copy book for different files in COBOL?

Post by Robert Sample »

Have you tried compiling the program with the multiple COPY statements in it? What results did you get from the compile?
Ashalata M
New Member
Posts: 5
Joined: Mon Nov 24, 2014 3:59 pm

Re: Can we use same copy book for different files in COBOL?

Post by Ashalata M »

I'm gettign the below error;

Code: Select all

"SALES-AMOUNT" WAS NOT A UNIQUELY DEFINED NAME. THE DEFINITION TO BE USED CONTEXT. THE REFERENCE TO THE NAME WAS DISCARDED.
which is expected as you all said. I do not know if without changing the copybook I can do somthing.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Can we use same copy book for different files in COBOL?

Post by Robert Sample »

AS YOU HAVE BEEN TOLD, if the copy book is in the FD of two (or more) files, you can qualify by using the OF <file name> to uniquely identify the variable -- if the copy book is not in an FD then you CANNOT resolve this issue without changing the copy book -- PERIOD.
Ashalata M
New Member
Posts: 5
Joined: Mon Nov 24, 2014 3:59 pm

Re: Can we use same copy book for different files in COBOL?

Post by Ashalata M »

Thanks Robert. I am in the process of getting the approval to change the copybook.

thanks
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”