Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท ABAP Basics

Your First ABAP Program

Write your first ABAP report: REPORT, DATA, WRITE, parameters, control flow and string handling, plus how SE38, ADT and the ABAP syntax work.

ABAP (Advanced Business Application Programming) is the language SAP's business applications are written in. Most of the standard ERP logic you have seen so far, from sales orders to invoice verification, is ABAP code running on the application server. Learning the basics lets you read that code, enhance it and write your own reports, interfaces and extensions.

ABAP only runs inside an SAP system, which cannot run on this site. All ABAP listings here are read-only, and the runnable blocks are JavaScript stand-ins that show the same logic. Always check ABAP examples in a real (trial or training) system.

Where ABAP code lives

You can write ABAP in two environments:

  • SAP GUI: SE38 (ABAP Editor) for programs, SE80 (Object Navigator) for everything, SE11 for dictionary objects, SE24 for classes, SE37 for function modules.
  • ABAP Development Tools (ADT): a plug-in for the Eclipse IDE, the modern standard. S/4HANA and ABAP Cloud development are done mostly here.

Programs are stored in the repository, assigned to a package and recorded in a transport request, as described in Transports and the System Landscape. Customer programs must start with Z or Y (the customer namespace), for example ZHELLO.

Syntax in one minute

  • Every statement ends with a period.
  • Keywords are case-insensitive; the convention is upper-case keywords.
  • A line comment starts with * in column 1, or with " after code.
  • Chained statements share a prefix with a colon: DATA: a TYPE i, b TYPE i.
  • Strings use 'text' (type c, fixed) or `text` (type string). The string template |text { var }| formats values.

Hello, ABAP

TEXT
REPORT zhello.

DATA lv_name TYPE string VALUE 'World'.

WRITE: / 'Hello,', lv_name.

REPORT names the program. DATA declares a variable with a type and a start value. WRITE sends output to the list; the slash / starts a new line. On a modern system you can also declare inline: DATA(lv_count) = 5. The compiler infers the type.

A program can have a selection screen, generated from PARAMETERS and SELECT-OPTIONS:

TEXT
PARAMETERS p_year TYPE i DEFAULT 2026.

START-OF-SELECTION.
  WRITE: / 'Year chosen:', p_year.

The event START-OF-SELECTION marks where the main processing begins. A variant (see Background Jobs, Monitoring and Housekeeping) can store the selection screen values for scheduled runs.

Elementary data types

ABAP type Meaning Example
i integer DATA n TYPE i.
p packed decimal (money, exact) DATA amt TYPE p LENGTH 8 DECIMALS 2.
f floating point rarely for business data
c fixed-length text DATA c(10) TYPE c.
string variable-length text DATA s TYPE string.
d date (YYYYMMDD) DATA d TYPE d.
t time (HHMMSS) DATA t TYPE t.
n numeric text (digits only) DATA n(6) TYPE n.
xstring raw bytes binary data

Use p for amounts: floating-point numbers would give rounding errors that auditors will not accept. The stand-in below shows why.

JS Stand-in: why money uses packed decimals

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Control flow

TEXT
IF lv_amount > 1000.
  WRITE / 'Large order'.
ELSEIF lv_amount > 100.
  WRITE / 'Normal order'.
ELSE.
  WRITE / 'Small order'.
ENDIF.

CASE lv_status.
  WHEN 'A'. WRITE / 'Active'.
  WHEN 'B'. WRITE / 'Blocked'.
  WHEN OTHERS. WRITE / 'Unknown'.
ENDCASE.

DO 3 TIMES.
  WRITE: / 'Round', sy-index.
ENDDO.

WHILE lv_n < 10.
  lv_n = lv_n + 1.
ENDWHILE.

Comparison operators: =, <>, <, >, <=, >=, and word forms EQ, NE, LT, GT. sy-index counts loop passes, and sy-subrc is the return code almost every statement sets: 0 means success, anything else means "not found" or "failed". Other system fields are sy-datum (date), sy-uzeit (time) and sy-uname (user).

JS Stand-in: control flow and sy-subrc

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Strings and templates

TEXT
DATA(lv_text) = |Order { lv_order } for { lv_customer }|.
DATA(lv_len) = strlen( lv_text ).
DATA(lv_up) = to_upper( lv_text ).
CONDENSE lv_text.
SPLIT lv_line AT ',' INTO lv_a lv_b.
IF lv_text CS 'Order'. ... ENDIF.      " contains string

Function calls use spaces inside the parentheses: strlen( lv_text ). That spacing is required, a common first-day error.

Debugging and testing

Put a breakpoint (click in the margin, or type /h in the command field to start the debugger) and step through with F5 (single step), F6 (execute), F7 (return) and F8 (continue). Watch variables and sy-subrc. Unit tests use ABAP Unit (CLASS ltcl_test DEFINITION FOR TESTING), covered in the class lesson, Modularization and Classes in ABAP.

Common mistakes

  • Forgetting the period at the end of a statement.
  • Using i for amounts, which loses decimals.
  • Assuming a failed statement raises an error: usually it only sets sy-subrc.
  • Naming objects without Z/Y and clashing with SAP's namespace.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does an ABAP statement end?

    Every ABAP statement ends with a period.
  2. 2. Which data type is best for a money amount?

    Packed decimals (type p) keep exact decimal digits, unlike floating point.
  3. 3. What does sy-subrc = 0 usually mean?

    0 means success; other values mean not found or an error.
  4. 4. Which prefix do customer programs normally use?

    Z and Y are reserved for customer objects, so they never clash with SAP delivery.

Summary

  • ABAP runs on the SAP application server; write it in SE38 or in ADT (Eclipse).
  • Statements end with a period; DATA declares variables, WRITE prints.
  • Use p for money; check sy-subrc after statements that can fail.
  • IF, CASE, DO and WHILE control the flow; string templates format text.
  • Programs are customer objects (Z/Y), packaged and transported.

Next: Data Types, Structures and Internal Tables