SAP ABAP Syntax

SAP ABAP syntax defines how statements, keywords, comments, data declarations, control blocks, messages, and database access are written in an ABAP program. The most important rule for beginners is simple: an ABAP statement starts with a keyword and ends with a period.

This tutorial explains the basic SAP ABAP syntax used in report programs and executable programs. It also shows where beginners commonly miss periods, confuse comments, or write older syntax where modern ABAP syntax is easier to read.

SAP ABAP Syntax Rules at a Glance

  • Each ABAP statement normally begins with an ABAP keyword such as REPORT, DATA, WRITE, IF, LOOP, SELECT, or MESSAGE.
  • Each ABAP statement must end with a period (.). Missing the final period is one of the most common syntax errors.
  • ABAP is not case-sensitive for keywords, but uppercase keywords are commonly used in examples and training material.
  • Multiple spaces and line breaks are allowed, but the period decides where the statement ends.
  • A single ABAP statement can continue across multiple physical lines until the ending period is reached.

Statement

SAP ABAP program is a collection of statements, each statement begins with a keyword and ends with a period.The first line of statement start with a word PROGRAM or REPORT and followed by the name.

In practice, report programs commonly start with REPORT. The PROGRAM statement is also valid in ABAP source programs, but most beginner examples use REPORT for executable report programs.

The syntax is

PROGRAM [Program_Name].

[Statements.].

Statement is a combination of keywords, operators, data types, constants, etc. Below an example of ABAP syntax of report statement type.

REPORT Z_Report123

Write 'Hello World'.

The example above shows the basic idea, but in a real ABAP editor you should also end the REPORT statement with a period. The corrected beginner-friendly form is shown below.

</>
Copy
REPORT z_report123.

WRITE 'Hello World'.

ABAP Statement Structure: Keyword, Operands, and Period

An ABAP statement is made of a keyword and the values or additions needed by that keyword. These values may be variables, constants, literals, operators, internal tables, database tables, or additions such as INTO, WHERE, FROM, and ORDER BY.

</>
Copy
DATA lv_name TYPE string.

lv_name = 'TutorialKart'.

WRITE lv_name.

Here, DATA declares a variable, the assignment statement stores a value, and WRITE displays the value in the report output.

SAP ABAP Keywords Used in Basic Syntax

Keywords

The ABAP statement starts with a keyword and that determines the meaning of ABAP statement. The different types of keywords are

  • Declarative keywords : -These keywords are used to create variable constants and data types. for e.g. Constants, data, types, etc.
  • Control keywords : – These keywords are used to control flow of ABAP program. For e.g. If, Case, Do, While, etc
  • Event keywords : – These keywords are used to define blocks (set of statements) externally. For e.g. Top – of – page, End – of – page.
  • Operational keywords – These keywords are used to process the data in an ABAP program. For e.g. Add, Subtract, divide, multiply, etc.

The same keyword groups can be understood more clearly with common ABAP examples:

ABAP keyword groupPurpose in ABAP syntaxCommon keywords
Declarative keywordsDefine variables, constants, field symbols, and data typesDATA, CONSTANTS, TYPES, FIELD-SYMBOLS
Control keywordsControl decision-making and repeated executionIF, ELSE, CASE, DO, WHILE, LOOP
Event keywordsStart processing blocks in classic executable reportsINITIALIZATION, START-OF-SELECTION, END-OF-SELECTION, TOP-OF-PAGE
Operational keywordsRead, change, calculate, display, or transfer dataADD, SUBTRACT, MOVE, WRITE, SELECT, APPEND

Declaring Variables in SAP ABAP Syntax

Variables are declared with DATA. Constants are declared with CONSTANTS. A clear naming convention makes ABAP code easier to read; for example, many projects use prefixes such as lv_ for a local variable and lt_ for an internal table.

</>
Copy
DATA lv_count TYPE i.
DATA lv_price TYPE p DECIMALS 2.
DATA lv_text  TYPE string.

