Skip to main content
  1. Documentation/

ABAP Program Structure

··12 mins· loading · loading · · ·
SAP ERP Back-End
Adrien D'acunto
Author
Adrien D’acunto
Table of Contents

Basic Architecture of an ABAP Program
#

Skeleton of an ABAP Program
#

Objectives
#

  • Understand the basic structure of an ABAP PROGRAM
  • Identify standard event blocks (INITIALIZATION, START-OF-SELECTION, PERFORM, END-OF-SELECTION)
  • Know when each section of the program is executed
  • Be able to write a complete skeleton of an ABAP PROGRAM

Prerequisites
#

  1. Create a new program ZAELION_TRI_UNIT02_01 to store in your PACKAGE and OT
  2. Create the 3 INCLUDES (TOP > SCR > F01)
  3. Save, Activate objects and switch to Modification mode.

Definition
#

An ABAP PROGRAM is composed of EVENT BLOCKS determining the order and context of code execution.
These blocks structure the execution flow and make the code predictable and maintainable.

The program is a theater play REPORT = title

each block (INITIALIZATION, START-OF-SELECTION, …) = act

each PERFORM = sequence

the instructions in PERFORM = lines.

Building the Skeleton
#

Program Block: REPORT
#

  1. In the REPORT, add the following instruction between the REPORT instruction and the INCLUDES
        WRITE:/ '01 - REPORT...',
              / '     WARNING: Never implement code here!',
              / '-------------------------------------------------'.
  1. Save, Activate and Execute
        *&---------------------------------------------------------------------*
        *& Report ZAELION_FGI_UNIT02_01
        *&---------------------------------------------------------------------*
        *&
        *&---------------------------------------------------------------------*
        REPORT zaelion_fgi_unit02_01.

        WRITE:/ '01 - REPORT...',
              / '     WARNING: Never implement code here!',
              / '-------------------------------------------------'.

        INCLUDE zaelion_fgi_unit02_01_top.
        INCLUDE zaelion_fgi_unit02_01_scr.
        INCLUDE zaelion_fgi_unit02_01_f01.

INCLUDES
#

  1. In each INCLUDE, add respectively the following instructions
  • INCLUDE _TOP
      WRITE:/ '02 - INCLUDE _TOP...',
            / '     INFO: Specific to global declarations',
            / '-------------------------------------------------'.
  • INCLUDE _SCR
      WRITE:/ '03 - INCLUDE _SCR...',
            / '     INFO: Specific to selection screens',
            / '-------------------------------------------------'.
  • INCLUDE _F01
      WRITE:/ '04 - INCLUDE _F01...',
            / '     INFO: Specific to REPORT processing',
            / '-------------------------------------------------'.
  1. Save, Activate objects and Execute

Event Block: INITIALIZATION
#

The INITIALIZATION block in ABAP is a section of the REPORT executed before displaying the first selection screen (that is, before the user sees the program’s parameters and selections).

It serves to pre-fill default values in selection screen fields or to initialize global variables necessary before any user interaction. This block executes once, automatically, without explicit call from the programmer.

Imagine you open a paper form already partially filled with your name and date. You haven’t done anything yet, but someone prepared the document for you. The INITIALIZATION block plays this “preparer” role in an ABAP program: it sets up the starting values before the user sees the form (the selection screen).

  1. In the REPORT, add the following instruction below the last INCLUDE
        INITIALIZATION.
          WRITE:/ '00 - Program INITIALIZATION...',
                / '     TYPE: Event block',
                / '     WARNING: Single activation at launch',
                / '-------------------------------------------------'.
  1. Save, Activate and Execute
        *&---------------------------------------------------------------------*
        *& Report ZAELION_FGI_UNIT02_01
        *&---------------------------------------------------------------------*
        *&
        *&---------------------------------------------------------------------*
        REPORT zaelion_fgi_unit02_01.

        WRITE:/ '01 - REPORT...',
              / '     WARNING: Never implement code here!',
              / '-------------------------------------------------'.

        INCLUDE zaelion_fgi_unit02_01_top.
        INCLUDE zaelion_fgi_unit02_01_scr.
        INCLUDE zaelion_fgi_unit02_01_f01.

        INITIALIZATION.
          WRITE:/ '00 - Program INITIALIZATION...',
                / '     TYPE: Event block',
                / '     WARNING: Single activation at launch',
                / '-------------------------------------------------'.

