SAP ABAP Data Types, Variables, Constants & Literals

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this tutorial, we are going to learn about SAP ABAP data types, variables, constants and literals. Like any other object-oriented programming language, you need data types, variables, constants and literals to facilitate ABAP programming. Let’s learn what they are all about.

What are Data Types?

  • Think about the different kinds of data we have – characters, text, numbers, and so on.
  • For a computer to perform functions on data, it is a much easier process if it falls under a pre-defined category.
  • Declaring a type for the data that we have speeds up execution considerably, and also reduces the time identifying data that would have otherwise been spent on producing useful output for the code.

Data Types in ABAP

In ABAP, we have several types of fixed as well as variable data types, as follows:

TYPEKEYWORDDESCRIPTION
Byte FieldXByte number in hexadecimal – e.g. 2B, 511, DC, etc.
Text FieldCAny kind of alphanumeric or special character – e.g. ‘c’
IntegerIAny kind of number e.g. 24, 1, -3478
Floating PointFNumbers with decimal point – e.g. 3.14, 2.0
Packed NumberPNumber defined by parameters ‘length’ and ‘decimal’ places – e.g. 100000000.00
StringSTRINGA series of alphanumeric and special characters – “Data Flair”, “SAP #2”, “ABAP123”

Complex and Reference Data Types in ABAP

  • There are also complex data types in ABAP.
  • Complex types are for structures (object made of components stored one after the other in memory) and tables (a 2D structure of rows and columns in which data is stored sequentially).
  • For structure types, the primary data types as well as nested structures may be grouped together.
  • When these primary types are grouped together, the entire structure can be accessed as one or the individual elements can be retrieved as well – this is known as internal tables in ABAP.

Now let’s see with what parameters these internal tables can be described –

PARAMETERDESCRIPTION
Line/rowRows, or the horizontal lines of a table can be of type  elementary, complex or reference.
KeyKeys are unique identifiers of table rows, and are usually of elementary type.
Access methodAccess methods are modifiers that basically declare how the individual table rows or cells can be authorised for access

Reference types are the data types used to refer to instances of class, interface and run-time data items. 

Here’s a sample program based on Data Types in ABAP –

EPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA text_line TYPE C LENGTH 40.
text_line = 'Data Flair SAP ABAP Tutorial'.
Write text_line.

DATA text_string TYPE STRING.
text_string = 'Awesome Tutorial on Data Types by Data Flair'.
Write / text_string.

DATA sample_int TYPE I.
sample_int = -1234.
Write / sample_int.

DATA sample_byte TYPE X.
sample_byte = 'FF'.
Write / sample_byte.

DATA sample_float TYPE F.
sample_float = '3.14'.
Write / sample_float.

Output:

Data Flair SAP ABAP Tutorial
Awesome Tutorial on Data Types by Data Flair
-1234
FF
3,140000000000001E+00

What are Variables?

Now, think about the data to be stored with an example.

  • Suppose you want to take an integer as input, add 1 to it, and output the result
  • How will you take the input?
  • Via keyboard, mouse and so on.
  • However, once you receive the input, how will you make sure whatever it is gets added to 1?
    • You store it, of course!
  • And to store the integer, you need something that would hold the value and ‘vary’ according to what the input is, i.e. you require a variable.

Therefore, a variable is a symbol that holds some data, which can be performed upon by some additional instructions.

Variables in ABAP

  • In ABAP, you can create variables according to one data type.
  • That is, before creating a variable, you must know what data type it will belong to.
  • Similarly, in our above example, we have declared that our input will be an integer.
  • Hence, the variable will be an integer variable.

SYNTAX FOR VARIABLE in ABAP

DATA variableName TYPE dataType.

Here,

  • Length limit to variable name is 30 characters (max)
  • Any data type – elementary, complex or reference can be used in declaration

EXAMPLE FOR VARIABLE in ABAP

DATA dataFlairID Type I.

There are three variable types in ABAP – 

  1. Static variables
  2. Reference variables
  3. System variables

STATIC VARIABLES IN ABAP

  • Static variables are variables that are active throughout the block in which they are declared i.e. its lifetime extends from the point of declaration in the block to the end of that block.
  • With local variables (described above) the declaration begins with ‘DATA’, for static variables within a class the declaration has ‘CLASS-DATA’ instead.
  • Along with ‘CLASS-DATA’ statement, ‘PARAMETERS’ are used to declare linked data objects and ‘SELECT-OPTIONS’ to include selection screens and their internal tables
  • Static variables are usually declared within subroutines or function modules.

There are a few rules to follow when naming a variable. Those rules are as follows – 

1. Do not use any ABAP keyword as a variable name

2. Do not use special characters in variable name except underscore ‘_’ used to separate words for easier understanding

3. Don’t try to change names of predefined structures, objects and data

4. Variable name must be lucid and it should easily convey the use without explanation

5. Avoid using hyphens ‘-’ in variable names as they are used in structures

Example of static variables in ABAP:

REPORT ZDataflair_001. 
PARAMETERS: NAME(10) TYPE C, 
CLASS TYPE I, 
ID TYPE P DECIMALS 2, 
CONNECT TYPE MARA-MATNR. 
WRITE: ID.

Output:

2

REFERENCE VARIABLES IN ABAP

  • Reference variables refer to another type, usually elementary data type
  • These may refer to a class as well
  • They are of dynamic type, whereas in its declaration a static type which is more general is used

Example of ABAP reference variables:

