Variables & Declarations in ABAP #
Variables #
Objectives #
- Understand what a
VARIABLEis in ABAP - Know how to declare and correctly name a
VARIABLE - Identify available DATA TYPES
- Distinguish between TYPE and LIKE syntax
Definition #
A
VARIABLEis a memory space associated with a name that allows storing a temporary value.
This value can change during program execution.
A
VARIABLEworks like a labeled box, a named container: you can place data in it, modify it, or replace it.
Declaration with DATA #
Simple Declaration #
WRITE:/ ’ - SIMPLE DECLARATION…'.
DATA lv_variable TYPE string.
DATAdeclares aVARIABLE.lv_variableis its name.TYPE stringindicates that it stores a character string.
Successive Declarations #
WRITE:/ ’ - SUCCESSIVE DECLARATION…'.
DATA: lv_variable1 TYPE string, lv_variable2 TYPE i, lv_variable3 TYPE c1.
DATA:declares a succession ofVARIABLES.lv_variable1→ text (character string)lv_variable2→ integer (number)lv_variable3→ fixed character of 10 characters
Grouping declarations makes code more readable and facilitates maintenance.
Naming Conventions #
| Rule | Example | Purpose |
|---|---|---|
| Clear and descriptive names | lv_age, lv_total |
Facilitate understanding |
Prefix lv_ for local VARIABLES |
lv_name |
Indicates it’s local |
Prefix gv_ for global VARIABLES |
gv_company |
Accessible throughout the program |
| No reserved keywords | ✅ lv_count / ❌ DATA |
Avoid compilation errors |
| Declare at program beginning | — | Simplifies review and maintenance |
Naming a
VARIABLEis like naming a folder on your computer: a precise name makes it easy to find what it contains.
TYPE vs LIKE #
Two syntax options exist for declaring a VARIABLE.
TYPE #
DATA lv_variable TYPE string.
Explicitly defines the data type.
LIKE #
DATA lv_other_variable LIKE lv_variable.
Copies the same type and length as another VARIABLE.
TYPE → reference to a data type.
LIKE → reference to anotherVARIABLE.
WRITE:/ ’ - DECLARATION… LIKE…'.
DATA: lv_variable4 TYPE string, lv_variable5 LIKE lv_variable4, lv_variable6 LIKE lv_variable5.
Basic Data Types #
Type C (character) #
WRITE:/ ’ - TYPE CHAR…'.
DATA: lv_firstname TYPE char255, lv_lastname(9) TYPE c. lv_firstname = 'John'. lv_lastname = 'Wick'.
Stores fixed text strings (names, addresses, labels, etc.).
Type I / N / NUMC (numeric) #
| Type | Description | Usage Example |
|---|---|---|
I |
Integer number (arithmetic) | Calculations, loops |
N |
Number stored as text (unsigned) | Manipulation of digits as text |
NUMC |
Numeric characters | Postal codes, account numbers |
WRITE:/ ’ - TYPE INTEGER…'.
DATA: lv_integer TYPE i, lv_year TYPE n, lv_numeric_string TYPE numc2. lv_integer = 10. lv_year = 2025. lv_numeric_string = 31000.
I= actual numeric value, used for calculation.
N/NUMC= text containing only digits.
Type D (date) and SY-DATUM #
WRITE:/ ’ - TYPE DATE…'.
DATA: lv_date TYPE d, lv_date2 TYPE datum. lv_date = sy-datum. lv_date2 = 19861102.
Format: YYYYMMDD
Example: 20251029 → October 29, 2025.
Type T (time) and SY-UZEIT #
WRITE:/ ’ - TYPE TIME…'.
DATA: lv_time TYPE t, lv_time2 TYPE uzeit. lv_time = sy-uzeit. lv_time2 = 183045.
Format: HHMMSS
Example: 183045 → 6:30:45 PM.
Type F (fixed floating point) / FLOAT (variable precision) #
WRITE:/ ’ - TYPE FLOAT…'.
DATA: lv_f TYPE f, lv_float TYPE float. lv_f = '123455.12'. lv_float = '123455.12'.
Type F handles decimal values with defined precision. As for FLOAT type, it’s a real value with variable precision, useful for scientific calculations.
Type DECFLOAT #
WRITE:/ ’ - TYPE DECFLOAT…'.
DATA: lv_decfloat TYPE decfloat34. lv_decfloat = '12345.67890123456789012345678901234'.
Precision up to 34 significant digits - used for financial calculations.
Type P (packed decimal) #
WRITE:/ ’ - TYPE PACKED DECIMAL…'.
DATA: lv_pack TYPE p DECIMALS 2. lv_pack = '1234.56'.
Compact numeric type, ideal for financial amounts.
Type STRING #
WRITE:/ ’ - TYPE STRING…'.
DATA: lv_string TYPE string. lv_string = 'Hello World'.
Variable length, useful for dynamic texts.
Type BOOLEAN / ABAP_BOOL #
WRITE:/ ’ - TYPE BOOLEAN…'.
DATA: lv_bool TYPE boolean, lv_boolean TYPE abap_bool. lv_bool = abap_true. lv_boolean = abap_false.
Possible values: abap_true (true) / abap_false (false)
Always initialize explicitly:
DATA lv_flag TYPE boolean VALUE abap_true.
Type XFELD #
WRITE:/ ’ - TYPE XFELD…'.
DATA: lv_xfeld TYPE xfeld VALUE 'X'. lv_xfeld = 'X'. "Check lv_xfeld = ' '. "Uncheck
Represents SAP checkboxes:
‘X’ = checked, ’ ’ = unchecked.
Prefer XFELD to visually represent binary states in SAP screens.
Best Practices #
| Best Practice | Importance |
|---|---|
Declare all VARIABLES at the beginning |
Readability and consistency |
| Use explicit names | Facilitates understanding of each data’s role |
| Group declarations by theme | Structure and clarity |
Always initialize VARIABLES |
Avoids unexpected behavior |
| Don’t mix types (e.g., text/num) | Prevents conversion errors |
Clear names and rigor in declaration prevent 80% of logic errors in ABAP.
Summary #
- A VARIABLE stores a modifiable value during the program.
- It’s declared with DATA and follows strict conventions (lv*, gv*).
- TYPE and LIKE are used to define or copy a type structure.
- Most used types: C, I, N, NUMC, D, T, F, STRING, BOOLEAN, XFELD.
- Always initialize, document, and clearly name
VARIABLES.
Constants #
Objectives #
- Understand what a CONSTANT is in ABAP
- Know how to declare a CONSTANT with
CONSTANTS - Apply naming conventions
- Identify possible data types
- Differentiate CONSTANTS and VARIABLES
Definition #
A
CONSTANTis a fixed value associated with a name.
Unlike a variable, its value never changes during program execution.
A
CONSTANTis like a fixed marker on a map: it doesn’t move.
A variable, on the other hand, can move.
Imagine a watch whose battery is dead: the value remains frozen.
A variable would be a normal thermometer, which changes with temperature.
Declaration with CONSTANTS #
Simple Syntax #
WRITE:/ ’ - SIMPLE DECLARATION…’.
CONSTANTS: lc_valeur_const TYPE i VALUE 10.
CONSTANTS: keyword to declare aCONSTANTlc_valeur_const: name of theCONSTANTTYPE i: integer data typeVALUE 10: fixed value assigned to theCONSTANT
It’s like setting multiple markers on the same map.
The
CONSTANTSkeyword accepts a list separated by:to declare multiple consecutiveCONSTANTS.
Successive Declarations #
WRITE:/ ’ - SUCCESSIVE DECLARATION…'.
CONSTANTS: lc_max_users TYPE i VALUE 100, lc_timeout TYPE i VALUE 30, lc_flag TYPE abap_bool VALUE abap_false.
- Use the
CONSTANTS:syntax followed by a comma-separated list to declare multipleCONSTANTSin a single statement. - Each CONSTANT must specify
TYPEandVALUEor useLIKE+VALUE.
Grouping
CONSTANTSby theme (parameters, codes, formats) helps with maintenance.
Naming Conventions #
| Rule | Example | Purpose |
|---|---|---|
| Descriptive names | lc_max_value |
Quickly understand the CONSTANT’s role |
Prefix lc_ for local |
lc_discount |
Indicates it’s local to the program |
Prefix gc_ for global |
gc_company |
Accessible from the entire program |
| No reserved keywords | ✅ lc_count / ❌ CONSTANTS |
Avoid compilation errors |
| Declare at program beginning | — | Facilitates reading and maintenance |
A
CONSTANTis a fixed sign: its label must be clear because it will never change.
TYPE vs LIKE #
TYPE #
CONSTANTS: lc_valeur_const TYPE i VALUE 10.
- Explicitly defines the type.
LIKE #
DATA: lv_variable TYPE i VALUE 5.
CONSTANTS: lc_valeur_const LIKE lv_variable VALUE 20.
- Copies the type of another variable, but remains immutable.
TYPE → you choose the shape of your box.
LIKE → you copy someone else’s box, but you can’t open it anymore.
Data Types for Constants #
Integer / Numeric / NUMC #
WRITE:/ ’ - TYPE INTEGER, N, NUMC…'.
CONSTANTS: lc_integer TYPE I VALUE 10, lc_max_value TYPE N VALUE 9999, lc_account_number TYPE NUMC10 VALUE 1234567890.
- I → integers for calculations
- N / NUMC → numeric text (e.g., postal code)
Float / F #
WRITE:/ ’ - TYPE F, FLOAT…'.
CONSTANTS: lc_fixed_value TYPE F VALUE '1234.56', lc_float_value TYPE FLOAT VALUE '1234.567890123456789'.
- F → fixed decimals
- FLOAT → variable precision (scientific)
DECFLOAT #
WRITE:/ ’ - TYPE DECFLOAT…'.
CONSTANTS: lc_decfloat TYPE DECFLOAT34 VALUE '12345.67890123456789012345678901234'.
- High precision, useful for financial calculations.
String #
WRITE:/ ’ - TYPE STRING…'.
CONSTANTS: lc_string TYPE STRING VALUE 'Hello, World!'.
- Variable-length text, non-modifiable.
Character #
WRITE:/ ’ - TYPE CHAR…'.
CONSTANTS: lc_char TYPE C LENGTH 1 VALUE 'F'.
- Fixed character (e.g., initial, status).
Boolean / ABAP_BOOL #
WRITE:/ ’ - TYPE BOOLEAN…'.
CONSTANTS: lc_bool TYPE BOOLEAN VALUE ABAP_TRUE, lc_boolean TYPE ABAP_BOOL VALUE ABAP_FALSE.
- Logical values: true or false.
Date (D) and SY-DATUM #
WRITE:/ ’ - TYPE DATE…'.
CONSTANTS: lc_date1 TYPE D VALUE SY-DATUM, lc_date2 TYPE D VALUE 19861102.
- Format YYYYMMDD.
Time (T) and SY-UZEIT #
WRITE:/ ’ - TYPE TIME…'.
CONSTANTS: lc_time1 TYPE T VALUE SY-UZEIT, lc_time2 TYPE T VALUE 183045.
- Format HHMMSS.
Packed Decimal (P) #
WRITE:/ ’ - TYPE PACKED…'.
CONSTANTS: lc_pi TYPE P DECIMALS 2 VALUE '3.14'.
- Fixed value for amounts or financial calculations.
XFELD #
WRITE:/ ’ - TYPE XFELD…'.
CONSTANTS: lc_xfeld TYPE XFELD VALUE 'X'.
- ‘X’ = checked / ’ ’ = unchecked.
Best Practices #
| Best Practice | Explanation |
|---|---|
| Always initialize CONSTANT with VALUE | It cannot be modified afterwards |
| Use explicit and readable names | Allows quick understanding of the marker’s role |
Group CONSTANTS by theme |
Facilitates maintenance |
Prefer CONSTANTS to hardcoded values |
Improves readability and avoids errors |
Declare CONSTANTS before variables |
Good ABAP code structure rule |
Always replace repeated numbers or texts with a
CONSTANT: your code will be clearer and easier to maintain.
Summary #
- A
CONSTANTkeeps a fixed value throughout the program.- Declared with
CONSTANTS ... VALUE.TYPEandLIKEdefine or copy an existing type.- Main types: I, N, NUMC, F, DECFLOAT, STRING, C, BOOLEAN, D, T, P, XFELD.
- Always prefer a
CONSTANTto a “hardcoded” value.CONSTANTSimprove clarity, security, and maintainability of ABAP code.
Field-symbols #
Objectives #
- Understand what a
FIELD-SYMBOLis in ABAP - Know how to declare a
FIELD-SYMBOLwithFIELD-SYMBOLS - Apply naming conventions for
FIELD-SYMBOLS - Differentiate
FIELD-SYMBOL, VARIABLE, and CONSTANT - Understand the assignment principle and why it’s mandatory
Definition #
A
FIELD-SYMBOLis a special variable that acts as a pointer or reference to a memory area.
Modifying aFIELD-SYMBOLmeans directly modifying the pointed data, not theFIELD-SYMBOLitself.
In other words, it doesn’t contain its own value but references another variable, a table field, or an expression result.
A
FIELD-SYMBOLis like an arrow on a map: the arrow doesn’t contain the object, it just indicates where it’s located.Moving or changing the arrow doesn’t change the object; but using the arrow to modify the area directly changes the object.
Purpose #
- Traverse internal tables without copying rows.
- Work on dynamic or unknown structures in advance.
- Gain performance and flexibility in code.
Principle #
- A
FIELD-SYMBOLacts as a temporary alias. - It’s declared with
FIELD-SYMBOLS, then associated with a variable usingASSIGN. - After association, any modification via the
FIELD-SYMBOLdirectly modifies the original data.
Declaration with FIELD-SYMBOLS #
Simple Syntax #
FIELD-SYMBOLS <lfs_> TYPE any.
ASSIGN <variable> TO <lfs_>.
IF <lfs_> IS ASSIGNED.
<lfs_> = 'New value'.
ENDIF.
Example:
DATA lv_text TYPE string VALUE 'Hello'.
FIELD-SYMBOLS <lfs_string> TYPE string.
ASSIGN lv_text TO <lfs_string>.
<lfs_string> = 'Hi'.
WRITE:/ <lfs_string>.
Multiple Declaration #
FIELD-SYMBOLS: <lfs_field1> TYPE any,
<lfs_field2> TYPE any,
<lfs_field3> TYPE any.
<lfs_integer>: name of theFIELD-SYMBOLbetween angle brackets< >TYPE: type of pointed data (i, string, any, etc.)- Tip:
TYPE ANYcan be used if theFIELD-SYMBOLwill point to different data types or an ABAP object.
TYPE ANYis very useful forFIELD-SYMBOLSthat will point to different types of objects or dynamic structures.
The Assignment Principle #
A
FIELD-SYMBOLcontains no value before being assigned.
Any attempt to write directly without assignment causes aCX_SY_REF_IS_INITIALerror.
Invalid Example #
FIELD-SYMBOLS <lfs_num> TYPE i.
<lfs_num> = 10. "ERROR! Not assigned
Correct Example #
DATA lv_number TYPE i.
FIELD-SYMBOLS <lfs_num> TYPE i.
ASSIGN lv_number TO <lfs_num>.
<lfs_num> = 10. "OK, directly modifies lv_number
ASSIGN: binds theFIELD-SYMBOLto an existing variable.- Then, any modification via the
FIELD-SYMBOLaffects the target variable. - Analogy: we point the arrow at the correct sign before being able to modify it.
Perfect Example #
DATA: lv_prenom2 TYPE string VALUE 'FRED'.
FIELD-SYMBOLS: <lfs_prenom> TYPE string.
ASSIGN lv_prenom2 TO <lfs_prenom>.
IF <lfs_prenom> IS ASSIGNED.
<lfs_prenom> = 'PIERRE'.
WRITE:/ <lfs_prenom>,
/ lv_prenom2.
ENDIF.
To be sure that a Field-Symbol is assigned, you can check the assignment as in the example above via the condition
IF <lfs_> IS ASSIGNED. "Processing here if condition is true ENDIF.
Field-symbol and Object #
For information for the CLASS module
For an object:
DATA lo_obj TYPE REF TO zcl_my_class.
FIELD-SYMBOLS <lfs_obj> TYPE zcl_my_class.
CREATE OBJECT lo_obj.
ASSIGN lo_obj->* TO <lfs_obj>. "Now <lfs_obj> points to the object
Naming Conventions #
| Rule | Example | Purpose |
|---|---|---|
Prefix <lfs_> for local |
<lfs_customer> |
Indicates FIELD-SYMBOL with local scope |
Prefix <gfs_> for global |
<gfs_order> |
Globally accessible |
| Based on structure or table | <lfs_customer_name> |
Clarifies pointer destination |
| Based on data type | <lfs_string> |
Indicates pointed type |
| Based on domain | <lfs_amount> |
Facilitates reading for financial data |
| Generic names | <lfs_generic> |
Used when destination is non-specific |
Usage #
- Always assign before use:
ASSIGN lv_variable TO <lfs_integer>.
- Any modification via
<lfs_integer>will directly affectlv_variable. - Never write to an unassigned
FIELD-SYMBOL.
Summary #
FIELD-SYMBOL= pointer to a memory area, not a direct value- Declare with
FIELD-SYMBOLS <name> TYPE ...- Always assign with
ASSIGNbefore any use- Prefixes
<lfs_>(local) or<gfs_>(global) for readabilityTYPE ANYuseful for objects or variable types- General analogy: arrow on a map, it points to the target, modification is done on the target, not on the arrow.