SAP ABAP Variable

The data objects that stores value according to allotted memory space are called Variables. As the name suggests values of VARIABLE can be varied using ABAP statements. SAP variable can be of Predefined Data Types that we have discussed in our earlier post or of Data Elements Type that we will discuss later. In this post I will show how to declare Data Variables using predefined Data Types.

ABAP Variables are declared using keyword DATA. These are created every time during Program execution and destroyed after every execution.

Example:

  1. Declaring single variable.DATA NAME (20) TYPE n.

 

  1. DECLARING USING DEFAULT DATA TYPE: Character is the default data type i.e. if you are not giving any data type it will be a character.

 

DATA NAME (20).

 

  1. Declaring multiple variable using colons (:).

DATA:   ID (10) TYPE n,

NAME (20) TYPE c.

  1. Declaring Variable using LIKE instead of TYPE keyword. LIKE keyword is used if the data type of given variable is same as the one we have already used. We make the second variable like the first variable.

DATA:   FIRST_NAME (10) TYPE c,

LAST_NAME (10) LIKE FIRST_NAME.

  1. Declaring Structured Variable. In our previous post we learned about Structured Data Types. Similarly, we can define structured Variable using keyword BEGIN OF<Variable Name> and END OF<Variable Name>.

 

DATA:   BEGIN OF EMPLOYEE_DETAILS,

ID (10) TYPE n,

NAME (20) TYPE c,

END OF EMPLOYEE_DETAILS.

 

  1. Declaring Variable Using/Referring Existing Structured Data Type:We can declare a structure Data Type and can reuse it in different ways to create Structured Variable.

Example:

TYPES:   BEGIN OF ADDRESS,

HOUSE_NUMBER (10) TYPE n,

NAME (20) TYPE c,

END OF EMPLOYEE_DETAILS.

DATA:   HOUSE_ADDRESS TYPE ADDRESS,

OFFICE_ADDRESS LIKE HOUSE_ADDRESS.

 

Difference between Structured Data Type and Structured Variable:

 

  1. DATA TYPES are structures that are created to be used by DATA to create variables.
  2. DATA TYPES creates structure using keyword TYPES that holds no memory while Variables created using keyword DATA holds memory.

Accessing SAP Variable of Structured Variable:

 

To access each individual field of structured variable, we use hyphen (-).

Example: Here we will access NAME from EMPLOYEE_DETAILS (Point 5 Above is a Structured Variable)

EMPLOYEE_DETAILS-NAME

To assign value we use equal to sign (=)

EMPLOYEE_DETAILS-NAME = ‘BARRY ALLEN’.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »