SAP ABAP Tutorial – In ABAP, one of the first actions you typically take is defining variables that will store the data you use. Such variables represent locations in memory where data will be stored. The data could be numeric, such as an integer (123), or alphabetic, such as a character (a) or string of characters (abc)
Variables are defined with the DATA keyword followed by the name of the variable and then the TYPE keyword followed by the data type.
The following example we will create variable called gv_variable of type i ( Integer ) .
1 |
DATA gv_variable TYPE i |
You can create a variable and give it a value using the optional VALUE keyword.
1 |
DATA gv_variable TYPE i VALUE 123 |
By adding a colon ( : ) after the keyword DATA, you can chain multiple variable definitions together.
1 2 |
DATA : gv_first TYPE i, gv_second TYPE i. |
Numeric Data Types
Integer Data Types, The integer range for a byte is 0 to 255, a short is -32,768 to 32,767, and an integer is -2,147,483,648 to 2,147,483,647.
Example declare variable integer.
1 |
DATA : lv_integer TYPE i VALUE 100 . |
Packed Number, Packed numbers allow you to define the number of decimal places (up to 14) in the number, and the value stored will be rounded when assigned. If no decimal value is defined, it will be treated as an integer.
This is a go-to data type for handling data such as distances or money.
1 |
DATA : lv_packed TYPE p DECIMALS 2. |
Decimal Floating Point Number, decimal floating point numbers (dec – point numbers fl oatl6 and decfl oat34) can be used when you need more precision in your calculations or when you need to work with numbers that are
outside of the range for i . de cf 1 oa tl6 allows for 16 decimal places of precision, and decf1 oa t34 allows for 34 decimal places of precision.
1 |
UATA : lv_decfloat TYPE decfloat16. |
Character Data Types
Character data types in ABAP are, c ( char ), string, n ( numeric character ) , d ( date in the form yyyymmdd ) and t ( time in format hhmmss ).
The character (c) data type is used to store a fixed amount of up to 262,143 alphanumeric characters. The number of characters is defined using the LENGTH keyword. If no length is provided, it is treated as a single character. If you are unsure of how many characters you will need, use a string.
1 2 |
DATA : lv_chars TYPE c LENGTH 5 VALUE 'test', lv_onechar TYPE c VALUE 'A'. |
A string (string) data type is used to hold any number of characters, and its length will change automatically as its value changes. In the example below, the variable d_string will hold the ‘Hello World’, without having to declare a length.
1 |
DATA : lv_hello type string VALUE 'HEllo World'. |
A numeric text (n) data type works just like the character (c) data type, except it only allows for numeric characters. This data type is useful when working with IDs that have leading zeros.
1 |
DATA : lv_id TYPE n LENGTH 10 VALUE 1234567890. |
A date (d) data type is used to store any date. The first four characters are the year, the next two represent the month, and the last two represent the day.
1 |
DATA : lv_d<strong>a</strong>te type d VALUE '20170101'. |
A time (t) data type is used to store any time. The first two characters Time data type are the hour (based on a 24-hour clock), the second two characters are the minutes, and the last two characters are the seconds.
1 |
DATA: lv_time TYPE t VALUE '101522'. |
lnline Data Declarations
Inline data declarations are new as of ABAP 7.4 SP2. This means that lnline data instead of defining variables in the beginning of the program with a declarations specific type, you can define them when they are needed and not
specify the type that the data will be. The variable is still hard-typed to a specific data type, meaning that it can’t start as an integer and then change to a string.
1 |
DATA(lv_integer) = 20 . |
Next article we will learn about Arithmetic and Basic Math Function SAP ABAP Tutorial.