ABAP data

Summary: in this tutorial, you will learn how to declare variables using the ABAP data keyword.

Declaring variables using ABAP data keyword

A variable is a data object whose value can be changed at runtime. To declare a variable, you use the data keyword with the following syntax:

data variable_name type data_type.

Following the data keyword is the variable name. The variable name must follow the these rules:

  • It cannot exceed 30 characters in length.
  • It starts with a letter, and can have Roman letters, digits, and underscores.

After the variable name is the type keyword followed by the data type of the variable.

Variables has two types static variable and reference varaibles.

Static varibles

Static variables have fixed-length types and reserve a data range in memory. For example:

data gv_name type c(50).

In this example, gv_name is a static variable. It has a data type c whose length is 50.

Note that the variable name starts with gv_ and its description. This is a common practice to make the code more readable. By convention, g means global, v stands for the variable. So gv_name indicates that it is a global variable. To declare local variables, you use lv_ prefix e.g., lv_name.

You can specify an initial value for a static variable by using the optional value keyword like this:

data gv_name type c(50) value 'John Doe'.

In this example, the gv_name takes the literal characters 'John Doe' as the initial value. If you do not specify the initial value for gv_name, its defaults value is blank.

Reference variables

Unlike a static variables, reference variables does not reserve a data range in main memory. Before using reference variables, you need to reserve data for them by using the create data or create object statement.

To declare a reference variable, you use the type ref to keywords. For example:

data gr_amount type ref to f.

Inline declarations

Since ABAP release 7.4, you can use inline declarations to both declare and assign a value to a variable at the same time.

Prior to ABAP release 7.4, you have to declare a variable and assign it a value using separate lines:

data gv_status type i.
gv_status = lcl_sales_order=>get_status( ).

Now, you can use the inline declaration as follows:

data(gv_status) = lcl_sales_order=>get_status( ).

In this syntax, the gv_status will take the type of the return value of the lcl_sales_order=>get_status( ) method.

Chain statements

To declare multiple variables, you can use multiple statements with the data keyword. For example:

data gv_status type i.
data gv_counter type i.
data gv_description type c(30).

Or you can chain the declaration statements:

data: gv_status type i,
      gv_counter type i,
      gv_description type c(30).      

Summary

  • Use the data keyword to declare variables.
  • In ABAP, variables are static variables whose length is specified at declaration time and reference variables whose length is specified at runtime.