Open In App
Related Articles

SAP ABAP | Understanding Variables

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

What is a Variable?

Variable are named data objects that refer to the memory location allocated during the program execution for processing. As the name indicates, users can use refer to the range of values that may be stored inside and the range of actions that can be carried out on the variable.

Syntax:

DATA <f> TYPE <type> VALUE <val>. 
  • <f> specifies the name of the variable.
  • <type> specifies the type of variable.
  • <val> specifies the initial value of the <f> variable.

The DATA statement automatically assigns the type-specific initial value to any elementary fixed-length variables you may define.

Example of Variable Declaration in ABAP:

DATA k1(5) TYPE D.  
DATA k2 LIKE k1.
DATA minimum_value TYPE I VALUE 10.

In the above code snippet, k1 is a variable of D type, k2 is a variable of k1 type, and minimum_value is a variable of ABAP integer type I.

Guildlines for Variable Declaration in SAP:

There are some certain rules in SAP for variable declaration, which are mentioned below:

  • We can not use special characters while declaring a variable.
  • The name of the predefined objects or keywords can not be changed.
  • We can not declare variable names same as any keyword in SAP ABAP.
  • We can not use hyphens ( – ) in any variables using variable declaration.

Types of Variable in ABAP

  1. Local variables
  2. Static variables
  3. Reference variables
  4. System variables
  5. Structured variables

1. Local Variable

Local variables meaning is self-explanatory. They are highly specific to the application, function, inclusion, etc. The variable’s lifetime is the duration of the execution of the program, function, include, etc. where it was initially declared.

Simple program to get clear understanding of Local variable.

*&---------------------------------------------------------------------*
*& Report Z_LOCAL
*&---------------------------------------------------------------------*
*& Program Written by GEEKSFORGEEKS
*&---------------------------------------------------------------------*

REPORT Z_LOCAL.

* Loop for 10 times.
DO 10 TIMES.

* Perform Subtraction subroutine.
PERFORM subtraction

ENDDO.

* Subroutine subtraction coding.
FORM subtraction.

* Initializing local variable.
DATA lv_local TYPE I VALUE 101

* subtracting 1 to local variable.
lv_local = lv_local - 1.

* Display local variable.
WRITE: / lv_local.

ENDFORM.

Output:

100
100
100
100
100
100
100
100
100
100

Every statement in the above mentioned example is preceded with a remark that provides context for the statement. Read these to obtain a clear idea of the sample code.

Coming to output, lv_local variable is declared in the subroutine and it is local to the subtraction subroutine. So the lv_local variable got initialized everytime whenever subtraction subroutine called.

In the above example, we have called subtraction subroutine 10 times. So, the lv_local variable got initialized with 99 on every time and subtract 1 from 101 and display output 100. The same happens for 10 times.

2. Static Variable

Static variables only ever initialize once, at the beginning of the program’s execution of the program, function, include, etc., and are not initialize after that. The static variable’s value is retained until the execution of the program, function, inclusion, etc. is complete.

Certainly, here are the rephrased points about variable declarations in SAP ABAP:

  • Declaration of Static Variables:
    • Static variables can be defined within subroutines, function modules, or static methods.
    • Their lifespan is tied to the scope of the declaration.
  • Usage of ‘CLASS-DATA’ Statement:
    • Within classes, you can employ the ‘CLASS-DATA’ statement to declare variables.
  • ‘PARAMETERS’ Statement for Input Fields:
    • The ‘PARAMETERS’ statement is utilized for declaring elementary data objects associated with input fields on a selection screen.
  • ‘SELECT-OPTIONS’ Statement for Input Fields and Internal Tables:
    • You can also employ the ‘SELECT-OPTIONS’ statement to declare internal tables linked to input fields on a selection screen.

Simple program to get clear understanding of static variable.

*&---------------------------------------------------------------------*
*& Report Z_STATIC
*&---------------------------------------------------------------------*
*& Program Written by GEEKSFORGEEKS
*&---------------------------------------------------------------------*

REPORT Z_STATIC.

* Loop for 8 times.
DO 8 TIMES.

* Perform addition Subroutine.
PERFORM addition.

ENDDO.

* Subroutine addition declaration and coding.
FORM addition.

* Initializing static variable.
STATICS lv_static TYPE I VALUE 99.

* Adding 1 to static variable.
lv_static = lv_static + 1.

* Display static variable.
WRITE: / lv_static.

ENDFORM.

Output:

100
101
102
103
104
105
106
107

In the above example, each statement is preceded by a remark that explains the assertion. Examine them to acquire a good view of the sample code.

In terms of output, the subroutine declares the lv_static variable, which is static to the addition procedure. As a result, the lv_static variable was only initialized the first time the addition procedure was used. The lv_static variable, which was initially set to 99, is added to 99 after the first sobroutine call, and the result 100 is shown for the first time. The value 100 from the first call is retained in the lv_static variable for the second call, and the result 101 is shown in the second iteration.

In the above example, we have called addition subroutine 8 times. So, the lv_static variable value incementing by 1 every time and display output 100, 101, 102, 103, 104, 105, 106 and 107.

3. Reference Variable:

Reference variables are made up of references. In an ABAP application, the real contents of a reference variable, namely the value of a reference, are not accessible. ABAP contains both data and object references. As a result, data reference variables are classified into two types: data reference variables and object reference variables.

Simple program to get clear understanding of reference variable.

*&---------------------------------------------------------------------*
*& Report Z_REFERENCE
*&---------------------------------------------------------------------*
*& Program Written by GEEKSFORGEEKS
*&---------------------------------------------------------------------*