Event Blocks: START-OF-SELECTION END-OF-SELECTION
#

The START-OF-SELECTION block is the main entry point for the logical processing of a report-type ABAP program.

This is when the main code executes, after validation of the selection screen (when the user has entered their values and launched the program). All calculation, data reading, and business logic are generally placed here.

The END-OF-SELECTION block executes after the end of START-OF-SELECTION block processing. It’s often used for final processing, such as displaying results (WRITE, ALV, etc.), or actions requiring that all data is already ready.

  1. In the REPORT, add the following instruction below the INITIALIZATION event block
        START-OF-SELECTION.
          WRITE:/ '05 - Program START-OF-SELECTION...',
                / '     TYPE: Event block',
                / '     INFO: Main REPORT processing',
                / '-------------------------------------------------'.

        END-OF-SELECTION.
          WRITE:/ '99 - Program END-OF-SELECTION...',
                / '     TYPE: Event block',
                / '     INFO: End REPORT processing',
                / '-------------------------------------------------'.
  1. Save, Activate and Execute
        *&---------------------------------------------------------------------*
        *& Report ZAELION_FGI_UNIT02_01
        *&---------------------------------------------------------------------*
        *&
        *&---------------------------------------------------------------------*
        REPORT zaelion_fgi_unit02_01.

        WRITE:/ '01 - REPORT...',
              / '     WARNING: Never implement code here!',
              / '-------------------------------------------------'.

        INCLUDE zaelion_fgi_unit02_01_top.
        INCLUDE zaelion_fgi_unit02_01_scr.
        INCLUDE zaelion_fgi_unit02_01_f01.

        INITIALIZATION.
          WRITE:/ '00 - Program INITIALIZATION...',
                / '     TYPE: Event block',
                / '     WARNING: Single activation at launch',
                / '-------------------------------------------------'.

        START-OF-SELECTION.
          WRITE:/ '05 - Program START-OF-SELECTION...',
                / '     TYPE: Event block',
                / '     INFO: Main REPORT processing',
                / '-------------------------------------------------'.

        END-OF-SELECTION.
          WRITE:/ '99 - Program END-OF-SELECTION...',
                / '     TYPE: Event block',
                / '     INFO: End REPORT processing',
                / '-------------------------------------------------'.

Sub-processing Block: PERFORM
#

The PERFORM instruction in ABAP is used to call a “subroutine” (a separate code block) defined with FORMENDFORM.

It allows structuring the program into several logical parts, each performing a specific task.

The benefit: storing precise logic for the entire program.

In a recipe, the PERFORM would be equivalent to a simple step of the whole such as “Wash the vegetables”. Although the step is “simple”, it requires a logical sequence of steps planned for that purpose only.

A subroutine only executes when called by a PERFORM (hence the importance of implementing it in the right place in the REPORT).

Data can be passed between the main REPORT program and the PERFORM subroutine through global variables or through parameters (USING, CHANGING) but you’ll see this in their specific module.

  1. In the REPORT, add the following instruction in the INITIALIZATION block
        PERFORM report_initialization.
  1. Double-click on the PERFORM name → creation popup → validate → save if requested → select the previously created INCLUDE F01 → validate

  2. Implement the following WRITE in the newly created FORM

        WRITE:/ '     A - PERFORM report_initialization...',
              / '         INFO: Initialization subprocess',
              / '-------------------------------------------------'.
  1. Save, Activate objects and Execute
        *&---------------------------------------------------------------------*
        *& Form report_initialization
        *&---------------------------------------------------------------------*
        *& text
        *&---------------------------------------------------------------------*
        *& -->  p1        text
        *& <--  p2        text
        *&---------------------------------------------------------------------*
        FORM report_initialization .

          WRITE:/ '     A - PERFORM report_initialization...',
                / '         INFO: Initialization subprocess',
                / '-------------------------------------------------'.

        ENDFORM.
  1. Same for the START-OF-SELECTION event block with PERFORM report_subprocess_01. and PERFORM report_subprocess_02.

    a. PERFORM report_subprocess_01.

        *&---------------------------------------------------------------------*
        *& Report ZAELION_FGI_UNIT02_01
        *&---------------------------------------------------------------------*
        *&
        *&---------------------------------------------------------------------*
        REPORT zaelion_fgi_unit02_01.

        WRITE:/ '01 - REPORT...',
              / '     WARNING: Never implement code here!',
              / '-------------------------------------------------'.

        INCLUDE zaelion_fgi_unit02_01_top.
        INCLUDE zaelion_fgi_unit02_01_scr.
        INCLUDE zaelion_fgi_unit02_01_f01.

        INITIALIZATION.
          WRITE:/ '00 - Program INITIALIZATION...',
                / '     TYPE: Event block',
                / '     WARNING: Single activation at launch',
                / '-------------------------------------------------'.

          PERFORM report_initialization.

        START-OF-SELECTION.
          WRITE:/ '05 - Program START-OF-SELECTION...',
                / '     TYPE: Event block',
                / '     INFO: Main REPORT processing',
                / '-------------------------------------------------'.

          PERFORM report_subprocess_01.

