Data Types and Data Objects
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Every value a program works with lives in a named place in memory, and that place has a fixed shape: how many characters it
holds, whether it is text or a number, how many decimal places it keeps. ABAP separates the shape, called a data type, from
the place, called a data object. A newcomer meets this on the first line of almost every program, because nothing can be
stored, compared or printed before it is declared. After reading this page you can choose a type for an amount, a date or a
claim identifier, declare it with TYPES, DATA, CONSTANTS or an inline declaration, and read the system fields that
ABAP fills in for you. The page is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20
(2608). Everything here applies to both ABAP Cloud and classic Standard ABAP unless the text says otherwise; the older
declarations that only classic code allows are marked, and they appear on on-premise systems and in the Private Edition but
not in ABAP Cloud development, which is the only mode in the Public Edition.
Before you read
-
Basic programming ideas such as a variable, a statement and a comment.
-
ABAP for the overview of the language and its two modes, ABAP Cloud and Standard ABAP.
Key terms
-
Data type: the description of a value’s shape, such as text of ten characters or a number with two decimals.
-
Data object: a named place in memory, such as a variable or a constant, that holds one value of a data type.
-
Elementary type: a type that describes a single value, for example a number, a piece of text or a date.
-
Structure: a data object made of several named components, much like one row of a form.
-
Internal table: a data object that holds any number of rows of the same type, kept in the memory of the running program.
-
Inline declaration: a declaration written at the place where a variable is first filled, so that ABAP works out its type.
-
System field: a read-only value that the runtime sets, for example the result of the last statement.
Types and objects
A data type is only a description; it reserves no memory. A data object is created from a type and holds a value. You
describe a type with TYPES and create an object with DATA. If you give DATA a type that already exists, the object gets
that shape. Types that SAP defines for you are built in (the elementary types below) or live in the ABAP Dictionary and in
CDS, where every program can refer to them by name.
Two small examples show the split. The first line only describes a shape; the second creates a place with that shape.
TYPES ty_claim_id TYPE c LENGTH 10.
DATA claim_id TYPE ty_claim_id.
This listing runs in the local playground, inside a report that also assigns a value to claim_id and prints it with WRITE.
Elementary types
An elementary type describes one value. ABAP groups the built-in elementary types into character-like, numeric and
byte-like families. Fixed-length types need a LENGTH; the numeric packed type also needs DECIMALS.
| Type | What it holds | Typical use |
|---|---|---|
|
Text of a fixed length. The default length is one character. |
Codes and identifiers such as a claim status. |
|
A fixed-length string of digits only, with leading zeros kept. |
Numbers that are labels, not quantities: a document number, a postal code. |
|
A date, stored as eight characters in the form year, month, day. |
Dates without a time. |
|
A time of day, stored as six characters: hour, minute, second. |
Times without a date. |
|
A four-byte integer. |
Counters and indexes. |
|
An eight-byte integer. |
Integers too large for |
|
A packed number: exact decimal arithmetic with a chosen length and number of decimals. |
Money amounts and quantities. |
|
Decimal floating point numbers with 16 or 34 significant digits. |
Large ranges of exact decimal values and results of financial calculations. |
|
Text of variable length. |
Descriptions and any text whose length is not fixed. |
|
A sequence of bytes of variable length. |
Binary content such as a file or a hash. |
Source: Built-In Numeric Types (ABAP Keyword Documentation), Built-In Character-Like Types, Built-In Date Types, Time Types, and Time Stamp Types
Fixed-length character-like types such as c and n have a length between 1 and 262,143 characters, and the default length is
one character. A value that is longer than the object is cut off, so choose a length that fits the longest value; the
string type grows as needed. A d object holds eight characters and a t object six, and they behave like text in text
positions. Only valid dates and times give guaranteed results in statements, so checking a date that comes from outside
is your job.
The next listing runs in the local playground, inside a report that prints the two objects with WRITE. The value
'20261313' is not a valid date, yet it is stored as given, and 'ABCDEF' assigned to a c object of length 3 leaves
ABC.
DATA d TYPE d VALUE '20261313'.
DATA c3 TYPE c LENGTH 3.
c3 = 'ABCDEF'.
Amounts: packed numbers
A money amount must not drift by a fraction of a cent, so the amount of a claim is a packed number. The type below has a
length of fifteen bytes, and with two decimals it holds a value such as 1,234.50 exactly. The decfloat34 type also does exact
decimal arithmetic but chooses the decimal point per value instead of fixing it; a packed number is the simpler choice
when every value has the same number of decimals.
TYPES ty_amount TYPE p LENGTH 15 DECIMALS 2.
DATA total TYPE ty_amount.
DATA price TYPE ty_amount VALUE '19.99'.
DATA tax TYPE ty_amount.
tax = price * '0.21'.
total = price + tax.
This listing runs in the local playground, inside a report that prints the three values with WRITE. The output is 19.99, 4.20 and 24.19.
The default length of a p object is eight bytes, and the number of decimals belongs to the type, so always write both LENGTH and DECIMALS. The
rounding rules that apply when a result has more decimals than the target are on
Expressions, Operators and Assignments.
Structures and table types
A structure groups components of different types under one name. A table type describes a table whose rows all have the same line type. Both are built from the elementary types and from each other, which is how a claim with its items is modelled in the Expense claims example.
TYPES: BEGIN OF ty_item,
item_no TYPE n LENGTH 4,
text TYPE string,
amount TYPE ty_amount,
END OF ty_item.
TYPES ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.
TYPES: BEGIN OF ty_claim,
claim_id TYPE c LENGTH 10,
status TYPE c LENGTH 1,
items TYPE ty_items,
END OF ty_claim.
DATA claim TYPE ty_claim.
claim-claim_id = 'C000000001'.
claim-items = VALUE #( ( item_no = 1 text = `Train ticket` amount = '45.50' ) ).
This listing runs in the local playground, inside a report that has REPORT zp_d2. as its first line and the amount type from the previous listing.
A component is reached with a hyphen, as in claim-claim_id. A component can itself be a structure or a table, which is
the case for items. Working with the rows of an internal table is on the page Internal Tables.
TYPES versus DATA, and CONSTANTS
The difference matters in practice. A TYPES statement can be repeated in several declarations and costs nothing at run
time. A DATA statement creates storage, and the object can be changed. A CONSTANTS statement creates storage too, but
it needs a value at declaration and the program can never change it afterwards. A constant is how a status code gets a
name instead of being repeated as a letter.
CONSTANTS: BEGIN OF c_status,
open TYPE c LENGTH 1 VALUE 'O',
approved TYPE c LENGTH 1 VALUE 'A',
rejected TYPE c LENGTH 1 VALUE 'R',
END OF c_status.
DATA status TYPE ty_claim-status VALUE c_status-open.
This listing runs in the local playground together with the structure listing above; status holds O afterwards.
The statement DATA status TYPE ty_claim-status takes the type from a component of a structure type. The older form
DATA status LIKE claim-status takes the type from another data object instead; both forms are valid, but
TYPE with a named type is usually clearer, because the type does not depend on a variable.
Inline declarations: DATA( ) and FINAL( )
Writing the declaration far from the first use makes code long and lets types drift. An inline declaration creates the
object at the place of its first assignment, and ABAP takes the type from the right-hand side. DATA( ) gives a variable
that can change; FINAL( ) gives an object that is set once and cannot be changed afterwards, which suits a result you only
read.
DATA(count) = lines( claim-items ).
FINAL(label) = |Claim { claim-claim_id } has { count } item(s)|.
DATA(first_item) = claim-items[ 1 ].
This listing runs in the local playground together with the structure listing above and prints Claim C000000001 has 1 item(s).
The type comes from the operand, so count is an integer and label is a string. An inline declaration works in
many operand positions, for example as the target of SELECT ... INTO @DATA(...) or of a method call, and the object can be used in the rest of the
context where it is declared. Because the type is implicit, check it when a value comes from a
generic or an untyped source.
Generic types
A generic type leaves part of the shape open. It is normally not used to create a data object; it types the formal parameters of a
procedure and field symbols, where a parameter of type any accepts a value of any type, any table accepts an internal table of any kind,
and clike accepts any character-like value, which means c, n, string, the date and time types and flat character-like
structures. The procedure then adapts to what it receives, and the actual type is known
when the program runs.
CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS show_length
IMPORTING text TYPE clike
RETURNING VALUE(length) TYPE i.
ENDCLASS.
CLASS lcl_demo IMPLEMENTATION.
METHOD show_length.
length = strlen( text ).
ENDMETHOD.
ENDCLASS.
This listing runs in the local playground when a report calls lcl_demo=>show_length( 'abc' ) after it; the result is 3.
The method accepts a c, n, d, t or string argument alike. The full list of generic types and the rules for
typing parameters are in the keyword documentation; classes and methods are on
the page Classes and Objects.
The system fields
ABAP maintains a structure called sy, whose components are system fields. You read them; you do not write them. The
ones every developer meets are these.
| Field | Meaning |
|---|---|
|
The return code set by many statements. Zero generally means that the statement ran without problems; what another value means depends on the statement. Test it directly after the statement. |
|
The line number in the table index of the row last addressed by an internal table statement. |
|
The pass number inside a |
|
The number of table lines that an SQL statement processed. |
|
The current date, time and user name in classic code. In ABAP for Cloud Development the date and time fields are not used; the class |
The keyword documentation lists the system fields that may be read in ABAP for Cloud Development, among them sy-subrc,
sy-tabix, sy-index, sy-dbcnt and sy-uname; reading any other system field leads to a warning of the syntax check.
Source: ABAP System Fields (ABAP Keyword Documentation), ABAP cheat sheet: Date and Time
Many new statements and functions avoid the need for them; for example, a table expression raises an
exception instead of setting sy-subrc, and line_exists( ) tests for a row.
Older material you may meet
Books and code written before release 7.40 declare everything at the top of a program and use older forms that you still read, mostly in maintenance work.
-
DATAblocks collect every declaration at the start of a program or method; current code declares withDATA( )orFINAL( )at first use. -
TABLES <name>creates a table work area, a structure whose type is the identically named Dictionary structure, and it is used with dynpros and classic reports. New code declares a structure withDATAand a type instead. -
DATA x LIKE ycopies the type of another object;TYPEwith a named type is usually clearer. -
Header lines and
OCCURSon internal tables are legacy forms; use a table type andDATA. -
Older text sometimes sizes money with the floating-point type
f; usepordecfloat34for exact decimals. -
FINAL( )and the many inline forms are newer than the books; a system on an older release may reject them, so check the release of the target system.
Summary
-
A data type describes a shape and reserves nothing; a data object is created from a type with
DATA,CONSTANTS,FINAL( )or an inlineDATA( ). -
Elementary types hold one value:
c,n,d,t,i,int8,p,decfloat16,decfloat34,stringandxstring. Usepordecfloat34for money. -
Structures group components and table types describe tables of one line type; both are built from other types with
TYPES. -
CONSTANTSgives a name to a value that never changes;FINAL( )does the same for a variable set once. -
Generic types such as
anyandclikeare used for parameters, not for objects, andsyholds read-only fields such assy-subrc. -
TABLESand header lines are legacy forms; new code usesDATAwith a type.
Try it
To do this by hand, follow the guide Data Types, Strings and String Templates: it declares variables, converts values and builds strings and string templates.