# ABAP programming

This (greatly undeveloped) article by Dan Polansky intends to provide a glimpse at ABAP syntax and features, where ABAP is a programming language used by SAP enterprise software. To hone ABAP skills, one typically needs to gain access to an SAP system with developer rights; thus, access to ABAP learning environment is not so straightforward as e.g. for the C, C++, Python or Java. The article is rather haphazard and incomplete, which it compensates a bit by the external links/further reading. The most comprehensive online source is probably the official ABAP Keyword Documentation. The Wikipedia article on ABAP currently reads like a guide and is a recommended complementary reading; some of its material may be reusable here.
Gambit Consulting indicates that ABAP syntax resembles the one of COBOL.
ABAP is peculiar by being SAP's proprietary language. Unlike for e.g. Java and C++, there are no multiple implementations of ABAP from different vendors; one can only get ABAP from SAP.

## Tasks suitable for ABAP

Programs in ABAP usually interact with database tables as storage rather than files. Moreover, they can interact with users via relatively plain text output. Also, they can interact with users via screens (also known as "dynpros") featuring input fields, value helps associated with fields, tables, etc.
It seems likely that ABAP is a bit like Java in some regards (especially J2EE) and that it is compiled into something like byte code. However, Java is much more versatile and has been used to create e.g. desktop applications (J2SE was used for that), a task for which ABAP is not suited at all.

## Who uses ABAP

SAP uses ABAP to write their software products, including what used to be SAP R/3 (a follow-on on the mainframe-based R/2), later SAP ERP, and SAP S/4HANA (business suite on the HANA in-memory database from SAP). ABAP is supported by something like ABAP runtime, probably written in C++. The most business application logic in SAP products is probably written in ABAP. Some SAP products or elements of products are probably written in Java. As a result, SAP is probably one of the largest employers of ABAP programmers on the planet. Statista.com indicates the number of SAP employees working in R&D to be over 36 000 as of Feb 2024, many of whom are likely to be ABAP developers.
SAP partners and customers use ABAP to write enhancements of the SAP-delivered software. They can also directly modify the SAP-delivered ABAP code, at the cost of greater upgrade pain. Moreover, they can use ABAP to create completely custom applications, using custom data models.

## Variable declaration

Data/variable declaration at a glance:
An issue: should it really be "DATA rcomplex1 TYPE REF TO complex1" rather than "DATA rcomplex1 LIKE REF TO complex1"?
Often, the type of a variable is not defined in terms of elementary built-in types as above but rather is taken from the data dictionary. Types in the data dictionary are trivial to share between ABAP programs; their names are global across ABAP programs.
Instead of TYPE, one can also use LIKE:
Further reading:
* DATA in ABAP Keyword Documentation, help.sap.com

## Type definition

Types can be taken from the data dictionary but they can be also defined in an ABAP program using TYPES keyword.
An example:
Further reading:
* TYPES in ABAP Keyword Documentation, help.sap.com

## Simple mathematics

Simple mathematics is supported as expected, including +, -, *, /.
There are built-in mathematical functions such as sin, cos, exp, long, etc.
Other mathematical functions, e.g. financial mathematics, are available by calling dedicated function modules and methods.
Further reading:
* Numeric Calculations in ABAP Keyword Documentation, help.sap.com

## Control structures

A glance at IF:
A glance at CASE is skipped and delegated to further reading.
A glance at DO (sy-index contains the current index of the loop):
A glance at WHILE:
There are analogues of C break and continue statements:
* EXIT (like C break): exits the loop, continuing with the first statement after the loop. The keyword has a different role outside of a loop.
* CONTINUE (like C continue): skips toward the end of the current iteration of the loop body.
There is an additional keyword not known from the C language:
* CHECK condition: if the condition is not met, does CONTINUE, that is, skips toward the end of the current iteration of the loop body.
There is a LOOP AT keyword for looping at internal tables, for which see section Internal tables.
Further reading:
* Control Structures in ABAP Keyword Documentation, help.sap.com
* IF in ABAP Keyword Documentation, help.sap.com
* CASE in ABAP Keyword Documentation, help.sap.com
* DO in ABAP Keyword Documentation, help.sap.com
* WHILE in ABAP Keyword Documentation, help.sap.com
* EXIT (loop) in ABAP Keyword Documentation, help.sap.com
* CONTINUE in ABAP Keyword Documentation, help.sap.com
* CHECK in ABAP Keyword Documentation, help.sap.com
* SAP ABAP - Loop Control, tutorialspoint.com
* SAP ABAP - Decisions, tutorialspoint.com

## Internal tables

Internal tables are variables and stand in contrast to database tables; the name "internal" points to that contrast. They are variable-length sequences of rows. The row is usually a fixed field sequence (in C terminology, think "struct"), but it can also be a single field. (In C++ terminology, if the internal table is a "standard table" of a fixed field sequence, one can think of it as a vector of struct.)
There are three types of internal tables:
* Standard tables. They are represented and organized by row index, much like C array or C++ vector.
* Sorted tables.
* Hashed tables.
Looping at internal tables:
Looping at internal tables with a modification:
There are more looping options including WHERE, etc.; see the links below.
Internal tables can have header lines, which SAP considers to be an obsolete feature. When they do, one can loop at the table without specifying the variable into which the row is copied; using the table name inside the loop with a hyphen to access a field automatically refers to the header line. For more, see the link below.
Further reading:
* Internal Tables in ABAP Keyword Documentation, help.sap.com
* LOOP AT itab, Basic Form in ABAP Keyword Documentation, help.sap.com
* Internal Tables with a Header Line in ABAP Keyword Documentation, help.sap.com
* SAP ABAP - Internal Tables, tutorialspoint.com
* SAP ABAP - Creating Internal Tables, tutorialspoint.com
* SAP ABAP - Populating Internal Tables, tutorialspoint.com
* 01_Internal_Tables.md, SAP-samples, github.com

## Field symbols

Field symbols are variables/symbols that do not have their own data space reserved. Rather, they point to data space reserved for a normal variable. Their names are like e.g. "<myfieldsymbol>", with the angle brackets mandatory.
They are somewhat like C++ references, but unlike them, field symbols can be unassigned and assigned again. They are somewhat like C pointers, but no dereferencing of a field symbol is required to access its value or data space.
An issue: field symbols can be used to operate dynamically on structures; an exposition is missing.
One can loop at an internal table assigning the current row/line into a field symbol:
On the other hand, one can equally well use REFERENCE INTO instead of ASSIGNING, doing without field symbols.
Further reading:
* FIELD-SYMBOLS in ABAP Keyword Documentation, help.sap.com
* field symbol (glossary) in ABAP Keyword Documentation, help.sap.com
* ASSIGN in ABAP Keyword Documentation, help.sap.com
* UNASSIGN in ABAP Keyword Documentation, help.sap.com
* IS ASSIGNED in ABAP Keyword Documentation, help.sap.com