Add this to the newly created PERFORM

        WRITE:/ '     A - PERFORM report_subprocess_01...',
              / '         INFO: REPORT subprocess 01',
              / '-------------------------------------------------'.

Save, Activate objects and Execute.

b. PERFORM report_subprocess_02.

        *&---------------------------------------------------------------------*
        *& Report ZAELION_FGI_UNIT02_01
        *&---------------------------------------------------------------------*
        *&
        *&---------------------------------------------------------------------*
        REPORT zaelion_fgi_unit02_01.

        WRITE:/ '01 - REPORT...',
              / '     WARNING: Never implement code here!',
              / '-------------------------------------------------'.

        INCLUDE zaelion_fgi_unit02_01_top.
        INCLUDE zaelion_fgi_unit02_01_scr.
        INCLUDE zaelion_fgi_unit02_01_f01.

        INITIALIZATION.
          WRITE:/ '00 - Program INITIALIZATION...',
                / '     TYPE: Event block',
                / '     WARNING: Single activation at launch',
                / '-------------------------------------------------'.

          PERFORM report_initialization.

        START-OF-SELECTION.
          WRITE:/ '05 - Program START-OF-SELECTION...',
                / '     TYPE: Event block',
                / '     INFO: Main REPORT processing',
                / '-------------------------------------------------'.

          PERFORM report_subprocess_01.
          PERFORM report_subprocess_02.

Add this to the newly created PERFORM

        WRITE:/ '     B - PERFORM report_subprocess_02...',
              / '         INFO: REPORT subprocess 02',
              / '-------------------------------------------------'.

Save, Activate objects and Execute.

Block Explanations
#

BLOCK EXECUTION MOMENT MAIN USAGE
REPORT Program start Defines program name and type
INCLUDES Includes code segments Modularization: top, screens, logic
INITIALIZATION Before any display Initializations and default values
START-OF-SELECTION After selection-screen validation Main processing
PERFORM Sub-processing to place in INCLUDE F01 Sub-processing
END-OF-SELECTION At the end of processing Final displays and summary

If no block is declared, the code is implicitly executed in START-OF-SELECTION.

Best Practices
#

Best Practice Explanation
Always separate blocks with * comments Facilitates reading and maintenance
Use INCLUDES for modularization Avoids duplication and clarifies code role
Comment each section Guides review and training
Respect SAP execution order Avoids unexpected behaviors
Don’t place code before REPORT SAP will reject or ignore misplaced code

Summary
#

  • An ABAP PROGRAM is organized into event blocks to control execution order.
  • Including INCLUDES (TOP, SCR, F01) facilitates modularity and maintenance.
  • Respecting the structure (REPORT -> INCLUDES -> INITIALIZATION -> START-OF-SELECTION -> PERFORM -> END-OF-SELECTION -> PERFORM) guarantees readability and predictability.

Comments in ABAP Code
#

Objectives
#

  • Understand the role of COMMENTS in ABAP code
  • Identify different types of COMMENTS
  • Know how to apply best practice conventions

Keyboard Shortcuts
#

  • [ CTRL ] + [ ? ] automatically comments the selected line
  • [ CTRL ] + [ ; ] automatically uncomments the selected line

Definition
#

A COMMENT in ABAP is a line or block of text ignored by the execution system.
Its only role is to explain the code for humans.
A good COMMENT helps understand why code exists, not just what it does.