CONSTANTS lc_company TYPE string VALUE 'SAP'.

In modern ABAP, inline declarations are also common when the type can be inferred from the context.

</>
Copy
DATA(lv_message) = 'Hello ABAP'.

WRITE lv_message.

Control Flow Syntax in SAP ABAP: IF, CASE, DO, WHILE, and LOOP

Control statements help an ABAP program choose a path or repeat a block of statements. The condition starts with a control keyword and the block ends with a matching closing keyword.

</>
Copy
DATA lv_score TYPE i VALUE 75.

IF lv_score >= 50.
  WRITE 'Pass'.
ELSE.
  WRITE 'Fail'.
ENDIF.

Use CASE when one value has several possible matches.

</>
Copy
DATA lv_grade TYPE c LENGTH 1 VALUE 'A'.

CASE lv_grade.
  WHEN 'A'.
    WRITE 'Excellent'.
  WHEN 'B'.
    WRITE 'Good'.
  WHEN OTHERS.
    WRITE 'Needs improvement'.
ENDCASE.

Use LOOP AT to process rows from an internal table.

</>
Copy
DATA lt_names TYPE STANDARD TABLE OF string.

APPEND 'Asha' TO lt_names.
APPEND 'Ravi' TO lt_names.

LOOP AT lt_names INTO DATA(lv_name).
  WRITE / lv_name.
ENDLOOP.

Comments in SAP ABAP Syntax

Comments

Comments are descriptive text between the lines of ABAP program. Comments are not executed during run time or execution. Symbol * is used in first column of sentence. The different types of comments that are used in ABAP programming are

  • Line commenting
  • Block commenting
  • Partial commenting
*----------------------------------------------------*
*& Compilation& of ZP001......
*----------------------------------------------------*