REPORT Z_REFERENCE.

* local variable lv_number declaration
DATA: lv_number TYPE I VALUE 7.

* local reference variable lr_number declaration
DATA: lr_number TYPE REF TO I.

* Displaying the local variable number before referencing
WRITE : 'lv_number value before referencing ', lv_number.

* Referencing lr_number to lv_number
GET REFERENCE OF lv_number INTO lr_number.

* Changing the value of lv_number using the reference variable lr_number
lr_number->* = 9.

* Displaying the local variable number after referencing & value change
WRITE : /'lv_number value after referencing ', lv_number.

Output:

lvl_number value before referencing     7
lvl_number value after referencing 9

In the above example, each statement is preceded by a remark that explains the assertion. Examine them to acquire a good view of the sample code.

In the output, the variable lv_number is declared as a local variable and initialized with 7. The reference variable lr_number was declared, and the reference of lv_number was assigned to lr_number using the command “GET REFERENCE OF”.

The lr_number now points to the lv_number memory address where the value 7 is stored. Using lr_number to assign 9 to the memory space indirectly alters the lv_number value. As a result, 7 was substituted with 9, and the final output is kept in lv_number.

4. System Variable

In SAP ABAP, system variables are predefined variables that record system-specific information like the current date, time, and user. They are available from within ABAP applications and offer critical system-related data for use in program logic and presentation.

A quick description of some popular SAP ABAP system variables:

  • SY-UNAME: The current user’s ID.
  • SY-DATUM: Today’s date.
  • SY-UZEIT: This is the current time.
  • SY-INDEX: The current loop’s index.
  • SY-SUBRC: The last operation’s return code.
  • SY-TABIX: The current line’s index in an internal table.
  • SY-CPROG: The current program’s name.
  • SY-TCODE: The transaction code of the current transaction.
  • SY-LSIND: A LOOP indicator at an internal table.
  • SY-LSLSP: A nested internal table LOOP indicator.

These variables are critical for program control and processing in SAP ABAP.

Simple program to display some of the system variables.

*&---------------------------------------------------------------------*
*& Report Z_SYSTEM_VARIABLE
*&---------------------------------------------------------------------*
*& Program Written by GEEKSFORGEEKS
*&---------------------------------------------------------------------*

REPORT Z_SYSTEM_VARIABLE.

* Displaying some of the system variables
WRITE:/'SY-ABCDE', SY-ABCDE,
/'SY-DATUM', SY-DATUM,
/'SY-DBSYS', SY-DBSYS,
/'SY-LANGU', SY-LANGU,
/'SY-MANDT', SY-MANDT,
/'SY-OPSYS', SY-OPSYS,
/'SY-SAPRL', SY-SAPRL,
/'SY-TCODE', SY-TCODE.



Output:

SY-ABCDE ABCDEFGHIJKLMNOPQRSTUVWXYZ
SY-DATUM 06.10.2023
SY-DBSYS MSSQL
SY-LANGU EN
SY-MANDT 800
SY-OPSYS Windows NT
SY-SAPRL 702
SY-TCODE SE38

In the above example, each statement is preceded by a remark that explains the assertion. Examine them to acquire a good view of the sample code.

The Latin alphabets are shown via SY-ABCDE. SY-DATUM displays the system date (also known as the program run date). SY-DBSYS shows the name of the system central database. The system language is shown via SY-LANGU. The current client ID used to logon is shown by SY-MANDT. SY-OPSYS displays the current application server’s operating system. SY-SAPRL displays the currently active ABAP release. SY-TCODE displays the ABAP editor’s current running transaction code.

5. Structured Variables

The structured variable specified as a structured data type is supported by ABAP. Structured variable may be defined by utilizing BEGIN OF and END OF keywords.

A structure variable can be declared by using LIKE on another structure variable. The hyphen (-) character can be used to access each individual field in the structure.

Simple program to display student details using structure.

*&---------------------------------------------------------------------*
*& Report Z_STRUCTURE_VARIABLE
*&---------------------------------------------------------------------*
*& Program Written by GEEKSFORGEEKS
*&---------------------------------------------------------------------*

REPORT Z_STRUCTURE_VARIABLE.

* Declaring student structure with student no, student name and
* student class
DATA: BEGIN OF student,
no TYPE n,
name(25) TYPE c,
class(10) TYPE c,
END OF student.

* Assigning value to student no
MOVE 1 TO student-no.

* Assigning value to student name
MOVE 'Ashish' TO student-name.

* Assigning value to student class
MOVE '3rd Class' TO student-class.

* Displaying student structure details by using structure appending
WRITE : 'student-no :', student-no,
/ 'student-name :', student-name,
/ 'student-class:', student-class.

Output:

student-no    : 1
student-name : Ashish
student-class : 3rd Class

Every statement in the mentioned example is preceded with a remark that provides context for the statement. Read these to obtain a clear idea of the sample code. A structural variable is student. No, the fundamental variables in the student structure variable are name and class. The structural variables relate to the students as student-no, student-name, and student-class respectively. In the programming procedure, the variables student-no, student-name, and student-class are utilized to manipulate the data.

Conclusion

Finally, variables are critical components in ABAP programming. They provide for data storage, manipulation, and control flow, all of which contribute to the underlying logic of SAP systems. Variables, whether used for local computations or global data management, are critical in establishing the behavior and functionality of ABAP applications. Mastery of variable handling is critical for effective and efficient ABAP programming, guaranteeing correct data processing and successful business process execution inside the SAP environment.



Last Updated : 29 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads