Search code examples
cobolmainframe

Create a table in Cobol from a copybook?


How do I create a table from a copybook in Cobol?

The copybook has several groups in it and only need one of them to be in a table.

Do I need to use the copybook multiple times replacing the start of variables so that the compiler views them as different things?

I can edit the copybook as long as the other programs using it will compile.

It is on an IBM Mainframe.

See my answer below for my final solution. Thanks to @Bruce Martin


Solution

  • The IBM's mainframe supports nested copybooks, so you can change your copybook to a nested copybook

    Also have a look at

    so

    01  record-layout-1.
    ...
    01  record-layout-2.
    ... 
    01  record-layout-3.
    ... 
    

    can be changed to

    01  record-layout-1.
    ...
    01  record-layout-2.
    ... 
    01  record-layout-3.
    copy newCopy.
    ... 
    

    and in your program you can use the newCopy or what ever you call it. You will probably want to renumber copybook levels while you are at it.

    So if the original copybook is

    01  record-layout-3.
        05 field-1          pic x(4).
        05 field-2 ... 
    

    you would create the new copybook as

        25 field-1          pic x(4).
        25 field-2 ... 
    

    The actual level numbers are not important, they just need to be > 01 for the copybook. Using 25 will make it easy to embed in your working storage.


    A few companies make it a standard- copybooks must not contain 01 levels so that copybooks can also be embedded in Working storage. This is rare though