Section 3 of 11

Data Types and Variable Declaration

Built-in types, declaring variables, structures and internal tables, and inline declarations in modern ABAP.

Open contents

ABAP has the types a business system needs. That amounts and quantities have a type designed for exact decimal arithmetic says a lot about the language.

Built-in types

The main built-in types
TypeContentDefault lengthInitial value
CFixed-length text1Space
NNumeric text, zero padded1'0'
DDate, YYYYMMDD8'00000000'
TTime, HHMMSS6'000000'
IInteger, 4 bytesโ€”0
INT8Integer, 8 bytesโ€”0
PPacked decimal80
FFloating point80
STRINGVariable-length textVariableEmpty
XSTRINGVariable-length bytesVariableEmpty

N compared with C

How they differ
DATA lv_num TYPE n LENGTH 5.
DATA lv_chr TYPE c LENGTH 5.

lv_num = 42.       " gives '00042' โ€” zero padded
lv_chr = 42.       " gives '42   ' โ€” space padded

Document numbers and material codes in SAP are usually N fields, which is why they display with leading zeros.

Declaring variables

Declarations
" Built-in type directly
DATA lv_name TYPE c LENGTH 30.
DATA lv_amount TYPE p LENGTH 8 DECIMALS 2.

" Referencing a Data Dictionary type (preferred)
DATA lv_matnr TYPE matnr.

" Referencing a table field
DATA lv_bukrs TYPE bkpf-bukrs.

" With an initial value
DATA lv_count TYPE i VALUE 10.

" Constant
CONSTANTS lc_status TYPE c LENGTH 1 VALUE 'A'.

Structures

Defining and using a structure
TYPES: BEGIN OF ty_item,
         matnr TYPE matnr,
         maktx TYPE maktx,
         menge TYPE i,
       END OF ty_item.

DATA ls_item TYPE ty_item.
ls_item-matnr = 'MAT-001'.

" A Dictionary structure
DATA ls_mara TYPE mara.

Internal table types

Declaring internal tables
DATA lt_items TYPE STANDARD TABLE OF ty_item.
DATA lt_mara  TYPE STANDARD TABLE OF mara.

DATA lt_sorted TYPE SORTED TABLE OF ty_item
               WITH UNIQUE KEY matnr.

Inline declarations

From ABAP 7.4
SELECT SINGLE * FROM mara INTO @DATA(ls_mara) WHERE matnr = @lv_matnr.

LOOP AT lt_items INTO DATA(ls_item).
  WRITE: / ls_item-matnr.
ENDLOOP.

DATA(lt_numbers) = VALUE int_tab( ( 1 ) ( 2 ) ( 3 ) ).

Declaring at the point of use keeps declarations near the logic that needs them and narrows variable scope, which makes code easier to read.

๐Ÿ“– Unfamiliar term? Look it up in the SAP glossary.

Check your understanding

Test what you just read.

Quiz 1

Which basic data type represents an integer in ABAP?

Quiz 2

What is the internal format of ABAP date type D?

Quiz 3

In ABAP, variables are declared using the DATA statement.

Quiz 4

Which data type is most suitable for calculations involving decimal points, such as amounts and quantities?

Quiz 5

Data element types defined in the Data Dictionary can be referenced using the TYPE clause in ABAP programs.

Quiz 6

Which statement correctly describes ABAP type N?