X

SAP ABAP Syntax

Welcome to the tutorial about SAP ABAP syntax. You will learn about SAP ABAP syntax for working with variables, declaration of data objects, operators, statement chaining, and event blocks statements. This tutorial is part of our free SAP ABAP training course.

ABAP Variables, Literals, Constants

Let’s start with discussing SAP ABAP syntax for variables, literals and constants. ABAP variables are called data objects and they are instances of certain ABAP data types. An ABAP data object can be defined by its name (which is a classic ABAP variable) or can be defined anonymously at runtime by using statement CREATE DATA. Here we are going to describe only named ABAP data objects.

The following name conventions in ABAP apply to variables:

  • Variable names cannot exceed 30 characters
  • Names can contain only characters “A” to “Z”, numbers from “0” to “9”, and underscores
  • Names must start either with a letter or an underscore
  • Names of predefined data types or data objects cannot be used for custom data objects
  • Field symbols (which are special objects) should be enclosed in angle brackets <>

Literals are variables that cannot be addressed by name, they are declared via the string in the source code of a program. Examples of literals:

DESCRIBE FIELD 15 TYPE i.
WRITE `This is John's bike`.
DATA(literal) = 'again' & 'and again' & 'and again'.

In the above example, the first line is the numeric literal and the last two are character literals. In the last example “&” literal operator is used to concatenate literals.

Constants are static data objects that cannot be changed during runtime with a predefined value. They are defined with a statement CONSTANTS and all data objects typing rules are applied to them as well. Example of constant is:

CONSTANTS lb TYPE p LENGTH 5 DECIMALS 8 VALUE ‘0.45359237'.

Declaration of Data Objects

Next, let’s talk about SAP ABAP syntax for declaration of data objects. ABAP variables can be declared using keyword or by its own. The latter case are literals and constants, which were described in the previous chapter.

Declaration of data objects is dependent on context and can be done through the following statements:

DATA – general way of data object declaration which is valid in any context.

CLASS-DATA – statement for declaring static data objects in classes. Their lifetime is dependent on object lifetime.

STATICS – allows declaration of static variables in procedures.

TABLES – declared work area, i.e. a flat data object from Data Dictionary. This statement if prohibited in classes and object-oriented context, and generally used for passing data between PBO/PAI fields.

Examples of data object declaration:

DATA: somevar TYPE c LENGTH 10.
CLASS-DATA: attribute1 TYPE matnr.
STATICS: stat TYPE n VALUE ‘100’.
TABLES: bseg, bsis, bsas.

ABAP Operators

Now, let’s talk about SAP ABAP syntax for operators. ABAP operators is the basic element of ABAP programming language, which implements data manipulations, data calculations and both memory and IO operations in ABAP. As a rule of thumb, they are usually categorized by operation type or operand type. There are the following groups of ABAP operators:

  • Declaration operators
  • Constructor operators
  • Assignment operators
  • Arithmetic operators
  • Bit operators
  • String operators
  • Relational operators
  • Boolean operators
  • Literal operator

Declaration Operators

Declaration operators declare variables and field-symbols in its position.

DATA – declares variables of any data type, which is visible within the current context of its position.

FIELD-SYMBOL – declares special ABAP type, field-symbol, which serves as a placeholder for existing data objects in memory and allow assigning them to a field-symbol.

Constructor Operators

Constructor operators declare variables and field-symbols in its position.

Assignment Operators

Assignment operators perform moving values of right side operand to left-side operand, possibly performing a conversion.

= – a simple assignment of a right operand to left operand

?= – a downcast operator, which performs assignment between two references. As a result, both of the references point to the same object.

There are also two legacy statements that can conduct assignments, they are MOVE and COMPUTE. MOVE contains part of the available semantics of =, and COMPUTE can be written in front of the any = statement but is simply ignored.

Arithmetic Operators

+ – addition of two numbers

– – subtraction of two numbers

* – multiplication of two numbers

/ – division of two numbers

DIV – integer part of the division

MOD – positive remainder of the division

** – raising to power

Bit Operators

BIT-AND – returns bit 1 only if two operands bits are true

BIT-OR – returns bit 1 if one of the operands bits is true

BIT-XOR – exclusive OR for bits

Relational Operators

= – tests equality

<> – tests not equality

< – tests if the first operand less than the second

> – tests if the first operand greater than the second

<= – tests if less or equal

>= – tests if greater or equal

BETWEEN – tests if operand belongs to a range