CLASS DATAFLAIRCLASS DEFINITION. 
PUBLIC SECTION. 
DATA DATAFLAIRDATA TYPE I VALUE 1. 
ENDCLASS. DATA: ODATAFLAIRREF TYPE REF TO DATAFLAIRCLASS , 
DDATAFLAIRREF1 LIKE REF TO ODATAFLAIRREF, 
DDATAFLAIRREF2 TYPE REF TO I . 
CREATE OBJECT ODATAFLAIRREF. 
GET REFERENCE OF ODATAFLAIRREF INTO DDATAFLAIRREF1. 
CREATE DATA DDATAFLAIRREF2. 
DDATAFLAIRREF2→* = DDATAFLAIRREF1→*→ DATAFLAIRDATA.
WRITE: DATAFLAIRDATA.

OUTPUT:

1

SYSTEM VARIABLES IN ABAP

  • System variables are ones that can be accessed by all ABAP programs
  • Once accessed, they are allotted values by the runtime environment itself
  • These values reflect the current status of the system at the given point in time
  • The SYST table provides a complete reference to all system variables in ABAP
  • The SYST structure, as all structures, comprises elementary components which can be accessed by using ‘SYST-’ or ‘SY-’

Example of ABAP system variables:

REPORT ZDATAFLAIR_001. 

WRITE:/'SY-SAPRL', SY-SAPRL,
 /'SY-ABCDE', SY-ABCDE,       
      /'SY-HOST ', SY-HOST, 
      /'SY-DBSYS', SY-DBSYS, 
      /'SY-TCODE', SY-TCODE,
      /'SY-LANGU', SY-LANGU,
/'SY-SYSID', SY-SYSID,
      /'SY-MANDT', SY-MANDT,
/'SY-DATUM', SY-DATUM,
      /'SY-UNAME', SY-UNAME

Output:

SAP ABAP Program

What are constants?

  • A constant is exactly what it sounds like – a constant value.
  • We can store a constant under a fixed name, similar to a variable. 
  • However, the difference is that you cannot change the value of a constant once it is declared.
  • For e.g. if you want to store the value of pi = 3.14, you would use a constant!
  • A constant has a value attached to it, which is assigned via its declaration. 
  • This value is stored in the program memory. 
  • The value attached cannot be changed during the execution of a program – which may lead to a run-time error, and this unchangeable value can be considered a literal.

Constants in ABAP

SYNTAX FOR ABAP CONSTANT:

CONSTANTS constantName TYPE dataType VALUE constantValue.

EXAMPLE FOR ABAP CONSTANT

CONSTANTS dataFlairID TYPE I VALUE 1.

The VALUE parameter here is important in constants. It assigns an initial value.

Types of constants in ABAP

1. Elementary: when we define a single element as constant

SYNTAX

CONSTANTS <constant_name> TYPE <date_type> LENGTH <data_length> DECIMALS <decimal_places_no> VALUE <value_of_constant>

e.g.

CONSTANTS PI TYPE P LENGTH 4 DECIMALS 2 VALUE ‘3.14’

2. Complex: when we combine a few elementary constants together

SYNTAX

CONSTANTS:

BEGIN-OF <constant_name>

END-OF <constant_name>

e.g.

CONSTANTS:
BEGIN-OF dataflaircomplex
CONSTANTS PI TYPE P LENGTH 4 DECIMALS 2 VALUE ‘3.14’
CONSTANTS PIE TYPE P LENGTH 6 DECIMALS 4 VALUE ‘3.1412’
END-OF dataflaircomplex

3. Reference: when we pass reference to constants, usually to pass value to parameters

SYNTAX:

CONSTANTS <pointer_name> TYPE REF TO <object_name> VALUE IS INITIAL.

E.g.

CONSTANTS dataflairref TYPE REF TO dataflairobj VALUE IS INITIAL.

What are literals?

  • While variables and constants required a name, literals are quite literally the opposite. 
  • A literal is a data value without a name. 
  • Simply put, it is a value. 
  • And since it is something that does not have a name, it cannot be changed either. 
  • A literal is a nameless constant.
  • It is declared through a keyword and value, which is stored in the program’s memory.

Literals in ABAP

There are two types of literals in ABAP:

1. Numeric Literal in ABAP

Numeric literals are simply integers. They may have a negative sign as prefix, but they cannot have decimal points, logarithms, etc.

E.g.
564
-324
3489732
are all numeric literals.

2. Character Literal in ABAP

Character literals are a combination of alphanumeric characters. They are usually written enclosed in quotation marks. They are also called text literals or string literals.

E.g.
‘Hello Data Flair’
‘Testing Data Flair’
are all character literals

P.S. – If you try to change the value of a literal, a run-time error shall occur.

Here is how you add variables, constants & literals in ABAP code –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA: A TYPE I VALUE 2,
      B TYPE I VALUE 4.
Write: / 'The variable value A is = ', A.
Write: / 'The variable value B is =', B.
*Here, A & B are variables.

Write: / 'Data Flair is awesome'.
Write: / 'This is an ABAP Tutorial'.
*The above two are text literals

CONSTANTS C TYPE P DECIMALS 4 VALUE '1.2356'.
Write: / 'The value of Constant Value is:', C.
*The above is an example of a constant of type - packed number

OUTPUT:

The variable value A is = 2
The variable value B is = 4
Data Flair is awesome
This is an ABAP Tutorial
The value of Constant Value is: 1,2356

Summary

Today, we learnt about the various data types, variables, constants & literals available in SAP ABAP. We learnt their meaning, why and how they are used. We also learnt through examples – both with code and without, on how to use these components.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *