ABAP

Code Statement

Each code statement starts with a keyword and end with a period. Example:

WRITE 'Hello world!'.

It will display Hello world!


Variables

Type Description Default value Example
C Characters Spaces
DATA myVariableName(5) TYPE C VALUE 'Haha!'.
WRITE myVariableName.
 
N Numeric text field '0...0'
DATA myVariableName(5) TYPE N VALUE '12345'.
WRITE myVariableName.
 
D Date field '00000000'
DATA myVariableName TYPE D VALUE '20101231'. "Format: YYYYMMDD
WRITE myVariableName.
 
T Time field '000000'
DATA myVariableName TYPE T VALUE '125959'. "Format: HHMMSS
WRITE myVariableName.
 
X Hexadecimal field X'0...0'
??????
 
P Packed number 0
??????
 
I Integer 0
DATA myVariableName TYPE I VALUE 2147483647.
WRITE myVariableName.
 
F Floating point number 0
DATA myVariableName TYPE F VALUE '1.01234567891234'.
WRITE myVariableName.
 
STRING String ?
DATA myVariableName TYPE STRING VALUE 'String has variable length.'.
WRITE myVariableName.
 
XSTRING Byte sequence ?
????????
 


Comments

Type Description Example
Line comment If you want to comment the whole line, then put * in front of the line.
* Comment the whole line.
 
Partial comment If you want to partially comment a line, then put " before your comment in the line. Everything after " will be considered as a comment.
WRITE 'Hello world!'  "It will display Hello world!
 


Assignment Operator

DATA counter TYPE I VALUE 0.
 
*Statement sequence: Increment the counter variable by 1, 4 times.
*                    Spaces are very important here.
counter = counter + 1.
counter = counter + 1.
counter = counter + 1.
counter = counter + 1.
WRITE counter.
 
*Chain statement: Another way to rewrite the above but shorter. Increment the counter variable by 1, 4 times.
counter = 0.
counter = counter +:1,1,1,1.
WRITE counter.


Conditional statement

OperatorDescription
EQ equal to
= equal to
NE not equal to
<> not equal to
>< not equal to
LT less than
< less than
LE less than or equal to
<= less than or equal to
GT greater than
> greater than
GE greater than or equal to
>= greater than or equal to
DATA a TYPE I VALUE 3.
DATA b TYPE I VALUE 4.
DATA c TYPE I VALUE 3.
 
*Simple IF statement.
IF a = c.
  WRITE 'a and c are equal.'.
ENDIF.
 
*IF statement with multiple conditions.
IF ( a = c ) AND ( a < b ).
  WRITE 'a and c are equal.'.
ENDIF.
 
 
*IF ELSE statement.
IF a GT b.
  WRITE 'a is greater than b.'.
ELSE.
  WRITE 'a is less than b.'.
ENDIF.
 
*IF ELSEIF statement.
IF a GT c.
  WRITE 'a is greater than b.'.
ELSEIF a = c.
  WRITE 'a is equal to c.'.
ELSE.
  WRITE 'a is less than b.'.
ENDIF.

String comparison operator

OperatorDescription
COContains Only
CNContains Not only
CAContains Any
NAcontains Not Any
CSContains String
NScontains No String
CPContains Pattern
NPcontains No Pattern


Loops

DATA i TYPE I VALUE 0.
DO 4 TIMES.
  i = i + 1.
ENDDO.
WRITE i.

Calculate the runtime

REPORT  Z_GET_RUNTIME.
* Note: GET RUN TIME returns microseconds. If the elapse time is too long, it may overflow the integer data types.
 
DATA: T1 TYPE I,
      T2 TYPE I,
      ELAPSED TYPE I.
 
* Get the time and put in T1.
GET RUN TIME FIELD T1.
 
* Wait 3 seconds.
CALL FUNCTION 'ENQUE_SLEEP'
  EXPORTING
    SECONDS = 3.
 
* Get the time and put in T2.
GET RUN TIME FIELD T2.
 
* Calculate the different between T2 and T1.
ELAPSED = T2 - T1.            " In microseconds.
ELAPSED = ELAPSED / 1000000.  " In seconds.
 
* Display the runtime.
WRITE:/ ' Runtime =', ELAPSED, 'seconds'.

Reference: http://help.sap.com/abapdocu_702/en/abapget_run_time.htm

REPORT  Z_ELAPSED_TIMESTAMP.
* Get the elapsed time using timestamps.
 
DATA: LV_TIMESTAMP_START TYPE TIMESTAMPL,
      LV_TIMESTAMP_END TYPE TIMESTAMPL,
      LV_TIMESTAMP_ELAPSED TYPE TIMESTAMPL,
      LV_ELAPSED TYPE STRING.
 
