Variables 

Variables are named data objects that you can declare statically using declarative statements, or dynamically while a program is running. They allow you to store changeable data under a particular name within the memory area of a program.

You can declare variables statically using the following statements:

This section explains the DATA and STATICS statements. For further information about CLASS-DATA; refer to Classes . For further information about PARAMETERS, SELECT-OPTIONS, and RANGES, refer to Selection Screens .

Variables are declared dynamically when you add characters or bytes to a string, or lines to an internal table. After you have declared a string, only its type is defined. When you declare an internal table, the line type, access type, and key are defined. The actual data objects - the characters and bytes for a string, or the lines of an internal table - are created dynamically at runtime.

You can also create data objects dynamically when you call procedures . These data objects are the formal parameters of the interface definition, which only have technical attributes when they inherit them from the actual parameters passed to them.

The DATA Statement

You use the DATA statement to declare variables in an ABAP program or instance attributes in a class. Within the program or class, you can also declare local variables within procedures. The same rules of visibility apply to variables as to types (see The TYPE Addition). Local variables in procedures obscure identically-named variables in the main program or class.

The DATA statement has a similar syntax to the TYPES statement:

DATA <f> ... [TYPE <type>|LIKE <obj>]... [VALUE <val>].

The variable name <f> may be up to 30 characters long. You can use all characters except for + . , : ( ). A name may also not consist entirely of digits. Names of predefined data objects cannot be changed. You should not use names that are the same as an ABAP keyword or addition. You should:

When you declare a variable statically, you define all of its technical attributes, that is, its length, data type, and number of decimal places. You can do this in the following ways:

Referring to Existing Technical Attributes

You can create a variable that inherits exactly the same technical attributes as an existing data type or data object as follows:

DATA <f> [TYPE <type>|LIKE <obj>]...

If you use the TYPE addition, <type> is any data type with fully-specified technical attributes. This can be a:

If you use the LIKE addition, <obj> is a data object that has already been declared. This can also be a predefined data object. The variable <f> adopts the same technical attributes as the data object <obj>. You can also use LIKE to refer to a line of an internal table that has already been declared as a data object:

DATA <f> LIKE LINE OF <itab>.

To ensure compatibility with previous releases, <obj> can also be a database table, a view, a structure, or a component of a structure from the ABAP Dictionary.

The data types to which you refer can be elementary types, reference types, or complex types (structures or tables). For elementary field types, the variables are a single field in memory. When you declare a data type with fixed length (D, F, I, T) the system fixes the amount of memory that will be assigned. When you declare an object with a variable length (STRING, XSRTING), the system only assigns enough memory to administer the object. The length of the data object is managed dynamically at runtime. For structures, the variables are a sequence of variables, which may themselves also be included in further complex structures. The individual components take their name <ci> from the type <type> or object <obj>, and can be addressed using <f>-<c i> For tables, the memory contains administration entries that can be filled dynamically at runtime.

TYPES: BEGIN OF struct,
         number_1 TYPE i,
         number_2 TYPE p DECIMALS 2,
       END OF struct.

DATA:  wa_struct TYPE struct,
       number    LIKE wa_struct-number_2,
       date      LIKE sy-datum,
       time      TYPE t,
       text      TYPE string,
       company   TYPE s_carr_id.

This example declares variables with reference to the internal type STRUCT in the program, a component of an existing data object WA_STRUCT, the predefined data object SY-DATUM, the predefined ABAP type T and STRING, and the data element S_CARR_ID from the ABAP Dictionary.

Referring to Data Types with Generic Attributes

If you refer to a generic predefined ABAP type in the DATA statement (types C, N, P, or X), you must specify the undefined technical attributes in the statement. The DATA statement has the same syntax as the TYPES statement for this purpose:

DATA <f>[(<length>)] TYPE <type> [DECIMALS <d>]...

The <length> option sets the field length. If you omit it, the field length is set to the relevant initial value in the table in the Predefined ABAP Types section. If <type> is P, you can specify the number of decimal places using the DECIMALS <d> addition. If you omit this, the number of decimal places is set to 0.

If you do not use the TYPE or LIKE addition, the system uses the default predefined generic type C. Thus the implicit statement

DATA <f>.

defines a character variable <f> with length 1. It is a shortened form of the explicit statement DATA <f>(1) TYPE C.

DATA <f>(1) TYPE C.

The maximum number of decimal places for a packed number is 14. To use the decimal places, the program attribute Fixed point arithmetic must be set, otherwise, the variables will be treated like integers. When you assign a value to a packed number, non-significant figures are rounded.

It is not currently possible to refer to generic data types in the DATA statement other than to generic standard tables without a defined key.

