Welcome to SAP ABAP Introduction tutorial. This is the first tutorial of our SAP ABAP training and we will begin learning basics of SAP ABAP here. You will learn about ABAP application area, ABAP naming conventions, ABAP data types, ABAP basic operators, and finally create your first HelloWorld program in SAP ABAP.
A Small Foreword
During its history, ABAP survived a vast number of releases and its syntax and principles were re-designed repeatedly, so it is practically impossible to cover all moments and techniques in a single tutorial. Therefore, in this tutorial we will stick to the most basic elements which are valid for any ABAP release. Release-specifics elements as well as obsolete ones will be followed by an additional note.
SAP ABAP Application Area
ABAP was developed by SAP SE company and stands for German abbreviation Allgemeiner Berichts-Aufbereitungs-Prozessor, which can be roughly translated as Advanced Business Application Programming, which generally makes sense. It is mainly used in several SAP products, main of which is SAP ERP, and used mostly for programming business applications. It is not used for system or low-level programming, unlike C or C++ and cannot be used standalone without SAP installation. ABAP can be generally classified as high-level programming language which experienced strong influence of Cobol language and even has some common syntax constructs.
ABAP mostly deals with common business entities which are predefined in SAP, such as order, quotation, delivery and so on, so it contains strong typing and a big toolset of predefined types, it also utilizes centralized reusable code library. As we speak about enterprise software, which prioritize safety, compatibility, scalability and reliability, all these concepts are fully implemented in ABAP language. Downward compatibility holds pride of place in ABAP and any piece of ABAP code can be reused continuously on any version ABAP on any platform with rare exceptions. It also has downsides though: maintenance of such systems often turns into pain, as one should be aware of all legacy code specifics and know obsolete syntax.
SAP ABAP Naming Conventions
All developments in ABAP are done in user namespaces Z and Y, they also called customer’s namespaces. What does it mean in practice? As SAP’s greatest value is stability and maintainability of code, it doesn’t allow to edit or create just everything you want, and all parts of any SAP system are strictly divided into standard and customer’s namespaces. Customers are allowed to create and edit object starting only from Z_ and Y_, and all other letters are reserved by SAP.
Other restrictions one can follow during the developments are:
- variable name cannot exceed 30 characters
- allowed characters for variables’ naming are letters A-Z, digits from 0 to 9 and underscore sign, all other characters are prohibited.
- the first character must be a letter or an underscore
- the names of predefined ABAP types or objects (function modules, function pools, tables, etc.) cannot be used by user
- fields symbols are enclosed in angle-brackets <>.
- Local classes are usually started from CL_<class name>, like CL_ORDER_FACTORY
- Interfaces are better to start from IF_<interface name>, like IF_ALERT_READER
- Parameters of methods should be named in a following way:
- importing parameters are named IM_<parname>, like IM_handler
- exporting parameters are named EX_<parname>, like EX_data
- changing parameters are named CH_<parname>, like CH_table
- resulting parameters are named RT_<parname>, like RT_result
- Field-symbols are usually marked as <FS_fieldsymbol name>, like <FS_WORKAREA>
- Local variables, structures and internal tables should be started in code from LV_, LS_ and LT_ respectively, like LV_MATNR, LS_AREA, LT_EKKO
- Global variables, structures and internal tables should be started in code from GV_, GS_ and GT_ respectively, like GV_FLAG, GS_PREF, GT_ALV
The alternative variants are not prohibited and above naming conventions are non-obligatory, however strongly recommended. One should always remember that SAP code is created once and is used for years, and it is better to stick to best-practice to ease the work for the man who will maintain your code.
Predefined SAP ABAP Data Types
All ABAP types are divided into language built-in (aka ABAP types) and Data Dictionary built-in (aka DDIC types). Here we will discuss the former ones.
ABAP types can be divided into numeric types, character-like types and byte-like types.
ABAP numeric types are b, s, i, p, decfloat16, decfloat34 and f. Their properties can be summarized in table:
|b||One byte||One byte integer (internal)|
|decfloat16||Eight bytes||Decimal floating point number with 16 decimal places|
|decfloat34||sixteen bytes||Decimal floating point number with 34 decimal places|
|f||Eight bytes||Binary floating point number with 16 decimal places|
|i||Four bytes||Four-byte integer|
|p||1 to 16 bytes||Packed number (implementation depends on hardware platform)|
|s||Two bytes||Two-byte integer (internal)|
The numeric data types are mainly utilized in arithmetic calculations. Data types b and s are internal data types and cannot be used by developer. Data type p is generic; therefore, its length is not the part of the description, also one should remember Fixed point arithmetic program setting must be set in order p decimals separators to be respected.
ABAP character-like types are c, string, n, d, t. Their properties can be summarized in table:
|c||1 to 65,535 characters||Text field|
|d||Eight characters||Date field (format YYYYMMDD)
Any eight characters that are valid as dates according to the calendar rules in the format “yyyymmdd”:
“yyyy”: 0001 to 9999, “mm”: 01 to 12, “dd”: 01 to 31
|n||1 to 65,535 characters||Numeric text
The only valid values are the digits 0 to 9.
|t||Six characters||Time field (format HHMMSS)
Any six characters that are valid as times in the 24-hour clock format “hhmmss”.
“hh”: 00 to 23, “mm”: 00 to 59, “ss”: 00 to 59.
The character-like data types are used for string processing. Data types n, d and t have special attributes for storing numbers, data and time, however types string and c are generic and can be used for anything. The valid values for types n, d and t are subsets of the value range, and specified in description.
ABAP byte-like types are x and xstring. Their properties can be summarized in table:
|x||1 to 65,535 bytes||Byte field|
The byte-like data types are mainly used for processing byte strings. One should remember that x types is generic and its length is not part of its description. Developer should also respect maximum size of string in current session.
Unlike other languages, ABAP types do not need to be initialized when declaring, they are initialized by default by initial values. Initial values are values that were assigned to variable by compiler before any operation was made on it. For most of the numeric type initial value is 0, and for character-like type it is a space.
SAP ABAP Basic Operators and Operands
ABAP operators can be divided into several groups, according their operand types.
Data declaration operators
DATA – declares local and global variables
TYPE – declares local data type
FIELD-SYMBOL – declares field-symbol, which is special ABAP type alike pointer in C++
= – assigns target field to source field
?= – downcast operator
- 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-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
= – tests equality
<> – tests not equality
< – tests if first operand less than the second
> – tests if first operand greater than the second
<= – tests if less or equal
>= – tests if greater or equal
BETWEEN – tests if operand belongs to range
String and literal operators
&& – chains two character operators into string expression
& – joins two literals
Operands in ABAP could be:
- Data objects defined in the current ABAP program
- Functions and expressions
- Types from the current program or from the repository
- Callable modules, such as ABAP programs from Data Dictionary, procedures (defined with FORM), dynpro screens, function modules and so on.
SAP ABAP Executable Program Types
Unlike other languages in ABAP programs (program objects) have strictly predefined types and one should choose before creation the type of the program to be developed.
There are many program types in SAP ABAP, and we will not examine all of them now, but will show you only the main type, which are used most frequently by developers. This is executable program (type 1) which also informally called report by many of old-school programmers. Why report? Because, historically ABAP language’s main domain was business reports, analytics and accounting calculations, which predefined most application aerated on ABAP, which included reporting functions. From those times term “report” inevitably followed ABAP programs.
What is the structure of typical report? Here it is:
That’s it, seriously. The one and only mandatory element for ABAP report is ABAP statement REPORT. You need no more statements, and you can freely create empty reports which would be fully valid programs. However, of course they would be insignificant in business sense without other useful statements.
So now we will enumerate essential blocks of a typical report and will try to create HelloWorld app.
- As we previously noted, REPORT statement denotes report start and gives ABAP compiler command to create report with given name.
- INTIALIZATION block. Is used only when complex data validations are necessary for selection screens. Otherwise it can be omitted.
- DATA: block which denotes data declaration statement. All data variables in a report are global so this statement could be placed in any place in the report before the first use, however, as a courtesy, it is usually placed at the very beginning.
- START-OF-SELECTION. This block is essential for any ABAP program of type 1 and it usually contains preparation statements, fetches data from DB and manipulate them. In trivial programs this block is redundant and can be securely omitted, as program treats no-block statements (i.e. which are not assigned to any block) as belonging to START-OF-SELECTION
- END-OF-SELECTION. This block now is rarely used, but it is recognized as a good ol’ style to specify it. It denotes end of DB selection statements, and contains usually post-processing of fetched data.
Let us create out first ABAP program aka HelloWorld. When creating program object in repository attention should be payed to following moments:
- Type of the program should be Executable program
- Checkboxes Unicode Checks Active and Fixed point arithmetic should be enabled
- Other attributes are not so important on that stage
Now we are ready to create our first ABAP program:
DATA: lv_string TYPE string.
lv_string = ‘Hello, World!’.
Here is our program output:
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.