SAP ABAP Explained — Beginner's Guide
In this tutorial, you'll learn about SAP ABAP Explained. We cover key concepts, practical examples, and best practices.
ABAP (Advanced Business Application Programming) is SAP's proprietary programming language used to customize, extend, and build enterprise applications on the SAP platform with data types, internal tables, and report programs.
What You'll Learn
You will learn the fundamentals of ABAP programming — data types, internal tables, modularization, reports, and how ABAP integrates with the SAP application server.
Why It Matters
Every SAP implementation requires some level of customization. Standard SAP covers about 80% of business needs. The remaining 20% — company-specific reports, custom business logic, enhanced user interfaces — requires ABAP development. Companies pay premium salaries for ABAP developers because custom SAP solutions are critical to their operations.
Real-World Use
A logistics company needs a custom report showing delivery delays by region. SAP's standard reports don't group data the way they need. An ABAP developer writes a program that reads delivery tables, applies the company's business rules, and outputs a formatted report — all running inside the SAP system with direct access to live business data.
Learning Path
flowchart LR A["SAP Explained"] --> B["ABAP Programming
You are here"] B --> C["SAP Modules Deep Dive"] C --> D["SAP FICO"] D --> E["SAP Basis Administration"] style B fill:#f90,color:#fff
ABAP Data Types
ABAP is a strongly typed language. Every variable must have a declared data type.
Elementary Data Types
| Type | Size | Description | Example |
|---|---|---|---|
| C | 1-65535 chars | Text/character | 'Hello' |
| N | 1-65535 digits | Numeric text | '00123' |
| D | 8 chars | Date (YYYYMMDD) | '20260622' |
| T | 6 chars | Time (HHMMSS) | '143000' |
| I | 4 bytes | Integer | 42 |
| F | 8 bytes | Float | 3.14 |
| P | 1-16 bytes | Packed decimal | 123.45 |
Declaring Variables
DATA: lv_name TYPE C LENGTH 30,
lv_age TYPE I,
lv_salary TYPE P LENGTH 8 DECIMALS 2,
lv_date TYPE D,
lv_percent TYPE P LENGTH 3 DECIMALS 1 VALUE '10.5'.
lv_name = 'Alice'.
lv_age = 30.
lv_salary = 75000.00.
Expected output: Variables are declared and assigned values. In the debugger, lv_name shows 'Alice', lv_age shows 30, lv_salary shows 75000.00.
ABAP Internal Tables
Internal tables are ABAP's most important data structure. They are runtime arrays that hold multiple rows of structured data — similar to a database table but stored in memory.
Types of Internal Tables
| Table Type | Key Behavior | Use Case |
|---|---|---|
| STANDARD | Index-based access, no key uniqueness | General purpose, sequential processing |
| SORTED | Key-based access, sorted automatically | Read operations by key |
| HASHED | Key-based access, uses hash algorithm | Direct key reads on large datasets |
Working with Internal Tables
TYPES: BEGIN OF ty_employee,
emp_id TYPE N LENGTH 6,
name TYPE C LENGTH 30,
salary TYPE P LENGTH 8 DECIMALS 2,
dept TYPE C LENGTH 10,
END OF ty_employee.
DATA: lt_employees TYPE TABLE OF ty_employee,
ls_employee TYPE ty_employee.
* Add first employee
ls_employee-emp_id = '100001'.
ls_employee-name = 'Alice Johnson'.
ls_employee-salary = 85000.00.
ls_employee-dept = 'FI'.
APPEND ls_employee TO lt_employees.
* Add second employee
ls_employee-emp_id = '100002'.
ls_employee-name = 'Bob Smith'.
ls_employee-salary = 92000.00.
ls_employee-dept = 'CO'.
APPEND ls_employee TO lt_employees.
* Add third employee
ls_employee-emp_id = '100003'.
ls_employee-name = 'Carol Davis'.
ls_employee-salary = 78000.00.
ls_employee-dept = 'MM'.
APPEND ls_employee TO lt_employees.
* Display all employees
LOOP AT lt_employees INTO ls_employee.
WRITE: / ls_employee-emp_id,
ls_employee-name,
ls_employee-salary,
ls_employee-dept.
ENDLOOP.
Expected output:
100001 Alice Johnson 85000.00 FI
100002 Bob Smith 92000.00 CO
100003 Carol Davis 78000.00 MM
Reading from Internal Tables
* Read by index (STANDARD table)
READ TABLE lt_employees INTO ls_employee INDEX 2.
WRITE: / 'Index 2:', ls_employee-name.
* Read by key
READ TABLE lt_employees INTO ls_employee
WITH KEY dept = 'CO'.
WRITE: / 'CO Employee:', ls_employee-name.
Expected output:
Index 2: Bob Smith
CO Employee: Bob Smith
ABAP Reports
Reports are executable ABAP programs that read data, process it, and display results. They are the most common type of ABAP program.
Report Structure
REPORT Z_EMPLOYEE_REPORT.
TYPES: BEGIN OF ty_employee_info,
emp_id TYPE N LENGTH 6,
name TYPE C LENGTH 30,
salary TYPE P LENGTH 8 DECIMALS 2,
dept TYPE C LENGTH 10,
END OF ty_employee_info.
DATA: lt_employees TYPE TABLE OF ty_employee_info,
ls_employee TYPE ty_employee_info.
* Selection screen parameters
PARAMETERS: p_dept TYPE C LENGTH 10 DEFAULT 'FI'.
* Populate test data using inline data declarations
lt_employees = VALUE #(
( emp_id = '100001' name = 'Alice Johnson' salary = '85000.00' dept = 'FI' )
( emp_id = '100002' name = 'Bob Smith' salary = '92000.00' dept = 'CO' )
( emp_id = '100003' name = 'Carol Davis' salary = '78000.00' dept = 'FI' )
( emp_id = '100004' name = 'David Lee' salary = '88000.00' dept = 'MM' )
).
* Filter by department
LOOP AT lt_employees INTO ls_employee WHERE dept = p_dept.
WRITE: / ls_employee-emp_id,
ls_employee-name,
ls_employee-salary.
ENDLOOP.
* Summary
SKIP 2.
WRITE: / 'Total employees in dept', p_dept, ':', sy-tabix, 'records'.
Expected output (when executed with p_dept = 'FI'):
100001 Alice Johnson 85000.00
100003 Carol Davis 78000.00
Total employees in dept FI: 2 records
Open SQL in ABAP
ABAP programs can read and write SAP database tables directly using Open SQL. This is ABAP's most powerful feature — direct access to live business data.
Reading Database Tables
REPORT Z_MATERIAL_READ.
DATA: lt_mara TYPE TABLE OF mara,
ls_mara TYPE mara.
* Select materials with specific characteristics
SELECT *
FROM mara
INTO TABLE lt_mara
UP TO 10 ROWS
WHERE mtart = 'FERT'
AND mjahr = '2026'.
IF sy-subrc = 0.
LOOP AT lt_mara INTO ls_mara.
WRITE: / ls_mara-matnr,
ls_mara-mtart,
ls_mara-meins.
ENDLOOP.
ELSE.
WRITE: / 'No materials found.'.
ENDIF.
Expected output: Material numbers, material types, and base units of measure for up to 10 finished products from the database.
Inserting Data
DATA: ls_marc TYPE marc.
ls_marc-matnr = '100-100'.
ls_marc-werks = 'PL01'.
ls_marc-pstat = 'X'.
ls_marc-nfmat = ''.
INSERT INTO marc VALUES ls_marc.
IF sy-subrc = 0.
WRITE: / 'Plant data inserted successfully.'.
ELSE.
WRITE: / 'Insert failed. Record may already exist.'.
ENDIF.
Expected output: "Plant data inserted successfully." If the record already exists, the message indicates the insert failed due to a duplicate key.
Modularization: Function Modules and Methods
ABAP encourages breaking code into reusable units.
Function Modules
* Function module in a function group
FUNCTION Z_CALCULATE_ANNUAL_SALARY.
*"---------------------------------------------------------------
*" Importing
*" VALUE(IV_MONTHLY) TYPE P
*" Exporting
*" VALUE(EV_ANNUAL) TYPE P
*"---------------------------------------------------------------
EV_ANNUAL = IV_MONTHLY * 12.
ENDFUNCTION.
Expected output: When called with IV_MONTHLY = 7000, the function returns EV_ANNUAL = 84000.
ABAP Classes and Methods (OO ABAP)
CLASS ZCL_SALARY_CALCULATOR DEFINITION.
PUBLIC SECTION.
METHODS: calculate_tax
IMPORTING iv_annual TYPE P
RETURNING VALUE(rv_tax) TYPE P.
ENDCLASS.
CLASS ZCL_SALARY_CALCULATOR IMPLEMENTATION.
METHOD calculate_tax.
IF iv_annual <= 100000.
rv_tax = iv_annual * '0.15'.
ELSE.
rv_tax = iv_annual * '0.25'.
ENDIF.
ENDMETHOD.
ENDCLASS.
* Usage
DATA: lo_calc TYPE REF TO ZCL_SALARY_CALCULATOR,
lv_tax TYPE P LENGTH 8 DECIMALS 2.
CREATE OBJECT lo_calc.
lv_tax = lo_calc->calculate_tax( iv_annual = 85000 ).
WRITE: / 'Calculated tax:', lv_tax.
Expected output: "Calculated tax: 12750.00" (15% of 85000).
Debugging in ABAP
The ABAP debugger is essential for every developer.
Key Debugger Commands:
/h - Start debugger for current session
F5 - Execute single line (step into)
F6 - Execute, treat as single step (step over)
F7 - Return from current routine
F8 - Continue until next breakpoint
BREAK-POINT. - Set a static breakpoint in code
Security Angle
ABAP programs run with the developer's authorization, but production systems restrict debug access. Durga Antivirus Pro uses ABAP-inspired logging patterns — every critical operation is logged with user, timestamp, and before/after values for complete audit trails.
Common Errors
1. SY-SUBRC not checked after Open SQL
Every Open SQL statement sets the system field sy-subrc. A value of 0 means success. Forgetting to check it leads to processing empty or incorrect data.
2. Internal table work area overwritten in loop
* WRONG: Work area ls_employee gets overwritten each iteration
LOOP AT lt_employees INTO ls_employee.
IF ls_employee-dept = 'FI'.
MODIFY lt_employees FROM ls_employee. " ls_employee may have changed
ENDIF.
ENDLOOP.
Use ASSIGNING FIELD-SYMBOL(<fs>) for direct modification instead.
3. Forgetting to refresh internal tables
Internal tables persist in memory. Always REFRESH or CLEAR them before a new selection if the variable is reused.
4. Short dump due to division by zero
ABAP short dumps terminate the program. Always check for zero before division:
IF lv_denominator <> 0.
lv_result = lv_numerator / lv_denominator.
ENDIF.
5. Incorrect use of MOVE-CORRESPONDING
MOVE-CORRESPONDING only copies fields with identical names. If source and target structures use different field names, the values are lost silently.
6. Overlooking authority checks
Production ABAP programs must include AUTHORITY-CHECK statements. Without them, users can execute programs that access data they should not see.
Practice Questions
What are the three types of ABAP internal tables? STANDARD (index-based), SORTED (sorted by key), HASHED (hash-based access).
What does SY-SUBRC = 0 mean after a SELECT statement? The SELECT executed successfully and returned data.
How do you declare a packed decimal field with two decimal places?
DATA lv_amount TYPE P LENGTH 8 DECIMALS 2.What is the difference between APPEND and INSERT with internal tables? APPEND adds a row to the end of a STANDARD table. INSERT adds a row at a specific position or in the correct sort order.
What happens if you divide by zero in ABAP? A runtime error (short dump) terminates the program. Always check for zero before division.
Challenge: Write an ABAP report that reads the MARA table (material master) for all finished products (material type FERT), joins with MAKT for descriptions, displays the data in a formatted list, and includes a summary at the bottom showing the total count. Include a parameter for the user to filter by material group (MATKL).
FAQ
Try It Yourself
Run a simple ABAP-style simulation using Python:
class SAPInternalTable:
def __init__(self):
self.rows = []
def append(self, row):
self.rows.append(row)
def where(self, **conditions):
results = SAPInternalTable()
for row in self.rows:
match = True
for key, value in conditions.items():
if row.get(key) != value:
match = False
break
if match:
results.append(row)
return results
def display(self):
if not self.rows:
print("No records found.")
return
headers = self.rows[0].keys()
header_line = " | ".join(f"{h:15}" for h in headers)
print(header_line)
print("-" * len(header_line))
for row in self.rows:
line = " | ".join(f"{str(row[h]):15}" for h in headers)
print(line)
# Test data - simulating ABAP internal table
employees = SAPInternalTable()
employees.append({"emp_id": "100001", "name": "Alice Johnson", "salary": 85000, "dept": "FI"})
employees.append({"emp_id": "100002", "name": "Bob Smith", "salary": 92000, "dept": "CO"})
employees.append({"emp_id": "100003", "name": "Carol Davis", "salary": 78000, "dept": "FI"})
print("=== All Employees ===")
employees.display()
print("\n=== FI Department ===")
fi_employees = employees.where(dept="FI")
fi_employees.display()
Expected output: Two formatted tables showing all employees and then filtered FI department employees.
What's Next
| Tutorial | What You'll Learn |
|---|---|
| SAP Explained — Complete Guide | Foundational SAP ERP concepts |
| SAP Modules Overview | Deep dive into each SAP module |
| Python Basics | Compare ABAP programming with Python |
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Updated 2026-06-22.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro