- Docs
- Algorithms
- Tree Algorithms
ABAP Variables
In ABAP, variables are used to store data that can be manipulated and used throughout the program. Understanding how to declare, initialize, and use variables is fundamental to ABAP programming.
Declaring Variables
- Syntax:
DATA: variable_name TYPE data_type [VALUE initial_value].
- Example:
DATA: lv_number TYPE I, lv_text TYPE STRING.
Initializing Variables
-
Variables can be initialized at the time of declaration using the
VALUE
keyword.DATA: lv_count TYPE I VALUE 10, lv_name TYPE STRING VALUE 'SAP'.
-
Variables can also be initialized later in the code.
lv_count = 20. lv_name = 'ABAP'.
Assigning Values
- Values can be assigned to variables using the
=
operator.lv_number = 100. lv_text = 'Hello, ABAP!'.
Working with Strings
-
Concatenation:
CONCATENATE lv_text1 lv_text2 INTO lv_result.
-
Substring:
lv_substring = lv_text+offset(length).
-
Finding Length:
DATA(lv_length) = STRLEN( lv_text ).
Working with Numbers
-
Arithmetic Operations:
lv_sum = lv_number1 + lv_number2. lv_diff = lv_number1 - lv_number2. lv_product = lv_number1 * lv_number2. lv_quotient = lv_number1 / lv_number2.
-
Increment/Decrement:
lv_count = lv_count + 1. lv_count = lv_count - 1.
Constants vs. Variables
- Variables can be changed during program execution, while Constants cannot.
CONSTANTS: lc_pi TYPE P DECIMALS 2 VALUE '3.14'. lv_radius = 5. lv_circumference = 2 * lc_pi * lv_radius.
Scope of Variables
-
Local Variables: Declared within a subroutine or a block and only accessible within that block.
FORM calculate_sum. DATA: lv_local_sum TYPE I. lv_local_sum = lv_number1 + lv_number2. ENDFORM.
-
Global Variables: Declared at the beginning of the program and accessible throughout the program.
DATA: gv_global_count TYPE I.
Example Program
REPORT z_variables_example.
DATA: lv_count TYPE I VALUE 0,
lv_name TYPE STRING VALUE 'SAP'.
lv_count = lv_count + 10.
lv_name = 'ABAP Programming'.
WRITE: / 'Count:', lv_count,
/ 'Name:', lv_name.
Conclusion
This guide provides an overview of how to work with variables in ABAP. Mastering variables, their initialization, and manipulation is key to effective ABAP programming. For more complex use cases, refer to further SAP documentation or advanced tutorials.