How To Declare Various Data Types in ABAP?
Like in any other programming languages, we use variables as containers for storing and managing data, allowing us to manipulate and process information throughout our code.
In this tutorial, we’ll go over the basics of variable declaration in ABAP, explore different data types, and examine examples to help illustrate how these data types are used in ABAP programs.
Tutorial Objectives |
1. Understand different data types in ABAP |
2. Know how to declare and use each data type in ABAP Program. |
Prerequisites |
SAP System Access: SAP GUI, ECC. |
Authorization: Developer role, TCODE: SE38. |
You will need to create a new ABAP Program to test the codes below, If you’re unsure how to create a new ABAP program, you can refer to our ABAP Hello World Tutorial for a step by step guide.
Basic Syntax For Declaring Variables in ABAP
In ABAP Programming, variables are known as data objects that can be declared using DATA statement and followed by TYPE to define the data types. This allows you to define the structure and type of the variable before using it in the program.
DATA lv_variable TYPE lv_types.
Just take a look at example below. In this example we have 2 data objects, the first data object is lv_name with string as the data type, and the second data object is lv_age data object with i (integer) as the data type.
DATA lv_name TYPE string.
DATA lv_age TYPE i.
Using these data objects, we can assign values to perform various operations. For instance:
lv_name = 'John Doe'.
lv_age = 45.
In ABAP, data types are categorized into three main groups: Elementary Data Types, Reference Types, and Complex Data Types.
ABAP Elementary Data Types
ABAP has what is called elementary data type or predefined data which are foundational types built into the language.
These predefined data types conveniently allow developers to specify commonly used types without needing custom definitions. For instance, there are types available for characters, as well as for numeric values; additionally, dates and times have their own dedicated types, making it easier to handle a range of standard data formats.
Data Type | Initial Length | Length | Description |
---|---|---|---|
Numeric Types | |||
I | 4 | 4 | Integer |
F | 8 | 8 | Float number |
P | 8 | 1-16 | Packed Number |
Character Types | |||
C | 1 | 1-65535 | Characters (alphanumeric) |
D | 8 | 8 | Date (format: YYYYMMDD ) |
T | 6 | 6 | Time (format: HHMMSS ) |
Here’s how to declare each of the data types in ABAP. For data type P, you can specify the number of decimal places. For type C, while the default length is 1, you also have the option to define a custom length as needed.
You can also use keyword data: to declare more that one data objects and separate with commas.
data: lv_i type i,
lv_f type f,
lv_p type p DECIMALS 2.
data: lv_c type c LENGTH 10,
lv_d TYPE d,
lv_t TYPE t.
lv_p = 10 / 3.
lv_d = '20240101'.
lv_t = '1313900'.
WRITE: lv_p, lv_d, lv_t.
The length of Elementary data types is fixed, meaning that these data types have a predefined storage size in memory and a set length or range of values they can hold.
But there are also another kind of data types that are variable-length data types, which means their length is not fixed which is called STRING and XSTRING.
So unlike elementary types, STRING and XSTRING can dynamically adjust their length to accommodate the content they hold, making them ideal for handling text and binary data of varying sizes (large sizes), so we use data type STRING to store a large alphanumeric data such as base64 of some PDF files and XSTRING to store binary of images.
data: lv_string type string,
lv_xstring type x.
ABAP Reference Types
Reference types point to data objects, instances of classes, or structures in memory using TYPE REF TO keyword. Reference types are essential for working with dynamic and complex data structures, especially in object oriented programming (OOP).
DATA: lv_variable TYPE REF TO lv_data_object.
In this example below, we declare a data object called g_cont as a reference to the class cl_gui_custom_container.
To instantiate the g_cont object, we use the create object syntax.
data: g_cont type ref to cl_gui_custom_container.
create object g_salv
exporting
container_name = 'CONT'
.
ABAP Complex Data Types
If you want to store a structured data such as records from a table or arrays then you can use complex data type. Complex data types in ABAP fall into two main categories: Structures and Internal Tables.
1. Structured Data Type
Using structured type you can group several fields into one unit, this way you can define each columns inside the structure. After you define the structured data type, you can declare a data object (wa_emp) that refers to this structured type.
TYPES:
BEGIN OF typ_emp,
emp_id type c LENGTH 3,
emp_nm type c LENGTH 30,
title type c LENGTH 5,
END OF typ_emp.
DATA: wa_emp TYPE typ_emp.
To assign values into wa_emp, each field of the structure needs to be defined individually. You can add these codes below.
wa_emp-emp_id = 'E01'.
wa_emp-emp_nm = 'John'.
wa_emp-title = 'Mr'.
After that you can display the values using the write statement for each column as shown below. By adding : after the WRITE keywords, you can display more than one values in one line.
WRITE: wa_emp-emp_id, wa_emp-emp_nm, wa_emp-title.
2. Table Types / Internal Tables
If you want to store multiple records in a table, you’ll need to use Table Types. You can declare a table type using the keyword TYPE TABLE OF followed by the structure’s name.
To add entries to an internal table, you’ll need a line type or working area. In this example we’ll be using wa_emp as the working area to store data for each record, and the internal table, which is it_emp as shown in the example below.
DATA: wa_emp type typ_emp,
it_emp TYPE TABLE OF typ_emp.
Using the APPEND statement, you can add entries to the internal table. Now, IT_EMP contains three employee records, as shown in the example below.
wa_emp-emp_id = 'E01'.
wa_emp-emp_nm = 'Grace'.
wa_emp-title = 'Mrs'.
append wa_emp TO it_emp.
wa_emp-emp_id = 'E02'.
wa_emp-emp_nm = 'John'.
wa_emp-title = 'Mr'.
append wa_emp TO it_emp.
wa_emp-emp_id = 'E03'.
wa_emp-emp_nm = 'Lucy'.
wa_emp-title = 'Ms'.
append wa_emp TO it_emp.
To display the internal table data we can loop through its contents using the LOOP statement. But please notice that we added slash ( / ) after the WRITE: statement to add new line for each record.
LOOP AT it_emp INTO wa_emp.
WRITE:/ wa_emp-emp_id, wa_emp-emp_nm, wa_emp-title.
ENDLOOP.
Here’s the screen output.

You can also use an existing transparent tables in SAP as the table type for your variables as shown below, where we declare data object called it_mara that refers to MARA table (Material master), meaning that it_mara will have all the columns from the MARA table.
DATA: it_mara TYPE TABLE OF mara.
Last But Not Least, Let’s talk about Performance
Okay now that you know how to use DATA and also TYPES, can you tell me what differentiate these statements below?
DATA lv_name type c LENGTH 10.
TYPES: typ_name type c LENGTH 10.
Here we declare a DATA object lv_name and immediately allocates memory for it, with the type c and length of 10 characters.
On the other hand, the statement TYPES: typ_name defines a data type typ_name but does not allocate any memory. It simply creates a type that can later be used for declaring variables.
It will start allocating memory after you use this TYPES to a data object as shown below.
DATA: ld_name TYPE typ_name.
So, performance wise, using TYPES to define a type and then referencing it in a DATA statement later on (when you need it) is generally efficient because memory is only allocated when a variable is actually declared with DATA statement. The TYPES statement merely defines the structure, so it doesn’t incur any performance cost related to memory allocation.
Download ABAP Data Types Source Code
You can download today’s lesson source code here at Github: ABAP Data Type Source Code
Share this tutorial with Fellow Geeks!