Introduction to the ABAP Programming Language - SAP BASIS

ABAP stands for Advanced Business Application Programming. Before release 4.0 of SAP R/3, the language was known as ABAP/4 (the 4 meant it was a fourth-generation language). With release 4.0B, back in 1998, the name was changed to simply ABAP because, although the language maintains most of the best fourth-generation language features and previous syntax and keywords, many object-oriented features have been incorporated in a move to make it a fully capable object-oriented language.

ABAP is the programming language that SAP has used to develop all the SAP R/3 business modules and applications, including the system management functions. It is available to customers and developers to extend SAP functionality for their particular needs. ABAP programs are created and maintained using the workbench tools discussed in previous sections.

Because of its origin and evolution, ABAP retains many of the fourth-generation language features such as the following:

  • It is based on structured programming methodologies, allowing modular programming and reutilization of code.
  • It resembles natural English language, making ABAP programs easy to read and understand
  • It's an interpretative language, not compiled. This nature facilitates testing and running earlier versions of programs without the need for constant compilation.
  • It can be used both from single report list programming (report programs) to complex transaction processing (dialog programs).
  • It's an event-driven language.
  • It's completely integrated with the rest of the workbench tools, such as the screen painter, the menu painter, the dictionary, and so on.
  • It supports multilanguage text elements. This means that you can create text elements in several languages without modifying the program source code.
  • Similar to many programming languages, it includes elements for
    • Variable and data type declarations
    • Flow control elements
    • Operational elements
    • Event elements
    • Functions and subroutines, which can be managed by a central library
  • It contains a subset of standard SQL statements enabling transparent database table access independently of the underlying database system being used.
  • It provides extensive functions for handling and operating with data types such as dates, strings, floating point numbers, and so on.

The ABAP object-oriented features started together with SAP's strategic and technological move toward working with business objects and the introduction of Business Application Program Interfaces (BAPIs) within the Business Framework architecture. The business objects are included within the ABAP Workbench in the business object repository (BOR). As an object-oriented language, the new ABAP, from release 4.x onward, incorporates technology principles such as inheritance, encapsulations, and polymorphism to provide the language with advantages such as lower maintenance costs and greater ease in reusing code.

Basic concepts and features of the ABAP object-oriented programming language are the same as those of other object-oriented languages. The most important are as follows:

  • A business object, or simply an object, represents a type of entity—a customer, a business unit, an account, and so on—containing all its properties. Every object has an identity that allows it to be distinguished from other objects.
  • Object classes, or simply classes, specify the structure of the objects belonging to a given class and the definition of the interfaces. Classes are useful for grouping objects with the same structure (attributes, methods, events). Generally, objects are defined using classes. The term instance is used for a specific object belonging to a class.
  • The object's attributes provide the object with its characteristics, describing the current object state.
  • Methods are the actions that can be performed with the object, indicating the behavior of the object.
  • Events are used so that the object can inform or be informed of any event or state change on the system to enable the system to react to those events.
  • Interfaces are another very important feature of objects. They define the method in which objects can be used independently of their internal implementation.

Basics of the Syntax of the ABAP Programming Language

In the next few sections we will introduce the basic elements of the ABAP programming language, with the objective of providing an overall description of the most important features of the ABAP syntax, such as data types, comments, naming conventions, operators, program flow control, and so on. We will also provide the classical example with the first ABAP program "Hello World."

ABAP Data Objects

When defining a data object within an ABAP program, there are two possibilities: using the keywords LIKE or the keyword TYPE. This type of definition is static. At runtime, we cannot modify the type of the data object. There are other possible syntax options, but they are not supported with ABAP objects. With the LIKE keyword, what we are indicating is that the type of the data object being defined is the same that the data object referenced in the clause. For example,

DATA field1 TYPE C.
DATA field2 LIKE field1.

In the example data object field2 is of type C (field of one position) because that is the type of data object field1. With the keyword TYPE we have three options for defining the data object:

  • Reference to a predefined ABAP type
  • Reference to a global type, that is, a type defined in the ABAP dictionary
  • Reference to a type defined with the TYPES keyword (local types)

There is also the possibility of defining complex structure types based on the elementary types.

Data Types

The following table shows the current ABAP predefined data types:

Data Types

The length of variable fields is established at runtime, and therefore it is not required to specify their length when defining the fields. It is necessary to specify the length for the fields of type "c," "n," "x," or "p." You cannot specify length for type "D" or "t" because the length is predefined for the standard type.

Type Groups

We can also define type groups in the ABAP dictionary. The main features of the type groups are the following:

  • The name of the type group can only have five characters.
  • Statement TYPE-POOL defines the group within the dictionary.
  • In a type group, we can define types and constants, but following the convention that the name bust start with a prefix including the same characters that the name of the group.
  • Constants are defined using the keyword CONSTANTS, like any other ABAP program.
  • Types are defined with keyword TYPES, like in any other ABAP program. In order to define this global types we can use predefined types or types that have been previously defined within the type group.
  • This is the only way to define global constants within SAP systems.

Type group

Type group

Maintaining a type group

Maintaining a type group

Integer Types

The features of the integer data type are the following:

  • The type identifier is "I."
  • Length is 4 bytes.
  • Value range goes from 2147843648 to -2147843648.
  • Arithmetic operations using integer types present a much better performance than operations performed with other numeric types.

