Specifying the Data Type and the Length of the Variable

To specify the data type, use the <type> parameter. You can create the <type> parameter by using either of the following:

The TYPE parameter

With the TYPE parameter you can specify either a predefined data type or a user-defined data type (see Data Types). The syntax is as follows:

Syntax

DATA <f>[(<length>)] TYPE <t>.

You can define the length of some of the elementary data types. To do this, enter the required value in place of <length>. This applies to field types C, P, N, or X. For a list of initial values and valid values, see the table in Predefined Elementary Data Types.

Examples of specifying data types with the TYPE parameter:

DATA: NUMBER TYPE P,
DATE TYPE D,
HEXADECIMALFIELD TYPE X,
COUNT TYPE I,
LINE(72) TYPE C.

The LIKE parameter

With the LIKE parameter, you can assign the data type of already defined data objects to the variable. To do so, you use:

Syntax

DATA <f> LIKE <g>.

When you use the LIKE parameter, the field <f> is created with exactly the same type and structure as the data object <g>.

You can use any data object for <g>. With the LIKE parameter, you can reference the data type of a data object declared in the ABAP Dictionary (for example a table, a structure, a view, or their individual fields).

DATA NUMBER_1 TYPE P.
DATA NUMBER_2 LIKE NUMBER_1.
DATA MYNAME LIKE SY-UNAME.

In this example, the data object NUMBER_2 is declared with the same data type as the data object NUMBER_1. The data object MYNAME has the same data type as the system-defined data object SY-UNAME.

The LIKE parameter is often used for auxiliary fields to store the contents of database fields temporarily. This facility enables you to avoid unintentional differences resulting from typing errors or changes in the definition of the database field. If, for example, you change the attributes of a database field, the system adapts the attributes of the auxiliary field automatically.

DATA PLANE LIKE SFLIGHT-PLANETYPE.

This statement creates a data object called PLANE with the same attributes as the ABAP Dictionary field SFLIGHT-PLANETYPE. PLANETYPE is a column of the database table SFLIGHT.

To create the data object <f> with the same type as a line of an existing internal table, use the LIKE parameter as follows:

Syntax

DATA <f> LIKE LINE OF <itab>.

In this case, <itab> must be a data object created as an internal table (see Creating Internal Table Data Objects).

Defaults for Type and Length

If the parameters <length> and <type> are not specified in the DATA statement, a character field (type C) of length 1 is created. If a length is specified but no type, a character field of the given length is created.

DATA TEXTFIELD.

This example creates a character field TEXTFIELD which has length 1.