Introducing ABAP Syntax

Objectives

After completing this lesson, you will be able to:

  • Explain the basics of ABAP syntax
  • Define data types, variables, constants and literals, and chained statements

The Basics of ABAP Syntax

Overview

By the end of this module, the beginner programmer will work with the basics of ABAP syntax, such as data types, variables, constants, and literals, chained statements, add comments to source code, define text symbols, perform arithmetic calculations, use system variables, debug a program, create an ABAP list and process character strings using the ABAP language.

Basic Features of the ABAP Language

ABAP programs are comprised of individual sentences (statements). The important things to remember about ABAP statements are as follows:

  • ABAP programs consist of individual statements
  • The first word in a statement is called an ABAP keyword
  • Each statement ends with a period
  • Two words must be separated by a space
  • Statements can be indented to enhance the readability of the code
  • May contain additions and operands (depending on the keyword used)
  • Can span multiple lines
  • Keywords, additions, and operands can use upper or lowercase
  • The ABAP runtime system does not differentiate between cases. It is customary to write keywords and their additions in uppercase letters and operands in lowercase lower

Data Objects in ABAP

Variables, Constants, and Literals

A data object in an ABAP program represents a reserved section of the program memory. ABAP knows three types of data objects: variables, constants and literals.

Variables
A variable is a data object with content that can change during runtime. A variable is identified by a name. The name is also used to address the data object at runtime. The starting value of an ABAP variable is well defined.
Constants
Constants are like variables. But, in contrast to variables the value is hard coded in the source code and must not change during runtime. Like variables, constants have a name by which they can be reused.
Literals

The value of literals is also hard coded in the source code. In contrast to constants, literals do not have a name. Because of that you cannot reuse a literal. Only use literals to specify the values for constants and the starting values for variables.

ABAP data objects are always typed: Every data object is based on a data type which determines the kind of information they can contain. The data type of an ABAP data object stays the same throughout a program execution.

Declaration of Variables

A variable in an ABAP program is declared with the keyword DATA.

A DATA statement consists of the following parts:

DATA

Keyword DATA is followed by the name of the variable. The name of a variable may be up to 30 characters long. It may contain the characters A-Z, the digits 0–9 and the underscore character. The name must begin with a letter or underscore.

TYPE
The type of the variable is specified after the addition TYPE. In the example, built-in types I (for integer numbers) and string (character string with variable length) are used.
VALUE
Addition VALUE is optional, and you can use it to specify a start value for the variable. If VALUE is missing, the variable is created with an initial value that depends on the technical type of the variable.

Use of Data Types

ABAP offers the following sources of data types:

ABAP Built-in (Predefined ABAP Types)

ABAP has a set of 13 predefined data type for simple numeric, char-like, and binary data objects.

TYPES Statements

Statement TYPES allows you to define predefined data types and reuse them in different places, depending on the location of the definition.

ABAP Dictionary
The ABAP dictionary is a part of the ABAP Repository. Among other things, it manages global data types, which are available throughout the system. ABAP Dictionary types not only define technical properties, but they also add semantic information – for example, labels. ABAP Dictionary types are particularly useful when implementing user interfaces.

ABAP Built-in (Predefined ABAP Types)

Some important standard ABAP types (built-in data types) are listed in the figure. The ABAP types are categorized as either complete or incomplete data types. Complete data types already contain type-specific, fixed-length specifications. Incomplete data types contain a default length but allow for a different length to be specified. In the case of type P, you may also specify a number of decimal places. 

Complete ABAP Types

TYPE STRING

A field of type string is a character-like field of variable length. The ABAP runtime system allocates and releases memory to optimize the management of string variables. You cannot influence this behavior directly. The maximum length of a string is governed by settings that the system administrator makes. However, in practice, we talk about strings being unlimited in length.

TYPE I

A field of type I is a numeric field that contains a whole number. The system allocates 4 bytes (32 bits) for such a field that allows for values between -2^31 and +2^31.

TYPE D

A field of type D represents a data. In ABAP, the date always has the format YYYMMDD (without separators). The system converts this format according to the current locale before the value is displayed on the user interface. Likewise, when the user enters a date, the systems convert it into the ABAP format before you start to process it.

TYPE T

A field of Type T represents a time. In ABAP, this has the format HHMMSS (without separators in 24-hour format). If the current locale used 12-hour format, the system converts the values automatically.

Incomplete ABAP Types

TYPE C
A field of type C is a character-like field of specific length. You specify is length in characters; the run time system then assigns double the number of bytes in order to accommodate the field. You use this type when a fixed length is important.
TYPE N

A field of type N is a character field or a specific length that only contains digits. This field should contain a sequence of digits that you do not want to regard as a number and perform calculations with – for example, this could be a personnel number or a cost center.

TYPE P

A field of type P (P for ‘packed number’) is a field that contains a numeric value with a specified number of digits and decimal places. Use this type for numbers with decimal places or where the value range type I is not sufficient.

ABAP Standard Data Types – Character-like and Numeric Data Types

ABAP Standard Data Types

TypeMeaningUse
cFixed-length character stringAny characters. Has a fixed length.
dDateHolds dates in the format YYYYMMDD.
tTimeHolds times in the format HHMMSS.
Numeric Types
iIntegerWhole numbers (no decimal places).
pPacked NumberDecimal numbers.

Use of Local Types

Instead of using built-in-types in the DATA statement directly you can use statement TYPES to define the type first. You can then use the type in a DATA statement after the TYPE addition.

Use of Global Types – ABAP Dictionary

In an SAP system, there are thousands of business entities, such as country code, plant, material number, fiscal year, cost center, and so on. Technically, it would be possible to define these entities in every single program using the build-in ABAP types that you have just seen. However, this would be extremely labor-intensive and error prone. Instead, SAP provides the ABAP Dictionary, which is the central store for important date types and the tool that you used to create database tables.

In the ABAP Dictionary, single business entities are described by data elements. In the example variable, variable airport is typed with data element /DMO/AIRPORT_ID.

When you press the F2 key to display the details of the data type you can see that technically this type is a character of length 3. In addition, the data element provides the description "Flight Reference Scenario: Airport ID" and the four field labels of different length.

When you press the F3 key to navigate to the definition of the type, a new view opens with the editor for data elements.

Constants and Literals

Declaration of Constants

Constants

A constant is a data object with a hard-coded value that must not be changed during runtime. Any write access to a constant will lead to a syntax error.

In ABAP, you declare a constant using keyword CONSTANTS. A CONSTANT statement consists of the same parts as a DATA statement. The only difference is that the VALUE addition is mandatory.

You can use the VALUE addition in the special form VALUE IS INITIAL, if the value of the constant should be the type-specific initial value.

Declaration of Literals

Literals

Literals are anonymous data objects with a hard-coded value. Literals are often used to define non-initial values for constants and non-initial starting value for variables.

Technically, you can use literals anywhere in your code. To support readability and maintainability, it is recommended to define and use constants instead.

ABAP knows three types of literals:

  • Number Literals are integer numbers with or without sign. Number literals usually have data type I. Only if the value is too large for data type I, type p is used with a sufficient length and without decimal places.
  • Text Literals are character strings in a pair of single quotes ( ‘ ). String literals are of type STRING. They should be used to provide values for string typed data objects.
  • String Literals are character strings in a pair of singe quotes ( ‘ ).  String literals are of type STRING. They should be used to provide values for string typed data objects.

Chained Statements

You can also define structured types in ABAP program using the TYPES statement. The structure definition begins with the statement TYPES BEGIN OF, structure type name> and ends with TYPES END OF <structure type name>. In between, you name each component and specify its type in an additional TYPES statement.

A compact form uses keyword TYPES only once, followed by a colon ( : ). The BEGIN OF addition, the END OF addition and the component definitions in between are then separated by commas.

This is referred to as a chain statement.

Note
In the past, chain statements were used a lot in ABAP. These days, they are only recommended to combine statements that belong closely together.

Comments in ABAP

A comment is an explanation that is added to a program to help others understand the code. Comments are a piece of source code that is ignored by the compiler or interpreter.

In ABAP, there are two different ways to define a piece of source code as a comment:

  • The star sign (*) in the first column identifies the entire line as a comment
  • The double quote sign (") identifies the rest of the line, that is, the code to the right as a comment

Log in to track your progress & complete quizzes