How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

IBM i e-Book

A Developer’s Guide to Mastering IBM i Concepts

Report Program Generator (RPG)

Data Structure

A data structure in RPG is a named area of storage that is divided into subfields. Subfields can be any RPG data type, including other data structures. Data structures can be used to organize data in a logical way and to make it easier to access and manipulate.

Free Format

Fixed Format

Types of Data Structures in RPG

  1. Externally described Data Structures
  2. Multiple occurrence Data Structures
  3. Data area Data Structures
  4. Qualified Data Structures
  5. File information Data Structures
  6. Indicator Data Structures
  7. Program status Data Structures
  • Externally described Data StructuresAn externally described data structure in RPG is a data structure whose definition is stored in an external file. This allows you to define the data structure once and then use it in multiple programs.

    To define an externally described data structure, you can use the DCL-DS operation code and the EXT or EXTNAME keyword.

    The EXT keyword specifies that the data structure definition is stored in an external file. The EXTNAME keyword specifies the name of the external file.

    Here, we have used the externally described PF file (CUST).

    Fixed format

    Free format

  • Multiple occurrence Data Structures A multiple occurrence data structure in RPG AS/400 is a data structure that can contain multiple occurrences of the same data. This can be useful for storing data that is repeated, such as a list of items in a purchase order or a table of data from a database.

    To define a Multiple Occurrence data structure in RPG AS/400, you use the OCCURS keyword. The OCCURS keyword specifies the number of occurrences of the data structure that can exist.

    Fixed format

    Free format

    Code example


  • Data area Data Structures A data area data structure in RPG is a data structure that is defined in a program and is associated with a data area. Data area data structures are automatically read in and locked at program initialization time, and the contents of the data structure are written to the data area when the program ends with LR on.

    Command to create a Data Area – CRTDTAARA

    Command to display the Data Area – DSPDTAARA

    Fixed format

    Free Format

    Code example

  • Qualified Data StructuresThe qualified data structure is a data structure which allows us to define same fields into two or more data structures. A qualified data structure is used to group related data items together. Each data item within the qualified data structure has a qualified name, which includes the data structure name and the field name separated by a dot known as period.
    Fixed format syntax

    Free format syntax

    Code example


  • File information Data StructuresA file information data structure (INFDS) in RPG is a data structure that contains information about a file, such as the file status, file feedback, and input/output feedback. INFDS are used by RPG programs to handle file exceptions and errors.
    Fixed format

    Free format

    Code example

  • Indicator Data StructuresAn indicator data structure in RPG is a data structure that is used to store conditioning and response indicators for a workstation/display file (DSPF) and the printer file (PRTF). Indicator data structures are defined using the DCL-DS operation code and the INDDS keyword. The INDDS keyword specifies the name of the file that the indicator data structure is associated with.

    INDARA keyword is declared in display file when we want to use an Indicator data structure.

    Fixed format

    Free format

  • Program status Data StructuresA program status data structure (PSDS) in RPG is a data structure that contains information about the status of a program, including any errors that have occurred. The PSDS is defined in the main source section of the program and is accessible to all modules in the program. Only one PSDS is allowed per module.

    Here, is the list of all the program status information.


    Fixed format

    Free format

Usage

  • Group fields: Data structure can be used to group fields and can make a single string.

  • Break fields into subfields: it can be used to break a string into several different fields.

Restrictions

  • Fixed Format: RPG traditionally used fixed-format source code, which means that columns are predefined for specific purposes. This can limit the flexibility in defining data structures.
  • Maximum Number of Elements: RPG has limits on the number of elements (fields) you can define in a data structure. The maximum number of elements varies depending on the RPG version and the specific compiler, but it’s typically around 99 elements.
  • Field Names: Field names in RPG data structures must adhere to specific naming conventions. For example, they are limited to 10 characters in fixed-format RPG, and no limit in free-format RPG but the most preferred is 10 characters.
  • No Dynamic Data Structures: RPG typically does not support dynamic data structures, such as linked lists or dynamically allocated arrays. Data structures are typically defined statically at compile time.
  • Data Structure Alignment: RPG data structures may be subject to alignment requirements, which can affect the storage layout and padding of fields within a structure.

How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

share_iconShare
X

Awards and Certifications

company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo