SAP ABAP | Understanding Variables
Last Updated :
30 Aug, 2024
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 SAP ABAP
Types of Variable in SAP ABAP- Local variables
- Static variables
- Reference variables
- System variables
- 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.
Local variable CodeOutput:
Local Variable OutputEvery 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.
Static variable CodeOutput:
Static Variable outputIn 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.
Reference variable CodeOutput:
Reference Variable outputIn 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.
Output:
system variables outputIn 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.
Structure CodeOutput:
Output of StructureEvery 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.
Similar Reads
SAP Advanced Business Application Programming (ABAP)
SAP ABAP stands for Advanced Business Application Programming. SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which i
6 min read
What is SAP ABAP: A Brief Overview
SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which includes enterprise resource planning (ERP) systems and other bu
8 min read
SAP ABAP | Basic Syntax & Statements
The German software company SAP created the high-level programming language, ABAP (Advanced Business Application Programming) primarily, this language serves as a tool for developing applications within the SAP R/3 system. Designed with simplicity and ease of learning in mind, ABAP syntax allows eff
9 min read
SAP ABAP | Understanding Variables
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.Syn
7 min read
SAP ABAP Keywords
The core of many global enterprises, SAP ABAP stands for Advanced Business Application Programming. It is the heart and soul of SAP systems. It gives users the ability to expand and modify SAP apps to satisfy particular business needs. The fundamental building blocks of SAP ABAP are its keywords, wh
5 min read
SAP ABAP | Constants & Literals Explained
In the world of SAP ABAP (Advanced Business Application Programming), the use of Constants and Literals is necessary for the effective handling of data. Literals are used to denote specific data types such as numbers, characters, strings, and boolean values. Constants & Literals in SAP What are
7 min read
SAP ABAP | Data Types
Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of t
6 min read
Relational operators in SAP ABAP
Relational operators in SAP ABAP are symbols or combinations of symbols that compare values and return a Boolean result (either true or false). These operators allow developers to establish relationships between variables, constants, or expressions, facilitating decision-making processes in the prog
6 min read
Operators in SAP ABAP
High-level programming languages like SAP ABAP (Advanced Business Application Programming) are used to create apps in the SAP environment. Operators are essential for the execution of many different operations in SAP ABAP, ranging from straightforward arithmetic computations to complex logical analy
7 min read