Site icon DataFlair

SAP ABAP Data Types, Variables, Constants & Literals

SAP ABAP Data types, Variables, Constants and Literals

FREE Online Courses: Click, Learn, Succeed, Start Now!

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?

Data Types in ABAP

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

TYPE KEYWORD DESCRIPTION
Byte Field X Byte number in hexadecimal – e.g. 2B, 511, DC, etc.
Text Field C Any kind of alphanumeric or special character – e.g. ‘c’
Integer I Any kind of number e.g. 24, 1, -3478
Floating Point F Numbers with decimal point – e.g. 3.14, 2.0
Packed Number P Number defined by parameters ‘length’ and ‘decimal’ places – e.g. 100000000.00
String STRING A series of alphanumeric and special characters – “Data Flair”, “SAP #2”, “ABAP123”

Complex and Reference Data Types in ABAP

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

PARAMETER DESCRIPTION
Line/row Rows, or the horizontal lines of a table can be of type  elementary, complex or reference.
Key Keys are unique identifiers of table rows, and are usually of elementary type.
Access method Access 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.

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

Variables in ABAP

SYNTAX FOR VARIABLE in ABAP

DATA variableName TYPE dataType.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Here,

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

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

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

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:

What are constants?

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?

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.

Exit mobile version