A COMMENT is a note in the margin of an engineering blueprint.
The blueprint builds the machine (the code), the note explains the reason for each piece.

Explanation
#

COMMENTS are essential to:

  • facilitate code reading by another developer,
  • help with maintenance over time,
  • document technical choices,
  • make a program’s logical structure visible.

In a company, ABAP code is rarely read by its author after a few weeks.
COMMENTS guarantee collective continuity of understanding.

Types of Comments
#

Single Line Comment
#

Use the * symbol in the first column (at the beginning of the line).
Everything following on the line is ignored by the system.

    * Variable declaration
    DATA: lv_age TYPE i.

The * must be placed in the first column (at the beginning of the line), otherwise SAP may interpret the line as code.

End-of-Line Comment
#

Use the " symbol (double quote) to comment after an instruction.

    DATA: lv_variable TYPE string.          "Variable to store first name
    CONSTANTS: lc_age TYPE i VALUE 30.      "Fixed age for testing

End-of-line COMMENTS explain the exact role of a variable or instruction without breaking code readability.

Structured Multi-line Comment
#

Use *& to build visual COMMENT blocks.
This method is used for headers, sections, or long explanations.

    *&---------------------------------------------------------------------*
    *& Global constants declaration
    *&---------------------------------------------------------------------*

    CONSTANTS: lc_limit TYPE i VALUE 100.

Think of a sign at a construction site: the *&---* block signals an important code area.

Maximum Length
#

SAP technically limits reading to 72–76 characters per line.
COMMENTS that are too long may be truncated.

Truncated text can make the COMMENT incoherent.
Better to have several brief aligned lines than one phrase that’s too long.

Best Practices
#

Best Practice Explanation
Start COMMENTS with a capital letter Improves readability
Limit to one idea per line Avoids visual overload
Update COMMENTS after any modification Prevents code/documentation inconsistencies
Align COMMENTS in blocks Improves code visual structure
Don’t over-comment obvious code Promotes clarity: comment intention, not syntax

Too many “useless” COMMENTS can pollute code readability.
A good COMMENT answers the question: Why does this code exist?

Example
#

    * Declaration of a variable to store first name
    DATA: lv_firstname TYPE string VALUE 'Alice'.     "First name variable

    * Display message on screen
    WRITE: / 'Hello', lv_firstname, '!'.              "Displays Hello Alice

Summary
#

  • COMMENTS document code without being executed.
  • Use * for an entire line, " for end of line, *& for a block.
  • Respect length limit and update COMMENTS with each evolution.
  • Standard header must be present in every program.
  • Good COMMENTS explain intention, not syntax.

Header (ABAP Program Header)
#

Objectives
#

  • Understand the role of the HEADER in an ABAP program
  • Identify essential information it contains
  • Know how to structure and write a HEADER correctly

Definition
#

A HEADER is a text block placed at the very beginning of an ABAP REPORT, a CLASS METHOD, a FUNCTION MODULE, etc.
It describes the context: its name, author, function, and history.

The HEADER is the program’s ID card.
It indicates who created it, when, for what purpose, and where the version stands.

It helps developers, administrators, and maintenance teams quickly understand the origin and purpose of the program.

Why Use a Header?
#

Without a HEADER, it becomes difficult to know what a program is for or who developed it.
In an environment where several people work on the same system, the HEADER is an indispensable reference point.

It allows you to:

  • Identify the code owner.
  • Understand the program’s role without reading all the code.
  • Track changes over time (history).
  • Ensure traceability between need, ticket, and development.

A well-filled HEADER avoids wasting time on unnecessary technical analysis.

Standard Header Structure
#

Element Description
Program name ABAP program technical identifier (e.g.: ZFGI_HELLO_WORLD)
Program type Category: REPORT, INCLUDE, FUNCTION MODULE, etc.
Author Developer name or trigram
Creation/modification date Change history
Description Summary of program role
Version / Ticket Link to project or fix tracking
Organization / Department Development responsible service

SAP GUI doesn’t automatically validate HEADER content.
It’s up to you (or your team) to respect common convention.