ABAP has two common comment styles. An asterisk (*) in the first column comments the whole line. A double quotation mark (") starts an end-of-line comment from that position.

</>
Copy
* This is a full-line comment.
DATA lv_total TYPE i VALUE 10. "This is an end-of-line comment

For readability, avoid long comments that repeat the code. Use comments to explain business meaning, assumptions, and non-obvious logic.

SAP ABAP Message Syntax

Messages

Message class are user defined messages, so we can create our own messages to display on the screen. We can create up to 1000 messages in a single message class and message ID numbers are ranges from 0 to 999. The created messages can be called from program using statement “MESSAGE”.

ABAP Syntax is

MESSAGE <type_of_message> <message_id> (message_class).

A message statement usually contains a message type and a message number from a message class. Common message types include success, information, warning, error, abort, and exit. In ABAP examples, they are often written as S, I, W, E, A, and X.

</>
Copy
MESSAGE e001(zmsg_class).

The exact message class and message number must already exist in the SAP system. In application code, use message types carefully because error and abort messages can stop or interrupt processing.

Open SQL Syntax in SAP ABAP Programs

ABAP is a programming language, but it also has Open SQL statements for reading and changing database data through SAP’s database abstraction layer. This is why ABAP may look partly similar to SQL, but ABAP and SQL are not the same language.

</>
Copy
SELECT carrid, connid, cityfrom, cityto
  FROM spfli
  INTO TABLE @DATA(lt_flights)
  UP TO 10 ROWS.

LOOP AT lt_flights INTO DATA(ls_flight).
  WRITE: / ls_flight-carrid, ls_flight-connid, ls_flight-cityfrom, ls_flight-cityto.
ENDLOOP.

The @ symbol is used to mark ABAP host variables in newer Open SQL syntax. In older ABAP examples, you may see similar database statements without the @ marker.

Old ABAP Syntax and New ABAP Syntax Differences

Many SAP systems contain older ABAP code, while newer training material often shows modern ABAP syntax. Beginners should be able to recognize both styles.

Older ABAP styleModern ABAP styleWhat changed
DATA lv_text TYPE string.DATA(lv_text) = 'ABAP'.Inline declaration can infer the type from the assigned value.
READ TABLE lt_tab INTO ls_row INDEX 1.DATA(ls_row) = lt_tab[ 1 ].Table expressions can read entries more directly in suitable cases.
CONCATENATE a b INTO c.c = |{ a } { b }|.String templates make many text expressions easier to read.
MOVE a TO b.b = a.Direct assignment is shorter and common in modern code.

When maintaining existing SAP applications, follow the style used by the project unless your team has a clear modernization rule. Mixing too many styles in one program can make the code harder to review.

Common SAP ABAP Syntax Errors for Beginners

  • Missing period: Every ABAP statement must end with a period.
  • Missing closing keyword: IF needs ENDIF, CASE needs ENDCASE, and LOOP needs ENDLOOP.
  • Wrong comment position: * works as a comment only when it starts in the first column.
  • Incorrect text literal: Character literals are usually written inside single quotes, such as 'Hello'.
  • Incorrect database host variable: Newer Open SQL examples often require @ before ABAP variables.

Complete Beginner SAP ABAP Syntax Example

The following small report combines several basic syntax rules: report declaration, data declaration, control flow, loop processing, comments, and output.

</>
Copy
REPORT z_basic_syntax_demo.

DATA lt_numbers TYPE STANDARD TABLE OF i.

APPEND 10 TO lt_numbers.
APPEND 20 TO lt_numbers.
APPEND 30 TO lt_numbers.

LOOP AT lt_numbers INTO DATA(lv_number).
  IF lv_number >= 20.
    WRITE: / 'Number:', lv_number.
  ENDIF.
ENDLOOP.

Expected report output:

Number: 20
Number: 30

How to Write SAP ABAP Code in a Simple Order

For a small beginner report, write ABAP code in this order:

  1. Start the program with REPORT program_name.
  2. Declare variables, constants, types, and internal tables.
  3. Add selection-screen parameters if the report needs user input.
  4. Write processing logic using IF, CASE, LOOP, and database statements where required.
  5. Display output with WRITE or pass data to a proper UI/list framework used in your SAP system.
  6. Activate the program and correct syntax errors shown by the ABAP editor.

SAP ABAP Syntax FAQ

What is the syntax in SAP ABAP?

SAP ABAP syntax is the set of rules used to write ABAP statements. A statement usually starts with a keyword such as DATA, WRITE, IF, or SELECT and ends with a period. ABAP syntax also defines how comments, variables, messages, loops, and database access are written.

Is SAP ABAP full of coding?

Yes. SAP ABAP is a programming language, so ABAP development involves coding. However, beginners usually start with simple report programs, data declarations, conditions, loops, and Open SQL before moving to classes, function modules, enhancements, and SAP application development.

How do I write SAP ABAP code?

Start with a program statement such as REPORT z_demo., declare the required data with DATA, write logic using ABAP keywords, end every statement with a period, and activate the program in the ABAP development environment to check syntax errors.

Is SAP ABAP similar to SQL?

ABAP is not the same as SQL. ABAP is a full programming language used in SAP systems. It includes Open SQL statements such as SELECT, INSERT, UPDATE, and DELETE for database work, so some parts of ABAP can look similar to SQL.

Why does an ABAP statement end with a period?

The period tells the ABAP compiler where the statement ends. Because one ABAP statement can continue across more than one physical line, the period is required to separate one complete statement from the next.

Editorial QA Checklist for This SAP ABAP Syntax Tutorial

  • Confirm every ABAP example ends complete statements with a period, except where an existing legacy sample is intentionally kept unchanged.
  • Check that code blocks showing ABAP syntax use the correct PrismJS-compatible language class and that output-only blocks use the output class.
  • Verify that beginner explanations distinguish ABAP programming syntax from Open SQL syntax.
  • Keep the message syntax examples generic unless the message class and message number are available in the target SAP system.
  • Review older and newer ABAP syntax examples for the SAP release level used by the reader or training system.