* Get the start time stamp.
GET TIME STAMP FIELD LV_TIMESTAMP_START.
 
********************************************
* Wait 11 seconds.
CALL FUNCTION 'ENQUE_SLEEP'
  EXPORTING
    SECONDS = 11.
 
 
* Get the end time stamp.
GET TIME STAMP FIELD LV_TIMESTAMP_END.
 
* Output the time stamps.
WRITE: / 'Start   : ', LV_TIMESTAMP_START.
WRITE: / 'End     : ', LV_TIMESTAMP_END.
 
* Calculate the elapsed time.
LV_TIMESTAMP_ELAPSED = LV_TIMESTAMP_END - LV_TIMESTAMP_START.
LV_ELAPSED = |{ LV_TIMESTAMP_ELAPSED TIMESTAMP = ISO TIMEZONE = 'UTC   ' }|.
 
* Output elapsed time stamp.
CONCATENATE LV_ELAPSED+11(10) '' INTO LV_ELAPSED. " Extract the time only.
WRITE: / 'Runtime : ', LV_ELAPSED.

Concatenate integers with strings

REPORT  zconcatenate_int_string.
DATA:
      lv_string TYPE string,
      lv_int TYPE i VALUE 9.
 
*CONCATENATE works with string only.
* Therefore, you must assign the integer variable
*   into a string variable.
lv_string = lv_int.
CONCATENATE 'The value is:' lv_string INTO lv_string.
 
WRITE: / lv_string.

Data type declaration

*&---------------------------------------------------------------------*
*& Report  ZDATA_TYPE_EXAMPLE
*& Show how variables can be declared.
*&---------------------------------------------------------------------*
REPORT  zdata_type_example.
 
DATA gv_label TYPE string VALUE 'Current date and time is:'. "Assign value direct to the gv_label variable.
DATA gv_date TYPE d VALUE '20110931'. "Assign value directly using VALUE 'YYYYMMDD'.
DATA gv_time TYPE t. "Assign value later.
gv_time = '065959'. "Assing the time(HHMMSS) now.
 
"The output of date and time may be different depending on your settings.
WRITE: gv_label.
NEW-LINE.
WRITE: /'  Date: [', gv_date, ']'. "Forward slash(/) is the same as NEW-LINE.
WRITE: /'  Time: [', gv_time, ']'.
 
"Use LIKE to defined the same data type as another variable(i.e gv_fname).
DATA gv_fname TYPE string VALUE 'Xuan'.
DATA gv_lname LIKE gv_fname VALUE 'Ngo'.
WRITE: /'My name is ', gv_fname, gv_lname, '.'.

Date Time manipulations

REPORT  Z_DATE_TIME.
 
DATA: LV_DATE TYPE D,
      LV_TIME TYPE T,
      LV_DATE_TIME TYPE STRING.
 
LV_DATE = SY-DATUM.
LV_TIME = SY-UZEIT.
 
WRITE: / 'Current Date:', LV_DATE MM/DD/YYYY.
WRITE: /(60) LV_TIME USING EDIT MASK 'Current Time: __:__:__'.
 
* Put date and time into a string.
CONCATENATE LV_DATE(4) LV_DATE+4(2) LV_DATE+6(2) INTO LV_DATE_TIME SEPARATED BY '-'.
CONCATENATE LV_DATE_TIME '_' LV_TIME(2) '.' LV_TIME+2(2) '.' LV_TIME+4(2) INTO LV_DATE_TIME.
WRITE: / LV_DATE_TIME.

String manipulations

Concatenation

Concatenate integers with strings:

REPORT  zconcatenate_int_string.
DATA:
      lv_string TYPE string,
      lv_int TYPE i VALUE 9.
 
*CONCATENATE works with string only.
* Therefore, you must cast the integer as a string 
*   by assigning the integer variable into a string variable.
lv_string = lv_int.
CONCATENATE 'The value is:' lv_string INTO lv_string.
 
WRITE: / lv_string.

Trimming

Remove leading and trailing spaces:

REPORT  Z_REMOVE_SPACES.
 
DATA: STR TYPE STRING.
STR = ' Hello, world ! '.
 
* Display the initial string with spaces.
WRITE:/ '|',STR,'|'.
 
* Remove space leading and trailing spaces from the string.
SHIFT STR LEFT DELETING LEADING SPACE.
SHIFT STR RIGHT DELETING TRAILING SPACE.
 
* Display end result.
WRITE:/ '|',STR,'|'.