Programming

ABAP Syntax: An Overview

The source code of an ABAP program is simply a collection of various ABAP statements that are interpreted by the runtime environment to perform specific tasks.

 

You use declarative statements to define data objects, modularization statements to define processing blocks, and database statements to work with the data in the database.

 

In this post, we’ll look at the basic ABAP syntax rules that every programmer should know. We’ll then look at the use of chained statements and comment lines.

 

Basic ABAP Syntax Rules

There are certain basic syntax rules that need to be followed while writing ABAP statements:

Rule 1

An ABAP program is a collection of individual ABAP statements that exist within the program. Each ABAP statement is concluded with a period (.), and the first word of the statement is known as a keyword.

Rule 2

An ABAP statement consists of operands, operators, or additions to keywords (see figure below). The first word of an ABAP statement is an ABAP keyword; the remaining can be operands, operators, or additions. Operands are the data objects, data types, procedures, and so on.

 

Various operators are available, such as assignment operators that associate the source and target fields of an assignment (e.g., = or ?=), arithmetic operators that assign two or more numeric operands with an arithmetic expression (e.g., +, -, *), relational operators that associate two operands with a logical expression (e.g., =, <, >), and so on. Each ABAP keyword will have its own set of additions.

Rule 3

Each word in the statement must be separated by at least one space.

Rule 4

An ABAP statement ends with a period, and you can write a new statement on the same line or on a new line. A single ABAP statement can be extended over several lines.

Rule 5

ABAP code isn’t case-sensitive.

Additional Information

In the figure below, the program shown consists of three ABAP statements written across three lines. The first word in each of these statements (REPORT, PARAMETERS, and WRITE) is a keyword. As you can see, each statement begins with a keyword and ends with a period. These contain a:

  1. Keyword
  2. Operand
  3. Addition

In addition, each ABAP word is separated by a space.

 

ABAP Statement

 

You can write multiple statements on one line or one statement can extend over multiple lines. Therefore, if you want, you can rewrite the code above as shown:

 

REPORT ZCA_DEMO_PROGRAM. PARAMETERS p_input(10) TYPE c. WRITE p_input RIGHTJUSTIFIED.

 

However, to keep the code legible, we recommend restricting your program to one statement per line. In some cases, it’s recommended to break a single statement across multiple lines, for example:

 

SELECT * FROM mara INTO TABLE it_mara WHERE matnr EQ p_matnr.

 

The preceding statement may be written as shown below to make it more legible.

 

SELECT * FROM mara

         INTO TABLE it_mara

         WHERE matnr EQ p_matnr.

 

Chained Statements

If more than one statement starts with the same keyword, you can use a colon (:) as a chain operator and separate each statement with a comma. These chained statements help you avoid repeating the same keyword on each line.

 

For example:

 

DATA v_name(20) TYPE c.

DATA v_age TYPE i.

 

can also be written as:

 

DATA : v_name(20) TYPE c,

            v_age TYPE i.

 

End the last statement in the chain with a period. Chained statements aren’t limited to keywords; you can put any identical first part of a chain of statements before the colon and write the remaining parts of the individual statements separated by a comma, for example:

 

v_total = v_total + 1.

v_total = v_total + 2.

v_total = v_total + 3.

v_total = v_total + 4.

 

can be chained as:

 

v_total = v_total + : 1, 2, 3, 4.

 

As rule 5 stated, ABAP code isn’t case-sensitive, so you can use either uppercase or lowercase to write ABAP statements. We recommend writing keywords and their additions in uppercase and using lowercase for other words in the statement to make the code more legible.

 

Comment Lines

To make your source code easy to understand for other programmers, you can add comments to it (see below). Comment lines are ignored by the system when the program is generated, and they’re useful in many ways.

 

DATA f1 TYPE c LENGTH 2 VALUE 'T3'.

DATA f2 TYPE n LENGTH 2.

*This is a comment line

f2 = f1.

WRITE f2. "This is also a comment line

4

There are two ways to add comment lines in source code:

  • You can enter an asterisk (*) at the beginning of a line to make the entire line a comment.
  • You can enter a double quotation mark (") midline to make the part of the line after the quotation mark a comment (this is called an in-line comment).

You can comment (i.e., set as a comment) on a block of lines at once (a multiline comment) by selecting the lines to be commented on and pressing (Ctrl) + (<) on the keyboard. Similarly, to uncomment (i.e., set as normal code) a block of lines, you can select the lines and press (Ctrl) + (>).

 

Alternatively, you can also use the context menu to comment or uncomment code. To comment a line of code or a block lines, select the code, right-click, and select the appropriate option from the Format context menu. This helps you avoid the tedious job of adding asterisks manually at the beginning of each line.

 

Conclusion

This post provided a better understanding of basic ABAP syntax rules and chaining

ABAP statements. You’re now ready to focus on more advanced ABAP concepts, such as keywords used in ABAP.

 

Editor’s note: This post has been adapted from a section of the book Complete ABAP by Kiran Bandari.

Recommendation

Complete ABAP
Complete ABAP

Whether you're new to ABAP, or you’ve been developing for years, this is the resource for you. Build your foundation with basic programming concepts and tools, then take it to the next level with modifications and enhancements for your ABAP code. Design reports and screens, develop applications using dialog programming, create interfaces, and more. Your ultimate reference guide to the world of ABAP is here!

Learn More
SAP PRESS
by SAP PRESS

SAP PRESS is the world's leading SAP publisher, with books on ABAP, SAP S/4HANA, SAP CX, intelligent technologies, SAP Business Technology Platform, and more!

Comments