What is an RPG Copybook?

October 10, 2008

What is an RPG Copybook?

By NickLitten

October 10, 2008

copybook, RPG

A copybook is a technique that us RPG PROGRAMMERS use to suck in piece of re-useable code without having to type it every time into lots of programs.

A typical use of a copybook might be something like this scenario:

  • Every program within an application uses the *LDA (Local Data Area) to store specific pieces of information.
  • Every program expects the *LDA to be defined in the same way.
  • Defining the *LDA takes 30 lines of code, which would be duplicated through the entire application in every single program source code.
  • We define these 30 lines in a COPYBOOK member — think of this as a ‘chapter’ of a book.
  • Then we add a simple ‘import chapter 7 of the copybook’ into every program — so one line of code in every program that imports the same 30 lines from the copybook. This import takes place at compile time.

Declaring a copybook means declaring its location to the program that will be importing the code from the copybook.

Why do we use copybooks?

Using our example of the *LDA – One huge advantage is that if we want to change the layout of our *LDA (perhaps to add a new field) then we can simply make the code change to the copybook member. Then as we recompile the programs that import it, they will pull in the new version and compile with it. The obvious advantage is that we would have to actually make any code changes to any of the programs that use the copybook, we just simply recompile them.

To declare a copybook we use the compiler directive /COPY or /INCLUDE. After this we write the name of the copybook.

D* This is a copy book importing RPG code into our program
 /COPY lib/file,member

The compiler directive begins at the 7th position — this means the slash ‘/’ is on the character after the specification character (F/D/C/O etc)

We simply specify the name of the copybook. In the above example I have qualified the library name but we could always just use the library list to find and select the copybook: This is the preferred technique as used in most change management environments

D* This is a copy book importing RPG code into our program from somewhere in the
 /COPY file,member

Remember that if we are using *LIBL then the first copybook FILE will be found in the *LIBL and it will attempt to pull the member source into it from there.


1. Declaring a copybook means telling the compiler its location.
2. On AS400, iSeries and IBM i computers we require three things to uniquely identify a source code. Library, Source Physical File’s Name and Member Name
3. Library: The first occurrence in the library list.
4. Source Physical File’s Name: It’s declared immediately after the compiler directive /COPY with a blank space.
5. Member: This is the actual copybook we are interested in. It’s declared after specifying the name of source PF with a comma. Refer the example above for clarification.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips