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, adding comments to source code, perform arithmetic calculations, use system information, debug a program, create an ABAP list and process character strings using the ABAP language.

Basic Features of the ABAP Language

The ABAP language comprises individual sentences (statements). The important things to remember about ABAP statements are as follows:

  • ABAP code consists of individual statements
  • The first word in a statement is called an ABAP keyword
  • Each statement ends with a period
  • A space must separate two words
  • 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.

Data Objects in ABAP

Variables, Constants, and Literals

ABAP knows three types of data objects: variables, constants, and literals

A data object in ABAP code 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 that determines the information it can contain. The data type of an ABAP data object stays the same throughout a program execution.

Declaration of Variables

Variables are declared using the DATA , TYPE, and optionally the VALUE keyword

A variable in ABAP code is declared with the keyword DATA.

A DATA statement consists of the following parts:

DATA

Keyword DATA is followed by the variable's name. The name of a variable may be up to 30 characters long. It may contain 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 supports 13 built-in types or user defined types using the TYPES keyword or the ABAP Dictionary

ABAP offers the following sources of data types:

ABAP Built-in (Predefined ABAP Types)

ABAP has a set of 13 predefined data types for simple numeric, character-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 available throughout the system. ABAP Dictionary types not only define technical properties but also add semantic information – for example, labels. ABAP Dictionary types are particularly useful when implementing user interfaces.

ABAP Built-in (Predefined ABAP Types)

Examples of built-in 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 have a default length but allow 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 date. In ABAP, the date always has the format YYYYMMDD (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 converts 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 a 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 the length in characters; the run time system then assigns double the number of bytes 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 of a specific length that only contains digits. This field should contain a sequence of digits 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 a decimal place. Use this type for numbers with a decimal place or where the value range type I is not sufficient.

Use of Local Types

User-defined types are defined using the TYPES keyword

Example

Code Snippet
12
TYPES my_type TYPE p LENGTH 3 DECIMALS 2. DATA my_var TYPE my_type.

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

Example of a user-defined type in the ABAP Dictionary

In an SAP system, there are thousands of business entities, such as country codes, plants, material numbers, fiscal years, cost centers, and so on. Defining these entities in every program would be technically possible using the built-in ABAP types you have just seen. However, this would be extremely labor-intensive and error-prone. Instead, SAP provides the ABAP Dictionary, the central store for important data types, and the tool you use to create database tables.

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

When you press the F2 key to display the data type details, 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 can be defined using the CONSTANTS keyword

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 the 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. Initial values are normally zero for numeric types and a space for character types. There are no null values in ABAP.

Declaration of Literals

ABAP supports Number, Text, and String 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, defining and using constants is recommended.

ABAP has three types of literals:

  • Number Literals are integer numbers with or without signs. Number literals usually have data type I. If the value is too large for data type I, type p is used with a sufficient length and without a decimal place.
  • Text Literals are character types in a pair of single quotes ( ‘ ).
  • String literals are of type STRING and are in backquotes ( ` ). They should be used to provide values for string-typed data objects.

Chained Statements

Example of a chained TYPES statement

You can also define structured types in ABAP code 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 the 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

Example of a comment using the star sign symbol

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