DATA: text1,
      text2(2),
      text3(3) TYPE c,
      pack TYPE P DECIMALS 2 VALUE '1.225'.

This example creates three character variables with lengths one, two, and three bytes respectively, and a packed number variable with length 8 bytes and two decimal places. If the attribute Fixed point arithmetic is set, the value of PACK is 1.23.

Creating Variables With Their Own Data Type

We have so far seen how you can define variables by referring to existing data types. However, the DATA statement also allows you to use the same type constructors as in the TYPES statement to assign a data type to a variable when you declare it. This data type then does not exist in its own right, but only as an attribute of the corresponding data object. The data type is linked to the data object. You can refer to it using the LIKE addition, but not using TYPE.

References

The syntax for a direct reference variable declaration is the same as the definition using the TYPES statement:

DATA <f> TYPE REF TO ...

After TYPE, there is no reference to an existing data type. Instead, the type constructor occurs:

REF TO DATA

defines a field <f> with the data type reference to a data object. The reference variable <f> can contain references (pointers) to data objects, that is, instances of data types (see also Data References).

REF TO <class>|<interface>

defines a field <f> with the data type reference to an object in ABAP Objects. The reference variable <f> can contain references (pointers) to instances of the class <class> or its subclasses, or to classes that implement the interface <interface> respectively. See also Object Handling.

structures

The syntax for declaring a structure directly is the same as you would use to define a structure using the TYPES statement:

DATA: BEGIN OF <structure>,
              ..............
<fi>...,
              ..............
END OF <structure>.

This chained statement creates a structure containing all of the variables between

DATA BEGIN OF <structure>. and DATA END OF <structure>.

that occur in the

DATA <fi>....

statements. As in the TYPES statement, the DATA BEGIN OF - DATA END OF statement blocks can be nested. The components <fi> can be elementary fields, reference variables, or, if you refer to known complex types, complex themselves. Since fields of type I or F are aligned (see Aligning Data Objects), the system inserts empty filler fields between the components, if necessary.

The individual variables within a structure are addressed in the program with a hyphen between the structure name and component name as follows: <structure>-<fi>.

DATA: BEGIN OF address,
         name(20)    TYPE c,
         street(20)  TYPE c,
         number      TYPE p,
         postcode(5) TYPE n,
         city(20)    TYPE c,
      END OF address.

This example defines a structure called ADDRESS. The components can be addressed using ADDRESS-NAME, ADDRESS-STREET, and so on.

Internal tables

The syntax for declaring an internal table directly as a data type of a variable is the same as you would use to define one using the TYPES statement:

DATA <f> TYPE|LIKE <tabkind> OF <linetype> WITH <key>.

After TYPE, there is no reference to an existing data type. Instead, the type constructor occurs:

<tabkind> OF <linetype> WITH <key>.

The variable <f> is declared as an internal table with access type <tabkind>, line type <linetype>, and key <key>. The line type <linetype> can be any known data type. For more information, refer to Internal Tables.

Specifying a Start Value

When you declare an elementary fixed-length variable, the DATA statement automatically fills it with the type-specific initial value as listed in the table in the Predefined ABAP Types section.

However, you can also specify a starting value of a fixed-length elementary variable (also within a structure declaration) using the VALUE addition in the DATA statement:

DATA <f>..... VALUE <val>.

The start value of the field <f> in the program is set to <val>, where <val> can be

You cannot use other variables or text symbols in the VALUE addition. You cannot assign a starting value to a variable-length data object (STRING, XSTRING), an internal table, or a reference variable.

Examples of start value specifications:

DATA: counter TYPE p VALUE 1,
      date    TYPE d VALUE '19980601',
      flag    TYPE n VALUE IS INITIAL.

After this data declaration, the variable FLAG contains its type specific initial value 0.

Static Variables in Procedures

Variables that you declare with the DATA statement live for as long as the context in which they are defined. So variables in an ABAP main program exist for the entire runtime of the program, and local variables in procedures only exist for as long as the procedure is running.

To retain the value of a local variable beyond the runtime of the procedure, you can declare it using the STATICS statement. This declares a variable with the lifetime of the context of the main program, but which is only visible within the procedure.

The first time you call a subroutine or function module, the corresponding main program is always loaded into the internal session of the calling program. It is not deleted when the procedure ends. This enables variables defined using STATICS to retain their values beyond the runtime of the procedure, allowing them to be reused the next time the procedure is called (see the example in the Local Data in Subroutines section).

In methods, variables defined with STATICS are static attributes that are only visible in the corresponding method, but for all instances of a class (see Classes).

The syntax of the STATICS statement is identical to that of the DATA statement.