SAP ABAP Sample Programs for Beginners

If you’re just starting with SAP ABAP (Advanced Business Application Programming), practicing small programs is the best way to understand syntax, data types, and SAP’s data dictionary concepts. Below are simple, beginner-friendly ABAP examples that cover basic programming, internal tables, loops, selection screens, and reports.

1. Hello World Program

The most basic ABAP program displays text output.

REPORT z_hello_world.

WRITE: 'Hello, SAP World!'.

Explanation:

2. Display System Date and Time

REPORT z_display_date_time.

DATA: lv_date TYPE d,
      lv_time TYPE t.

lv_date = sy-datum.
lv_time = sy-uzeit.

WRITE: / 'Current Date:', lv_date,
       / 'Current Time:', lv_time.

Key Points:

  • sy-datum and sy-uzeit are system fields for date and time.
  • / before WRITE prints a new line.

3. Add Two Numbers

REPORT z_add_numbers.

PARAMETERS: p_num1 TYPE i,
            p_num2 TYPE i.

DATA: lv_sum TYPE i.

lv_sum = p_num1 + p_num2.

WRITE: / 'Sum of numbers is:', lv_sum.

Explanation:

  • PARAMETERS create input fields on the selection screen.
  • Data type i stands for integer.

4. Loop Through Internal Table

REPORT z_loop_itab.

DATA: lt_names TYPE TABLE OF string,
      lv_name  TYPE string.

APPEND 'SAP' TO lt_names.
APPEND 'ABAP' TO lt_names.
APPEND 'HANA' TO lt_names.

LOOP AT lt_names INTO lv_name.
  WRITE: / 'Technology:', lv_name.
ENDLOOP.

Key Concepts:

  • Internal tables (TABLE OF) store multiple records in memory.
  • LOOP AT reads each record line by line.

5. Read Data from a Transparent Table (e.g., MARA)

REPORT z_read_mara.

TABLES: mara.

SELECT * FROM mara UP TO 5 ROWS.
  WRITE: / mara-matnr, mara-ersda, mara-mtart.
ENDSELECT.

Explanation:

  • SELECT retrieves data from database tables.
  • UP TO 5 ROWS limits the output for beginners.
  • Fields MATNR, ERSDA, MTART belong to the MARA (Material Master) table.

6. Use SELECT…INTO Internal Table

REPORT z_select_into_itab.

TYPES: BEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         ersda TYPE mara-ersda,
       END OF ty_mara.

DATA: lt_mara TYPE TABLE OF ty_mara,
      ls_mara TYPE ty_mara.

SELECT matnr ersda
  FROM mara
  INTO TABLE lt_mara
  UP TO 10 ROWS.

LOOP AT lt_mara INTO ls_mara.
  WRITE: / ls_mara-matnr, ls_mara-ersda.
ENDLOOP.

Concepts Covered:

  • Structure declaration using TYPES.
  • Bulk selection into an internal table.

7. Use IF / ELSE Conditions

REPORT z_if_else.

PARAMETERS: p_age TYPE i.

IF p_age < 18.
  WRITE: 'You are a minor.'.
ELSEIF p_age < 60.
  WRITE: 'You are an adult.'.
ELSE.
  WRITE: 'You are a senior citizen.'.
ENDIF.

Key Learning:
Basic decision-making structure in ABAP.

8. Simple ALV Report

REPORT z_alv_report.

DATA: lt_spfli TYPE TABLE OF spfli.

SELECT * FROM spfli INTO TABLE lt_spfli UP TO 20 ROWS.

cl_salv_table=>factory(
  IMPORTING r_salv_table = DATA(lo_alv)
  CHANGING  t_table      = lt_spfli ).

lo_alv->display( ).

Explanation:

  • SPFLI is a standard SAP flight connection table.
  • CL_SALV_TABLE is a modern ABAP class for displaying ALV reports (interactive tables).

9. Case Statement Example

REPORT z_case_example.

PARAMETERS: p_grade TYPE c LENGTH 1.

CASE p_grade.
  WHEN 'A'.
    WRITE: 'Excellent'.
  WHEN 'B'.
    WRITE: 'Good'.
  WHEN 'C'.
    WRITE: 'Average'.
  WHEN OTHERS.
    WRITE: 'Invalid grade'.
ENDCASE.

Purpose: Demonstrates multi-condition control structure.

10. Basic String Manipulation

REPORT z_string_example.

DATA: lv_text TYPE string VALUE 'SAP ABAP Tutorial'.

WRITE: / lv_text+4(4).     "Extract substring
WRITE: / strlen( lv_text ). "Display string length

Explanation:

  • lv_text+4(4) extracts a substring starting at position 4 for 4 characters.
  • strlen() calculates the string length.

Summary

Here’s a quick reference of what you learned:

ConceptExample Program
Print textHello World
System variablesDate & Time
ArithmeticAdd Numbers
Internal tablesLOOP AT example
Database accessSELECT from MARA
ConditionsIF / ELSE, CASE
ReportsALV output
StringsSubstring, Length

Tip for Beginners:
Start by running these programs in SE38 or ABAP Development Tools (ADT in Eclipse). Gradually explore database joins, modularization (FORMs, FUNCTIONs), and OOP (Classes and Methods).

Leave a Comment