Standard Header Type Example
#

    *&---------------------------------------------------------------------*
    *& PROGRAM NAME                                                        *
    *& ------------------------------------------------------------------- *
    *& Object  : Object name                                               *
    *& Ticket  : Ticket number                                             *
    *& Title   : Ticket title                                              *
    *& ------------------------------------------------------------------- *
    *& Comment :                                                           *
    *& Short program description                                           *
    *& ------------------------------------------------------------------- *
    *& Modification history                                                *
    *& Date      | Name (Trigram)     | Ticket - Object                    *
    *& 00.00.2025| P.NAME (PNA)       | Program creation                   *
    *&---------------------------------------------------------------------*

Imagine a project sheet stuck to your code: each developer can see at a glance what it does, why, and by whom.

Complete Example in a Program
#

    *&---------------------------------------------------------------------*
    *& Report  ZAELION_FGI_UNIT02_01
    *&---------------------------------------------------------------------*
    *&
    *&---------------------------------------------------------------------*

    *&---------------------------------------------------------------------*
    *& ZAELION_FGI_UNIT02_01                                               *
    *& ------------------------------------------------------------------- *
    *& Object  : Demonstration - Program structure                         *
    *& Ticket  : DEV-0001                                                  *
    *& Title   : First ABAP program                                        *
    *& ------------------------------------------------------------------- *
    *& Comment : Example program for ABAP introduction                     *
    *& ------------------------------------------------------------------- *
    *& Modification history                                                *
    *& Date      | Name (Trigram)     | Ticket - Object                    *
    *& 01.01.2025| F.GIUSTINI (FGI)   | Program creation                   *
    *&---------------------------------------------------------------------*

    REPORT zaelion_fgi_unit02_01.

    WRITE:/ '01 - REPORT...',
        / '     WARNING: Never implement code here!',
        / '-------------------------------------------------'.

    INCLUDE zaelion_fgi_unit02_01_top.
    INCLUDE zaelion_fgi_unit02_01_scr.
    INCLUDE zaelion_fgi_unit02_01_f01.

    INITIALIZATION.
    WRITE:/ '00 - Program INITIALIZATION...',
            / '     TYPE: Event block',
            / '     WARNING: Single activation at launch',
            / '-------------------------------------------------'.

    PERFORM report_initialization.

    START-OF-SELECTION.
    WRITE:/ '05 - Program START-OF-SELECTION...',
            / '     TYPE: Event block',
            / '     INFO: Main REPORT processing',
            / '-------------------------------------------------'.

    PERFORM report_subprocess_01.
    PERFORM report_subprocess_02.

    END-OF-SELECTION.
    WRITE:/ '99 - Program END-OF-SELECTION...',
            / '     TYPE: Event block',
            / '     INFO: End REPORT processing',
            / '-------------------------------------------------'.
  • The upper block (HEADER) presents the program.
  • The following blocks (SELECTION-SCREEN, START-OF-SELECTION, etc.) structure the code.
  • Each section is visually delimited for smooth and quick reading.

Best Practices
#

Best Practice Explanation
Fill in each HEADER field Guarantees clarity and traceability
Use *& separators Standardizes presentation across the team
Update history with each change Reliable tracking of code evolution
Add date and developer trigram Clear identification of contributors
Adopt a common structure Facilitates review and technical audits
Keep a clear and concise tone Quick reading, without information overload

An empty field in a header can complicate incident search or fix management.

Visual Memory Aid
#

A header is:

  • At the top of the program
  • Delimited by *&---*
  • Always up to date
  • Identifiable by all
  • Unique to each program

When creating a new program in SE38 or SE80, always copy a standard header provided by your team before starting your code.

Summary
#

  • The HEADER is the descriptive heading of each ABAP program.
  • It serves to quickly identify the what, who, when, and why of the code.
  • Always placed at the beginning of the program, it guarantees traceability and consistency.
  • It must be complete, up to date, and homogeneous throughout the SAP system.
  • A program without a header is like a document without a title: difficult to understand and maintain.

Related

Instructions in ABAP
··20 mins· loading · loading
SAP ERP Back-End
Introduction to ABAP
·14 mins· loading · loading
SAP ERP Back-End
ABAP Variables & Declarations in ABAP
··11 mins· loading · loading
SAP ERP Back-End
Dracula Theme
·1 min· loading · loading
SAP ERP
Business Functions and SAP - Complete Guide
··7 mins· loading · loading
ERP SAP
Introduction to SAP
··6 mins· loading · loading
ERP SAP