This is the arithmetic used by integer number types:

  • All the auxiliary fields required to perform internal operations are also of the same type, integer.
  • Results are always rounded to an I type.
  • Arithmetic operators are as follows:

+ Addition
- Subtraction
* Multiplication
/ Division
DIV Integer division
MOD Rest of the integer division
** Exponentiation

  • You can also perform operations with the COMPUTE clause.

For example:

  • DATA integer TYPE i.
  • Integer = 4/10 "Result is 0.
  • Integer = 5/10 "Result is 1.

Packed Types

The main features and arithmetic of the packed number types are the following:

  • The length in the definition of the data object describes the length in bytes of the field.
  • The DECIMALS keyword established the number of decimal positions within the field, but it's only relevant when showing the field in the screen or printed.
  • The length allows lengths from 1 to 16 bytes. For this reason, packaged types can include numbers from 1 to 31 digits, because the last byte is used to store the sign (+ or -).
  • Logically, the number of decimal positions cannot be larger than the number of digits, so it never has more than 15 digits.

The arithmetic of the packed type is as follows:

  • The intermediate results from arithmetic operations are kept in fields with maximum length.
  • Type is the data type to be used with operations on business fields. For example:
  • DATA field(3) TYPE p DECIMALS 2 VALUE '234.20'

Floating Comma Numeric Type

The features of this type are the following:

In packed numbers the floating comma types are represented by addition of binary fractions. Both the exponent and the mantissa are stored in this way.

  • Packed numbers should not be used.

The arithmetic of this type is the following:

  • Fields of this type keep the approximation of the real number. Therefore, they should not be used when a exact precision is needed.
  • Packed numbers can only be used for approximations, because the number that the system stores might not be the one expected.
  • The main advantage of this type of fields is the range of values accepted, from 2,2250738585072014E-308 to 1,7976931348623157E+308.
  • Packed numbers is the only valid type for operations of aggregation of the SELECT statement.

For example:

  • DATA field(8) TYPE f.

String Character Type

Main features of the string type are the following:

  • The range of values accepted by the string character data type depends on the page code that the system is using. The code page contains all the characters supported by means of a code number. From transaction SPAD , you can validate the code page of your system.
  • The initial value of all string types is ***.

SPAD transaction

SPAD transaction

Comments in ABAP

Comments in ABAP are easy to include in the programs. The two types of comments that we can enter in our ABAP programs are

  • Line comments
  • Partial line comments

Line Comments ABAP provides a way to mark a line as a comment. The method consists of entering an asterisk sign (*) in the first column of the line. For example:

  • *&———————————————————————————————
  • *&
  • *&———————————————————————————————
  • *&
  • *&
  • *&———————————————————————————————

Partial Line Comments The partial line comments allow for making a comment only in a part of a line. In order to enter one of these types of comments, you have to code double quotes (") in any column of the line. The compiler will not take into consideration anything that is entered after the quotes. It is important to note that the ABAP comments are entered using quotes (") whereas literal values are identified by a single quote sign ('). For example:
• Select * from bkpf "Read all the columns from the table

Conventions in ABAP

The ABAP programming language is not restrictive when defining identifiers, but it has some rules that must be closely observed. The names of the components of classes (attributes, methods, events, and so on) can only be made up of characters A–Z, a–z, 0–9 and character _. Additionally, the first character cannot be a numeric digit. You cannot use special characters in the identifiers because, on some occasions, these have special meaning for the system. For example: The first DATA statement is wrong, whereas the second one is correct:

DATA: field-name TYPE, ...
1name TYPE ...
DATA: field_name TYPE, ...
name1 TYPE ...

Although there are other elements of the ABAP language less restrictive, it is recommended to use the same conventions as those used in ABAP objects.

ABAP Operators
Relational Operators for Any Data Type

Relational Operators for Any Data Type
Relational Operators for String Character Types

Relational Operators for String Character TypesRelational Operators for String Character Types
Relational Operations for Fields of Byte Type Data of type byte are of type X (fixed length) and XSTRING (variable length).
Relational Operations for Fields of Byte TypeRelational Operations for Fields of Byte Type
Relational Operators for Bit Masks

Relational Operators for Bit Masks
Other Relational Operators

Other Relational OperatorsOther Relational OperatorsOther Relational Operators
My First ABAP Program, Hello SAP World!!!

As it cannot be any other way, we will show a very easy example that will show the message Hello world!!! in the screen.

Hello SAP World result

Hello SAP World result

The ABAP Language Editor is included within the SAP systems, and it can be accessed from transaction SE38. When in main screen of transaction SE38, enter the name of the program (ZLESSON1) in the Program field, and click on the Create button.

Main Screen of transaction SE38

Main Screen of transaction SE38

In the next screen, enter the following information.

  • Title. Enter the program title.
  • Type. Enter Executable Program.
  • Status. Enter Test Program although we could enter some other types.

Program attributes

Program attributes

Leave default values in the rest of the fields and click on Save. Next the system will show up the ABAP Editor. In this next screen is where we write the ABAP code that will generate the output as we have shown previously.

ABAP source code

ABAP source code

After entering the code we must verify the syntax by clicking on the fm:see file in this email button, saving the changes in the program by clicking on the Save button, and then activating the program to be able to run it by clicking on the Activate button. We can run the program by clicking on the Direct Processing button. After pressing this button, and if there was no error on the code, we will get the output. Easy, isn't it?

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status