String and Literal Operators

&& – chains two character operators into string expression

& – joins two literals

Statement Chaining

Chained statements are the most amazing thing one can find in ABAP. If you have an action which should be done on several operands, you can unite these operations into one consecutive statement for the clarity and conciseness. For example, often this is used in data declaration. Instead of typing

DATA BEGIN OF mara.
DATA matnr TYPE mara-matnr.
DATA mtart TYPE mara-mtart.
DATA END OF mara.

you can use

DATA BEGIN OF mara.
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
DATA END OF mara.

Also, several consecutive calls of the same function modules

CALL FUNCTION spell_amount EXPORTING amount = '5'.
CALL FUNCTION spell_amount EXPORTING amount = '100'.
CALL FUNCTION spell_amount EXPORTING amount = '5000'.

can be replaced with

CALL FUNCTION spell_amount EXPORTING amount = : '5', '100', '5000'.

Event Blocks Statements

This is the most interesting part of SAP ABAP which a developer who came from other object-oriented programming language may not be aware of. Every ABAP executable program (type 1) has a predefined set of events. Event blocks divide ABAP program into parts and handle standard events in ABAP runtime. Every event block is introduced by an event keyword and lasts till the start of the next event block. SAP ABAP syntax for block statements is simple and here are the main ABAP events:

LOAD-OF-PROGRAM

This event is fired before all other events at the moment program is loaded into an internal session.

INITIALIZATION

During this event, global data of the program is initialized. This event is fired right after the LOAD-OF-PROGRAM. Usually this event is used for initializing selection screen values.

START-OF-SELECTION

This event defines the main processing block of any program. It holds all significant statements. One should note that each program with statements not assigned to any explicit processing block are assigned to implicit START-OF-SELECTION block.

END-OF-SELECTION

Deprecated statement used for legacy programs involving logical databases and which designated that selection from logical DB is ended.

AT SELECTION-SCREEN OUTPUT

This event is triggered by PBO (Process Before Output) block right before showing of the screen.

AT SELECTION-SCREEN …

This is a generic event for the whole variety of events fired by PAI (Process After Input) block of the screen. They are fired by user actions on screen and its controls.

Here are examples of these events (” – is a symbol for single-line comments):

AT SELECTION
SCREEN ON BLOCK p_block. “fired when user clicks block
AT SELECTION
SCREEN ON RADIOBUTTON GROUP p_group. “fired when radiobutton is changed

Sample Program

Collecting all the statements of SAP ABAP syntax we have learned in the previous sections let’s create a simple ABAP program and will analyze it into pieces.

REPORT zsample.
DATA:
gt_par TYPE abap_parmbind_tab,
gs_par LIKE LINE OF gt_par,
oref1 TYPE REF TO object,
s TYPE string.
PARAMETERS:
pa_class TYPE seoclsname AS LISTBOX VISIBLE LENGTH 30 DEFAULT 'LCL_NORMAL',
pa_name TYPE text30 DEFAULT sy-uname.
*---------------------------------------------------------------------*
* INITIALIZATION
*---------------------------------------------------------------------*
INITIALIZATION.
DATA:
gt_values TYPE vrm_values,
gs_values LIKE LINE OF gt_values.
gs_values-KEY = 'LCL_NORMAL'.
gs_values-TEXT = 'Normal class'.
*---------------------------------------------------------------------*
* START-OF-SELECTION
*---------------------------------------------------------------------*
START-OF-SELECTION.
INSERT gs_values INTO TABLE gt_values.
CREATE OBJECT oref1.
WRITE: / s.

Here in the program we have two event blocks involved: INITIALIZATION and START-OF-SELECTION. In the INITIALIZATION block gt_values and gs_values data objects are declared and filled. In the START-OF-SELECTION block gt_values internal table is filled with gs_values structure. Then, object oref1 is created and finally string s is being output.

The PARAMETERS statement is a special statement which defines implicit selection screen and we will discuss it in the future tutorials. Also in the INITIALIZATION block you can notice that the chain technique is utilized. A special * sign is used for marking blocks of comments in ABAP.

Did you like this tutorial? Have any questions or comments? We would love to hear your feedback in the comments section below. It’d be a big help for us, and hopefully it’s something we can address for you in improvement of our free SAP ABAP tutorials.

Navigation Links

Go to next lesson: SAP ABAP Dialog Programming

Go to previous lesson: SAP Data Objects

Go to overview of the course: SAP ABAP Training