Renaming a field in an RPG program 

 September 5, 2012

By  NickLitten

Sometimes you want to read a file but a field on that file is giving you problems because the name conflicts with something that already exists in your program. There are a few scenarios for this head scratching problem, but luckily it also has several solutions. So lets look at some basic problems:

  1. I am using two files in the same RPG Program.
  2. In the two files some of the fields have same name, so it will not compile!
  3. In my program I want to rename the fields of one of the file.

How do I rename the fields of the Physical file in a RPG Program?

Rename all fields in one go:

FFileName IF E K DISK 

This will prefix all fields for this file with “A_”

Rename fields individually

Define an externally described data structure and rename fields inside that data structure.

D Wrk_Rcd E DS EXTNAME(FileName) 
D Wrk_Field1 E EXTFLD(Field1) 
D Wrk_Field2 E EXTFLD(Field2)

In this example Field1 and Field2 are respectively renamed Wrk_Field1 and Wrk_Field2. Other fields are unaltered.

However I think that you’d be better off using the PREFIX keyword instead to get rid of that EXTFLD so RPGIII style.

D Wrk_Rcd E DS EXTNAME(MyFile) 
 Read MyFile Wrk_Rcd;

PREFIX (prefix{:nbr_of_char_replaced})
The PREFIX keyword is used to partially rename the fields in an externally described file. The character string or character literal specified is prefixed to the names of all fields defined in all records of the file specified in positions 7-16. In addition, you can optionally specify a numeric value to indicate the number of characters, if any, in the existing name to be replaced. If the ’nbr_of_char_replaced’ is not specified, then the string is attached to the beginning of the name.

If the ’nbr_of_char_replaced’ is specified, it must be a numeric constant containing a value between 0 and 9 with no decimal places. For example, the specification PREFIX(YE:3) would change the field name ’YTDTOTAL’ to ’YEARTOTAL’. Specifying a value of zero is the same as not specifying ’nbr_of_char_replaced’ at all.

You can explicitly rename a field on an input specification, even when the PREFIX keyword is specified for a file. The compiler will recognize (and require) the name which is first USED in your program. For example, if you specify the prefixed name on an input specification to associate the field with an indicator, and you then try to rename the field referencing the unprefixed name, you will get an error. Conversely, if you first rename the field to something other than the prefixed name, and you then use the prefixed name on a specification, you will get an error at compile-time.

The total length of the name after applying the prefix must not exceed the maximum length of an RPG field name.

The number of characters in the name to be prefixed must not be less than or equal to the value represented by the ’nbr_of_char_replaced’ parameter. That is, after applying the prefix, the resulting name must not be the same as the prefix string.

  • If the prefix is a character literal, it can end in a period.
  • If the prefix is a character literal, it must be uppercase. In this case, the field names must all be subfields of the same qualified data structure.


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"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

I’m always looking for awesome input, feedback and critique!