What is an RPG Copybook?

October 10, 2008

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.


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

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

Start your
7-day free trial

Take This Course with ALL ACCESS

Unlock your Learning Potential with instant access to every course and all new courses as they are released.
 [ For Serious Software Developers only ]

IBM i Training For Technology Experts


Successfully Work Remote 

 $ 129.00  $59.00

By adding new skills in information technology, employers will be confident that you have the necessary skills and tools needed to successfully work remotely


Snug CBD

 20% Discount

I have partnered with SNUG CBD givING you Organic CBD
20% discount code "NL20"