DAY 12: Mastering ABAP - A Comprehensive Guide to Variables, Data Types, and Operators

On a journey of continuous learning π I share the concepts I learn daily through blogs. Learn concepts with me β one topic at a time.
#LearningInPublic #ContinuousLearning
1. What is a Variable in ABAP?
A variable is a named memory location used to store data during program execution. Its value can change while the program runs.
β Syntax
DATA variable_name TYPE data_type VALUE initial_value.
β Example
DATA gv_total TYPE i VALUE 10.
gv_total = gv_total + 5.
WRITE: / gv_total.
Output:
15
Types of Variables in ABAP
1οΈβ£ Local Variables
Declared using
DATAExist only within the program block
Recreated each time block runs
FORM calculate.
DATA lv_sum TYPE i.
lv_sum = 10 + 20.
WRITE: / 'Sum:', lv_sum.
ENDFORM.
β lv_sum exists only inside FORM.
2οΈβ£ Static Variables
Retain value between calls
Declared using
STATICS
FORM counter.
STATICS lv_count TYPE i.
lv_count = lv_count + 1.
WRITE: / 'Counter:', lv_count.
ENDFORM.
Each time counter runs β value increases.
3οΈβ£ Reference Variables
Used in Object-Oriented ABAP
DATA lr_obj TYPE REF TO object.
CREATE OBJECT lr_obj.
Stores a reference (pointer), not actual data.
4οΈβ£ System Variables (sy- fields)
Automatically maintained by SAP.
| System Field | Meaning |
| sy-uname | Current user |
| sy-datum | Current date |
| sy-uzeit | Current time |
| sy-subrc | Return code |
| sy-tabix | Current internal table index |
WRITE: / 'User:', sy-uname,
/ 'Date:', sy-datum.
5οΈβ£ Structured Variables
Used to group multiple fields.
TYPES: BEGIN OF ty_employee,
emp_id TYPE i,
emp_name TYPE string,
age TYPE i,
END OF ty_employee.
DATA ls_emp TYPE ty_employee.
ls_emp-emp_id = 101.
ls_emp-emp_name = 'Harish'.
ls_emp-age = 25.
WRITE: / ls_emp-emp_name.
2. What are Literals?
Literals are fixed values written directly in code.
πΈ Numeric Literals
DATA lv_num TYPE i VALUE 100.
DATA lv_pack TYPE p DECIMALS 2 VALUE '1234.50'.
πΈ Character Literals
Always enclosed in ' '.
DATA lv_text TYPE string VALUE 'Hello ABAP'.
3. What are Constants?
A constant is a fixed value that cannot change.
β Syntax
CONSTANTS constant_name TYPE type VALUE value.
β Example
CONSTANTS c_tax TYPE p DECIMALS 2 VALUE '0.18'.
If you try to change it:
c_tax = 0.20. " β Error
π Variable vs Constant
| Feature | Variable | Constant |
| Value changes | Yes | No |
| Keyword | DATA | CONSTANTS |
| Use case | Calculations | Fixed rules |
4. Data Types in ABAP
Elementary Types
| Type | Meaning | Example |
| C | Character | TYPE c LENGTH 10 |
| N | Numeric text | TYPE n LENGTH 6 |
| I | Integer | TYPE i |
| D | Date | TYPE d |
| T | Time | TYPE t |
| P | Packed decimal | TYPE p DECIMALS 2 |
| F | Float | TYPE f |
| STRING | Dynamic text | TYPE string |
| XSTRING | Binary data | TYPE xstring |
Example
DATA lv_date TYPE d.
lv_date = sy-datum.
WRITE: / lv_date.
Complex Types
Structure
TYPES: BEGIN OF ty_emp,
empid TYPE i,
name TYPE string,
END OF ty_emp.
Internal Table
DATA lt_emp TYPE TABLE OF ty_emp.
DATA ls_emp TYPE ty_emp.
ls_emp-empid = 1.
ls_emp-name = 'John'.
APPEND ls_emp TO lt_emp.
5. Input Methods in ABAP
PARAMETERS
PARAMETERS p_name TYPE string.
SELECT-OPTIONS
SELECT-OPTIONS s_date FOR sy-datum.
Radio Buttons
PARAMETERS r_yes RADIOBUTTON GROUP g1.
6. Arithmetic Operators
| Operator | Meaning |
| + | Add |
| - | Subtract |
| * | Multiply |
| / | Divide |
| MOD | Remainder |
| DIV | Integer division |
| ** | Power |
Example
DATA: a TYPE i VALUE 10,
b TYPE i VALUE 3,
result TYPE i.
result = a MOD b.
WRITE result.
Output:
1
7. Comparison Operators
| Operator | Meaning |
| \= / EQ | Equal |
| <> / NE | Not equal |
| \> / GT | Greater |
| < / LT | Less |
| BETWEEN | Range check |
| IS INITIAL | Empty check |
Example
IF a > b.
WRITE 'A is greater'.
ENDIF.
8. String Operators
| Operator | Meaning |
| CS | Contains string |
| CA | Contains any character |
| CP | Pattern match |
| && | Concatenation |
Example
DATA lv_full TYPE string.
lv_full = 'John' && ' ' && 'Doe'.
WRITE lv_full.
9. Modern Operators (7.4+)
VALUE Operator
DATA lt_numbers TYPE TABLE OF i.
lt_numbers = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
REDUCE Operator
DATA lv_sum TYPE i.
lv_sum = REDUCE i(
INIT x = 0
FOR wa IN lt_numbers
NEXT x = x + wa ).
COND (Ternary Alternative)
DATA lv_result TYPE string.
lv_result = COND string(
WHEN a > b THEN 'Greater'
ELSE 'Smaller' ).
10. Types of SELECT in ABAP
1. Basic SELECT
SELECT carrid carrname
FROM scarr
INTO TABLE @DATA(lt_carrier).
2. SELECT SINGLE
SELECT SINGLE *
FROM scarr
INTO @DATA(ls_scarr)
WHERE carrid = 'LH'.
3. SELECT DISTINCT
SELECT DISTINCT carrid
FROM sflight
INTO TABLE @DATA(lt_unique).
4. ORDER BY
SELECT carrid carrname
FROM scarr
INTO TABLE @DATA(lt_scarr)
ORDER BY carrname ASCENDING.
5. GROUP BY
SELECT carrid, COUNT(*) AS total
FROM sflight
GROUP BY carrid
INTO TABLE @DATA(lt_group).
6. INNER JOIN
SELECT a~carrid, b~fldate
FROM scarr AS a
INNER JOIN sflight AS b
ON a~carrid = b~carrid
INTO TABLE @DATA(lt_join).
7. LEFT OUTER JOIN
SELECT a~carrid, b~fldate
FROM scarr AS a
LEFT OUTER JOIN sflight AS b
ON a~carrid = b~carrid
INTO TABLE @DATA(lt_join).
8. FOR ALL ENTRIES
β Always check internal table not empty.
IF lt_scarr IS NOT INITIAL.
SELECT carrid fldate
FROM sflight
INTO TABLE @DATA(lt_flight)
FOR ALL ENTRIES IN @lt_scarr
WHERE carrid = @lt_scarr-carrid.
ENDIF.
β Important Rule for FOR ALL ENTRIES
If internal table is empty β system fetches ALL records.
Always write:
IF lt_table IS NOT INITIAL.
π― Final Understanding
Day 12 covers the core foundation of ABAP programming:
β Variables & Memory β Constants vs Variables β Data Types (Elementary & Complex) β Operators (Arithmetic, Logical, String) β Modern ABAP Operators β SELECT Variants β GROUP BY & ORDER BY β JOINS β FOR ALL ENTRIES



