Skip to main content

Variables & Datatypes

To define a variable in ABAP you can.

Declare Variables

Define Single Variable

Syntax: DATA (Variable_name) TYPE (DATA_Type) LENGTH(Length)

Note that Length is optional

DATA lv_name TYPE C LENGTH 20.
DATA lv_age TYPE I.
DATA meow TYPE STRING.

You can define multiple variables of same data type in one statement

DATA: lv_name  TYPE C LENGTH 20,
lv_name2 TYPE C LENGTH 20,
lv_name3 TYPE C LENGTH 20.

You can assign a value to a variable using

lv_name = 'Meooooow'.
meow = 'cat'.

You can also Define a variable with value in the same statement

DATA lv_name TYPE C LENGTH 15 VALUE 'Abdullah'.
DATA lv_age TYPE I VALUE 13.
Inline Declarations

You Can Define a variable with its value without assigning a type. ABAP will automatically infer its data type

Syntax: DATA(Variable_Name) = 15.

DATA(lv_age) = 15.
Primitive Data Types in ABAP
Type KeywordData Type MeaningExample
CCharacter (fixed length)DATA variable TYPE C LENGTH 10 VALUE 'Hello'.
NNumeric text (fixed length)DATA variable TYPE N LENGTH 5 VALUE '12345'.
DDate (YYYYMMDD)DATA variable TYPE D VALUE '20251016'.
TTime (HHMMSS)DATA variable TYPE T VALUE '143000'.
IIntegerDATA variable TYPE I VALUE 15.
INT88-byte IntegerDATA variable TYPE INT8 VALUE 123456789.
FFloating point numberDATA variable TYPE F VALUE '3.14'.
PPacked number (decimal)DATA variable TYPE P LENGTH 8 DECIMALS 2 VALUE '123.45'.
XHexadecimal (fixed length)DATA variable TYPE X LENGTH 4 VALUE 'A1B2'.
STRINGDynamic character stringDATA variable TYPE STRING VALUE 'Hello World'.
XSTRINGDynamic byte stringDATA variable TYPE XSTRING VALUE '48656C6C6F'.
DECFLOAT16Decimal floating point 16DATA variable TYPE DECFLOAT16 VALUE '1.234567890123456'.
DECFLOAT34Decimal floating point 34DATA variable TYPE DECFLOAT34 VALUE '1.234567890123456789012345678901234'.

Adding Comments

You can add comments in ABAP using * at the beginning of the line

* This is a comment
" This is also a comment
DATA lv_age TYPE I VALUE 15. " This is an inline comment
DATA lv_name TYPE C LENGTH 20 VALUE 'Abdullah'.
Commenting Multiple Lines Shortcuts

You can highlight code and then press Shift + > to comment whole code.

Or

You can press Shift + < to uncomment

Write Statements

You can write to the console using the WRITE statement

WRITE: 'Hello World'.

You can write multiple items in one statement

WRITE: 'Hello', 'World', '!' .

You can write variables

DATA(lv_name) = 'Abdullah'.
WRITE: 'Hello', lv_name, '!' .

You can write multiple lines using / before the item

WRITE: / 'Hello',
/ 'World',
/ '!'.

Conditional Blocks

IF Statement

Syntax: IF (condition). ... ENDIF.

DATA(lv_age) = 15.

IF lv_age > 18.
WRITE: 'Adult'.
ENDIF.

IF-ELSE Statement

DATA(lv_age) = 15.

IF lv_age >= 18.
WRITE: 'Adult'.
ELSE.
WRITE: 'Minor'.
ENDIF.

IF-ELSEIF-ELSE Statement

DATA(lv_score) = 85.

IF lv_score >= 90.
WRITE: 'Grade A'.
ELSEIF lv_score >= 80.
WRITE: 'Grade B'.
ELSEIF lv_score >= 70.
WRITE: 'Grade C'.
ELSE.
WRITE: 'Grade F'.
ENDIF.

CASE Statement

Use CASE when you have multiple conditions based on same variable

DATA(lv_day) = 'Monday'.

CASE lv_day.
WHEN 'Monday'.
WRITE: 'Start of week'.
WHEN 'Friday'.
WRITE: 'Almost weekend'.
WHEN 'Saturday' OR 'Sunday'.
WRITE: 'Weekend'.
WHEN OTHERS.
WRITE: 'Midweek'.
ENDCASE.
Comparison Operators
OperatorMeaningExample
= or EQEqual toIF lv_age = 18.
<> or NENot equalIF lv_age <> 18.
> or GTGreater thanIF lv_age > 18.
< or LTLess thanIF lv_age < 18.
>= or GEGreater or equalIF lv_age >= 18.
<= or LELess or equalIF lv_age <= 18.

Logical Operators: AND, OR, NOT

Workareas & Internal Tables

Internal Table : Similar to SQL Table (stores multiple rows of data)
Work Area : Single row of data from internal table

Define Structure

First define the structure of your data

TYPES: BEGIN OF ty_student,
id TYPE I,
name TYPE STRING,
age TYPE I,
END OF ty_student.

Define Work Area and Internal Table

DATA: wa_student TYPE ty_student,
it_students TYPE TABLE OF ty_student.

Add Data to Internal Table

Using Work Area:

wa_student-id = 1.
wa_student-name = 'Abdullah'.
wa_student-age = 20.

APPEND wa_student TO it_students.

Read Data from Internal Table

Loop Through All Rows:

LOOP AT it_students INTO wa_student.
WRITE: / wa_student-id, wa_student-name, wa_student-age.
ENDLOOP.

Debugging

We talked about debugging techniques. However, most of interactions needed GUI and i Don't have access to GUI now.

Will update it later with ScreenShots

info General Recommendations & Notes

  • Save & Format Regularly: Always save your work. Sessions can close anytime and un-saved work can be lost.
  • Use Comments: Comment your code for clarity, especially in complex logic.
  • Test Frequently: Run and test your code often to catch errors early.