What Is a Copybook?
A copybook is a standalone member of source code, most often a set of DATA DIVISION field definitions, that is stored in a library and pulled into a COBOL program at compile time using the COPY statement, so that many programs can share one authoritative definition instead of retyping the same record layout repeatedly. A typical copybook might define a customer record with fields like CUST-ID, CUST-NAME, and CUST-BALANCE, and any program that needs to read or write that record simply writes COPY CUSTREC to bring in that exact layout. Because the copybook is expanded inline by the compiler's preprocessor before syntax checking, the resulting compiled program behaves as if every field had been typed directly into the source, giving zero runtime overhead compared to hand-copied code.
Cricket analogy: A copybook is like the ICC's official pitch-and-boundary specification that every stadium references instead of each ground inventing its own rules, so a Lord's Test and an MCG Test both build their playing conditions from the same shared standard.
Using the COPY Statement
The basic form, COPY CUSTREC, tells the compiler to locate a member named CUSTREC in the library concatenation specified for the compile job, such as an MVS partitioned data set or a library path in a distributed COBOL environment, and insert its text at that point in the source before compilation proceeds. Copybooks can be placed in the DATA DIVISION for record layouts, but they are equally valid in the PROCEDURE DIVISION for shared paragraphs of logic, in the FILE-SECTION for FD entries, or even in the ENVIRONMENT DIVISION for SELECT clauses, making COPY a general-purpose textual-inclusion mechanism rather than something limited to data definitions. Multiple copybooks are frequently nested, where one copybook itself contains a COPY statement pulling in another, so a change to a deeply shared low-level field only needs to be made in a single place.
Cricket analogy: COPY is like a franchise's scouting department pulling a player's full statistical profile from the central BCCI database into their own draft dossier, inserting the complete record rather than re-collecting the stats manually.
The REPLACING Phrase for Customization
The REPLACING phrase lets a program customize a generic copybook without editing the shared source, using syntax like COPY GENERIC-REC REPLACING ==PREFIX== BY ==CUST==, which substitutes every occurrence of the pseudo-text PREFIX with CUST as the copybook is expanded. This is commonly used when the same record structure needs to be included twice in one program under different names, such as a WS-OLD-RECORD and a WS-NEW-RECORD built from the identical field layout for a before-and-after comparison, or when a shared copybook uses a placeholder like ==FILLER-NAME== that each calling program fills in with its own file's actual name. Because REPLACING operates on the raw text before compilation, it can substitute identifiers, literals, or even whole clauses, giving copybooks template-like flexibility while still guaranteeing every program derives from the same base definition.
Cricket analogy: REPLACING is like a stadium using a generic scoreboard template but swapping in the home team's name and colors for tonight's match, keeping the underlying scoreboard logic identical while customizing the labels.
* Copybook member: CUSTREC (in COPYLIB)
01 ==PREFIX==-CUSTOMER-RECORD.
05 ==PREFIX==-CUST-ID PIC X(6).
05 ==PREFIX==-CUST-NAME PIC X(30).
05 ==PREFIX==-CUST-BALANCE PIC 9(7)V99.
* Calling program
WORKING-STORAGE SECTION.
COPY CUSTREC REPLACING ==PREFIX== BY ==OLD==.
COPY CUSTREC REPLACING ==PREFIX== BY ==NEW==.
PROCEDURE DIVISION.
IF NEW-CUST-BALANCE NOT = OLD-CUST-BALANCE
DISPLAY 'BALANCE CHANGED FOR ' NEW-CUST-ID
END-IF.Most COBOL shops maintain a single enterprise copybook library for shared record layouts, such as customer, account, and transaction records, governed by a change-control process because a single field-length change in one widely used copybook can require recompiling and retesting dozens or hundreds of dependent programs.
Because COPY performs a purely textual expansion at compile time, editing a copybook has zero effect on programs that were already compiled and are still running from an older load module. Every program that includes the changed copybook must be recompiled and, if using static CALL, relinked before the change takes effect in production.
- A copybook is a reusable source member inserted into a program at compile time via the COPY statement.
- Copybooks can hold DATA DIVISION layouts, PROCEDURE DIVISION paragraphs, FD entries, or ENVIRONMENT DIVISION clauses.
- COPY performs textual substitution before compilation, so the expanded code behaves exactly as if typed inline.
- The REPLACING phrase customizes a generic copybook by substituting pseudo-text placeholders per calling program.
- Copybooks can be nested, letting one shared low-level field change propagate through a single edit.
- Changing a copybook has no effect on already-compiled programs until they are recompiled and relinked.
- Centralized copybook libraries reduce duplication but require disciplined change control given their wide blast radius.
Practice what you learned
1. At what stage does the COPY statement's text substitution occur?
2. Which divisions can legitimately contain a COPY statement?
3. What does the REPLACING phrase on a COPY statement do?
4. If a widely used copybook's field length is changed, what must happen for the change to take effect in a program that already includes it?
5. Why is it valid to include the same copybook twice in one program using REPLACING with different substitution values?
Was this page helpful?
You May Also Like
Subprograms and CALL Statements
Learn how COBOL programs invoke reusable subprograms with the CALL statement, pass parameters via the LINKAGE SECTION, and choose between static and dynamic linkage.
Error Handling in COBOL
Learn how COBOL programs detect and respond to runtime errors using FILE STATUS codes, the ON SIZE ERROR and ON OVERFLOW phrases, and structured exception handling with USE and declaratives.
COBOL and DB2
Learn how COBOL programs embed SQL to interact with a DB2 relational database, using host variables, cursors, and SQLCA/SQLCODE for error checking.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics