ABAP syntax


This brief description of the ABAP syntax begins inevitably with the ubiquitous “Hello World” program.”Helloworld”

PROGRAM TEST.
WRITE ‘Hello World’.

This example contains two statements, one on each line. The keywords are PROGRAM and WRITE. The program displays a list on the screen. In this case, the list consists of the line “Hello World”.

abap Comments


BAP has 2 ways of defining text as a comment.

An asterisk (*) in the leftmost column of a line makes that line a comment. A double quotation mark (>>”<<) anywhere on a line makes the rest of that line a comment.
Example

***************************************
** Program: BOOKINGS **
** Author: Joe Byte, 07-Jul-2007 **
***************************************

REPORT BOOKINGS.

* Read flight bookings from the database
SELECT * FROM FLIGHTINFO
WHERE CLASS = ‘Y’ “Y = economy
OR CLASS = ‘C’. “C = business
(…)

abap DATA and TYPES


It is a special strength of ABAP that you can define a great variety of data types and objects that span the spectrum from very elementary data types to very complex and dynamic types. Consequently, the subject of ABAP declarations is quite extensive.

ABAP accepts all data types defined in the SAP dictionary or in Type Pools. Types can also be defined inside the program itself. Object numeric (I for integer, F for floating point), packed decimal (P), character (C or N, where the N type is used for numeric strings that can be used in computation) or hexadecimal (X). Date fields (type D) and time fields (type T) have a “dual” nature; in an input/output context they behave like strings, but in a computational context they are numeric integers. This makes date and time calculations extremely easy. For example:

DATESENT = ‘20070901’.
VALIDTO = DATESENT + 60.
WRITE: ‘Offer is valid until’, VALIDTO DD/MM/YYYY.

In this example, a string literal representing September 1, 2007 is assigned to DATESENT. DATESENT is then used in a numeric calculation to produce another data field, VALIDTO. VALIDTO is then output as a string. The optional “DD/MM/YYYY” modifier displays the date in a predefined format, here “31/10/2007”. Without the modifier the date would display as “20071031”.
Data and Types

All ABAP variables must be explicitly declared in order to be used. The convention is for all declarations to be at the top of the program, or subroutine. The declaration consists of the name, type, length (where applicable), additional modifiers (e.g. the number of implied decimals for a packed decimal field) and optionally an initial value:

* Primitive types:
DATA: COUNTER TYPE I,
VALIDITY TYPE I VALUE 60,
TAXRATE(3) TYPE P DECIMALS 1,
LASTNAME(20) TYPE C.

* Dictionary types:
DATA: ORIGIN TYPE COUNTRY.

* Internal table:
DATA: T_FLIGHTS TYPE TABLE OF FLIGHTINFO,
T_LOOKUP TYPE HASHED TABLE OF FLT_LOOKUP.

* Objects:
DATA: BOOKING TYPE REF TO CL_FLT_BOOKING.

Notice the use of the colon to chain together consecutive DATA statements.

ABAP statements


The first element of an ABAP statement is the ABAP keyword. This determines the category of the statement. The different statement categories are as follows:

Declarative statements

These statements define data types or declare data objects which are used by the other statements in a program or routine. The collected declarative statements in a program or routine make up its declaration part.

Examples of declarative keywords:

TYPES, DATA, TABLES

Modularization statements

These statements define the processing blocks in an ABAP program.

The modularization keywords can be further divided into:

· Event Keywords

You use statements containing these keywords to define event blocks. There are no special statements to conclude processing blocks – they end when the next processing block is introduced.

Examples of event keywords are:

AT SELECTION SCREEN, START-OF-SELECTION, AT USER-COMMAND

· Defining keywords

You use statements containing these keywords to define subroutines, function modules, dialog modules and methods. You conclude these processing blocks using the END- statements.

Examples of definitive keywords:

FORM ….. ENDFORM, FUNCTION … ENDFUNCTION, MODULE … ENDMODULE.

Control statements

You use these statements to control the flow of an ABAP program within a processing block according to certain conditions.

Examples of control keywords:

IF, WHILE, CASE

Call statements

You use these statements to call processing blocks that you have already defined using modularization statements. The blocks you call can either be in the same ABAP program or in a different program.

Examples of call keywords:

CALL METHOD, CALL TRANSACTION, SUBMIT, LEAVE TO

Operational statements

These keywords process the data that you have defined using declarative statements.

Examples of operational keywords:

MOVE, ADD