Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’6 min read
DAY 12: Mastering ABAP - A Comprehensive Guide to Variables, Data Types, and Operators
R

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 DATA

  • Exist 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 FieldMeaning
sy-unameCurrent user
sy-datumCurrent date
sy-uzeitCurrent time
sy-subrcReturn code
sy-tabixCurrent 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

FeatureVariableConstant
Value changesYesNo
KeywordDATACONSTANTS
Use caseCalculationsFixed rules

4. Data Types in ABAP

Elementary Types

TypeMeaningExample
CCharacterTYPE c LENGTH 10
NNumeric textTYPE n LENGTH 6
IIntegerTYPE i
DDateTYPE d
TTimeTYPE t
PPacked decimalTYPE p DECIMALS 2
FFloatTYPE f
STRINGDynamic textTYPE string
XSTRINGBinary dataTYPE 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

OperatorMeaning
+Add
-Subtract
*Multiply
/Divide
MODRemainder
DIVInteger 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

OperatorMeaning
\= / EQEqual
<> / NENot equal
\> / GTGreater
< / LTLess
BETWEENRange check
IS INITIALEmpty check

Example

IF a > b.
  WRITE 'A is greater'.
ENDIF.

8. String Operators

OperatorMeaning
CSContains string
CAContains any character
CPPattern 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

More from this blog