ABAP DATA Statement: Declare Variables, Syntax and Examples

Category
ABAP-Statements
Published
Author
Johannes

The DATA statement is the fundamental statement in ABAP for declaring variables. Variables are named data objects (memory areas) whose content or value can be changed during program runtime. They are the core of data processing in ABAP, as they enable storing, reading, calculating, and manipulating information.

Unlike constants (declared with CONSTANTS), whose value is fixed after declaration, variables are flexible. Each variable has a data type that determines what kind of data it can store (e.g., text, number, date) and how much memory it needs.

Syntax

Declaring variables with DATA can be done in various ways:

1. Declaration of a Single Variable

DATA <variable_name> TYPE <data_type> [LENGTH <length>] [DECIMALS <decimal_places>]
[VALUE <initial_value> | IS INITIAL].
  • <variable_name>: The name of the variable (must follow ABAP naming conventions).
  • TYPE <data_type>: Preferred method. Specifies the data type (e.g., I, P, C, STRING, D, T, F, XSTRING, a Dictionary type like MARA-MATNR, a global type from SE11, or a local type).
  • LIKE <other_data_object>: Older method, should be replaced by TYPE in new code where possible. The variable inherits data type and properties from another already declared data object (e.g., another variable, a structure component, a Dictionary field like MARA-MATNR).
  • LENGTH <length>: Optional explicit length specification for certain types (C, N, X, P).
  • DECIMALS <decimal_places>: Required for type P (packed number) to define the number of decimal places.
  • VALUE <initial_value>: Optional. Assigns an initial value to the variable directly at declaration.
  • VALUE IS INITIAL: Optional. Explicitly assigns the type-specific initial value. This is the default behavior when VALUE is omitted.

2. Declaration of Multiple Variables (Chain Statement)

DATA: <var1> TYPE <type1> [VALUE <value1>],
<var2> LIKE <field2>,
<var3> TYPE <type3>,
... .

With a colon after DATA, multiple variables can be declared separated by commas. The statement ends with a period.

3. Declaration of a Structure Variable

DATA: BEGIN OF <structure_name>,
<component1> TYPE <type1> [VALUE <value1>],
<component2> LIKE <field2>,
...
END OF <structure_name>.

Defines a variable that contains a group of components (fields).

4. Declaration of an Internal Table

DATA <table> TYPE [STANDARD | SORTED | HASHED] TABLE OF <row_type>
[WITH <key_definition>]
[INITIAL SIZE <n>].

Defines an internal table, a dynamic data structure for holding multiple rows of the same type (<row_type>).

5. Declaration of a Reference Variable

DATA <ref_variable> TYPE REF TO <type> | <class> | <interface> | DATA.

Defines a variable that does not contain data directly, but points to other data objects or instances of classes/interfaces (similar to a pointer).

Initialization / Default Values

If no VALUE addition is specified when declaring with DATA, the variable is automatically initialized with the type-specific initial value:

  • Numeric types (I, P, F): 0
  • Character types (C, N, STRING): Spaces (C, N) or empty string (STRING)
  • Date/Time (D, T): '00000000' or '000000'
  • Byte types (X, XSTRING): Hexadecimal 0 (X'00') or empty byte string (XSTRING)
  • References (REF TO): NULL reference (points to nothing)

Inline Declaration (Modern ABAP Style >= 7.40)

A very common, modern alternative is inline declaration. Here the variable is declared directly at the point where it is first assigned a value:

DATA(<new_variable>) = <expression>.
" Examples:
DATA(lv_date) = sy-datum. " lv_date automatically gets type D
DATA(lo_instance) = NEW zcl_my_class( ). " lo_instance gets type REF TO zcl_my_class
SELECT SINGLE * FROM t001 WHERE bukrs = '0001' INTO @DATA(ls_t001). " ls_t001 gets type T001

The type of the variable is automatically derived by the compiler from the context (e.g., the result of the expression on the right side or the SELECT statement). This reduces declaration overhead and keeps the declaration close to usage.

Scope

The location of the DATA declaration determines where the variable is valid (visible and usable):

  • Global: Declaration in the global declaration section of a program (REPORT, FUNCTION-POOL, CLASS-POOL). The variable is valid throughout the program/object.
  • Local: Declaration within a procedure (METHOD, FUNCTION, FORM). The variable is only valid within this procedure.

Examples

1. Simple Variables

DATA counter TYPE i. " Integer, initial value 0
DATA message TYPE string VALUE `Hello`. " String with initial value
DATA customer_id TYPE n LENGTH 10. " Numeric text, 10 digits, initial value '0000000000'
DATA valid_until TYPE d. " Date, initial value '00000000'
DATA company_name TYPE name1. " Dictionary type (e.g., from KNA1)
DATA total_amount LIKE bseg-wrbtr. " Type from field WRBTR from structure BSEG (older syntax)

2. Chain Statement

DATA: first_name TYPE string,
last_name TYPE string,
age TYPE i VALUE 30,
city TYPE string.

3. Structure Variable

DATA: BEGIN OF person,
name TYPE string,
dob TYPE d, " Date of birth
zip TYPE ad_pstcd1, " Postal code (Dictionary type)
city TYPE string,
END OF person.
person-name = 'Smith'.
person-dob = '19900101'.

4. Internal Table

TYPES: BEGIN OF ty_material_s,
matnr TYPE matnr,
maktx TYPE maktx,
END OF ty_material_s.
DATA materials TYPE STANDARD TABLE OF ty_material_s WITH EMPTY KEY.
DATA user_list TYPE HASHED TABLE OF syuname WITH UNIQUE KEY table_line.

5. Reference Variable

DATA alv_grid TYPE REF TO cl_gui_alv_grid.
DATA data_ref TYPE REF TO data.

6. Inline Declaration in Action

DATA(lv_user) = sy-uname.
WRITE: / 'Current user:', lv_user.
LOOP AT materials INTO DATA(ls_material). " ls_material is declared inline (type: ty_material_s)
WRITE: / ls_material-matnr, ls_material-maktx.
ENDLOOP.
TRY.
DATA(lo_calculator) = NEW zcl_simple_calculator( ). " lo_calculator is REF TO zcl_simple_calculator
DATA(lv_result) = lo_calculator->add( val1 = 5 val2 = 10 ). " lv_result is I (if method returns I)
WRITE: / 'Result:', lv_result.
CATCH cx_root INTO DATA(lx_error). " lx_error is REF TO cx_root
WRITE: / 'Error:', lx_error->get_text( ).
ENDTRY.

In summary: The DATA statement is indispensable for working with mutable data in ABAP. The modern inline declaration DATA(...) significantly simplifies variable definition and is now standard in many development scenarios.