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-datumandsy-uzeitare system fields for date and time./beforeWRITEprints 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:
PARAMETERScreate input fields on the selection screen.- Data type
istands 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 ATreads 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:
SELECTretrieves data from database tables.UP TO 5 ROWSlimits the output for beginners.- Fields
MATNR,ERSDA,MTARTbelong 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:
SPFLIis a standard SAP flight connection table.CL_SALV_TABLEis 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:
| Concept | Example Program |
|---|---|
| Print text | Hello World |
| System variables | Date & Time |
| Arithmetic | Add Numbers |
| Internal tables | LOOP AT example |
| Database access | SELECT from MARA |
| Conditions | IF / ELSE, CASE |
| Reports | ALV output |
| Strings | Substring, 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).