Chapter 04 ยท Foundations
Data Types, Variables & System Fields
The building blocks that let your programs store, change, and display real data โ in ABAP Cloud and Classic ABAP.
In Chapter 3, you ran your first ABAP program. It printed a fixed greeting. That was a milestone, but the greeting was hard-coded โ the program always said the same thing, no matter what.
Real ABAP programs are not like that. They read data from databases, receive input from users, calculate values, and respond dynamically. To do any of that, you need to understand variables โ named containers that hold changeable values โ and the data types that define what kind of value they can hold.
This chapter also covers one of the most underrated topics in ABAP: system fields. These are built-in variables that SAP updates automatically after every important operation, and they are how your programs know whether something succeeded or failed. Every interview tests system fields. This chapter teaches them properly.
๐ฏ Who Is This Chapter For?
This chapter serves both types of ABAP learners. Here is how to read it:
- If you are on the FREE BTP Trial โ follow the ๐ข green dual path boxes. You will use Eclipse ADT and ABAP Cloud syntax.
- If you have PAID SAP access โ follow the ๐ต blue dual path boxes. You will learn Cloud syntax plus the Classic ABAP variants of the same concepts.
- Both paths teach the same core ideas: data types, variables, constants, structures, and system fields. The syntax you learn in Cloud works in Classic too โ with small differences that this chapter highlights.
๐ฏ What You Will Learn in This Chapter
- The core ABAP data types and when to use each one
- How to declare variables with
DATAand assign values - How to declare constants with
CONSTANTSso values cannot be changed - How to group related variables into a structure
- The most important system fields and what they tell you
- How
sy-subrcandsy-tabixpower real ABAP logic - How to check system fields to handle errors gracefully
๐งญ Which Path Should I Choose?
Before writing code, decide which path applies to you. This chapter has two tracks running in parallel.
Two learner types โ find yourself
WRITE) are not available to you.
What You Can Do With Each Path
| Task | ๐ข Free Path (BTP Trial) | ๐ต Paid Path (SAP System) |
|---|---|---|
| Declare variables | โ
DATA in Cloud class | โ
DATA in SE38 report |
| Declare constants | โ
CONSTANTS | โ
CONSTANTS |
| Structures | โ Full support | โ Full support |
| System fields | โ
sy-* fields available | โ
sy-* fields available |
| Output method | cl_demo_output | cl_demo_output OR WRITE |
Testing sy-subrc after DB access | Limited (CDS + RAP later) | Full (Open SQL, function modules, BAPIs) |
| Editor | Eclipse ADT | Eclipse ADT + SAP GUI SE38 |
๐ The Core ABAP Data Types
A data type tells the system what kind of value a variable can hold, and how much space it needs. In ABAP, types are defined with single letters for primitive types, and the lengths are specified in parentheses.
Here are the ones you will use most often:
| Type | Name | Holds | Example | Default Length |
|---|---|---|---|---|
| I | Integer | Whole numbers | 42, -100, 0 | 4 bytes (fixed) |
| P | Packed decimal | Decimal numbers | 3.14, 1234.56 | 8 bytes |
| F | Float | Very large / scientific | 1.5E+10 | 8 bytes (fixed) |
| C | Character | Text | ‘Rahul’, ‘Pune’ | 1 char |
| N | Numeric text | Numbers kept as text | ‘007’, ‘2026’ | 1 char |
| D | Date | Dates | ‘20260128’ | 8 chars (YYYYMMDD) |
| T | Time | Time of day | ‘143000’ | 6 chars (HHMMSS) |
| STRING | String | Variable-length text | ‘Any length’ | Unlimited |
| XSTRING | Byte string | Binary data | file content | Unlimited |
Cloud Syntax
In ABAP Cloud, you declare variables inside a method. The type letters are the same as in Classic ABAP.
- Open your Cloud class in Eclipse ADT
- Inside
METHOD run, declareDATAvariables - Use type letters I, P, C, D, N, STRING
- Activate and run
Both Syntaxes
The same type letters work in Classic ABAP. You will write it twice: once in your Cloud class and once in SE38.
- Cloud class in Eclipse ADT (same as free)
- Plus a report in SE38 with the same variables
- Compare the two โ the DATA syntax is identical
๐ฆ Declaring and Using Variables
A variable is a named container. You declare it with the DATA statement, giving it a name and a type.
Basic Declaration
DATA lv_name TYPE string.
DATA lv_age TYPE i.
DATA lv_salary TYPE p DECIMALS 2.
DATA lv_city TYPE c LENGTH 20.
Let’s decode each one:
lv_nameโ a string, no fixed lengthlv_ageโ an integer, holds whole numberslv_salaryโ a packed decimal with 2 decimal placeslv_cityโ a character field, 20 characters wide
lv_ prefix stands for “local variable”. It is the most common naming convention in ABAP. Other prefixes you will see: gv_ (global variable), ls_ (local structure), lt_ (local table), lv_ (local variable). Following these conventions is expected in real projects.
Assigning Values
There are three ways to put a value into a variable:
" Method 1: Direct assignment
lv_name = 'Rahul'.
lv_age = 28.
lv_city = 'Pune'.
" Method 2: Declare and assign in one line
DATA(lv_country) = 'India'.
" Method 3: Using VALUE (modern ABAP style)
DATA(lv_status) = VALUE string( 'Active' ).
Reading Variable Values
In ABAP Cloud, you output variable values with cl_demo_output:
cl_demo_output=>write( 'Name:' && lv_name ).
cl_demo_output=>write( 'Age:' && lv_age ).
cl_demo_output=>write( 'City:' && lv_city ).
cl_demo_output=>display( ).
The && operator concatenates strings. In the output above, the variable name is merged with the label text.
&&, ABAP automatically converts it. But the result is unformatted. For nicer output, use the |...| string template syntax: |Name: { lv_name }|.
๐ Constants โ Values That Never Change
A constant is a variable whose value is fixed at declaration and cannot be changed afterwards. If you try to change it, the syntax check fails.
CONSTANTS c_pi TYPE p DECIMALS 5 VALUE '3.14159'.
CONSTANTS c_tax_rate TYPE p DECIMALS 2 VALUE '0.18'.
CONSTANTS c_company TYPE string VALUE 'eLearning Solutions'.
Constants are declared with CONSTANTS instead of DATA, and they use VALUE for the initial assignment. The c_ prefix is the naming convention.
Constants in Cloud
Declare constants at the top of your method, before any logic. Use the c_ prefix.
Constants in Classic
In SE38, you can declare constants at the top of the report, before the START-OF-SELECTION event. Same syntax, same prefix.
๐๏ธ Structures โ Grouping Related Variables
A structure is a group of related variables under one name. Each field inside the structure has its own type.
TYPES: BEGIN OF ty_employee,
name TYPE string,
age TYPE i,
salary TYPE p DECIMALS 2,
city TYPE c LENGTH 20,
END OF ty_employee.
DATA ls_employee TYPE ty_employee.
Now you can fill the structure field by field:
ls_employee-name = 'Rahul'.
ls_employee-age = 28.
ls_employee-salary = '45000.00'.
ls_employee-city = 'Pune'.
The hyphen (-) accesses individual fields of the structure. This is cleaner than declaring four separate variables when they logically belong together.
ls_ prefix means “local structure”. Similarly, lt_ means “local table” (which you will learn in Chapter 7). Using the right prefix makes code self-documenting.
โ๏ธ System Fields โ ABAP’s Built-In Variables
System fields are special variables that SAP updates automatically after every important operation. You do not declare them. You just read them. They always start with sy-.
Here are the most important ones:
| Field | What It Holds | When It Updates |
|---|---|---|
| sy-subrc | Return code (0 = success, non-zero = failure) | After database access, function module calls, and many statements |
| sy-tabix | Index of the current row in an internal table | During LOOP, READ TABLE, and similar operations |
| sy-datum | Current system date (YYYYMMDD) | At the start of the program |
| sy-uzeit | Current system time (HHMMSS) | At the start of the program |
| sy-uname | The logged-in user’s name | Always current |
| sy-mandt | The client number | Always current |
| sy-index | Loop counter in DO and WHILE loops | Every loop iteration |
| sy-msgid | Message class ID (after MESSAGE statement) | After a message is raised |
The most important one: sy-subrc
sy-subrc stands for “system subreturn code”. It is ABAP’s way of telling you whether the previous operation succeeded. 0 means success. Any non-zero value means something went wrong.
Example: after a database read, you must check sy-subrc:
SELECT SINGLE * FROM zemployees
INTO @DATA(ls_emp)
WHERE id = @lv_id.
IF sy-subrc = 0.
cl_demo_output=>write( 'Record found!' ).
ELSE.
cl_demo_output=>write( 'No record found for that ID.' ).
ENDIF.
cl_demo_output=>display( ).
sy-subrc after every database call, function module call, or BAPI call is mandatory. Interviewers will test whether you automatically check it. If you write code that ignores sy-subrc, you will be seen as a junior developer.
sy-tabix โ the loop index
sy-tabix gives you the row number during internal table operations. You will use it heavily once you reach Chapter 7, but a small preview:
LOOP AT lt_employees INTO ls_emp.
cl_demo_output=>write( 'Row' && sy-tabix && ':' && ls_emp-name ).
ENDLOOP.
sy-datum and sy-uzeit โ the current date and time
These give you the system’s current date and time. Example usage:
cl_demo_output=>write( 'Today is:' && sy-datum ).
cl_demo_output=>write( 'Time is:' && sy-uzeit ).
cl_demo_output=>write( 'User:' && sy-uname ).
System Fields in Cloud
All sy-* fields work in ABAP Cloud. Some are less relevant early on (like sy-subrc after Open SQL, which Cloud does not allow the same way). You will see them again in RAP and CDS work.
System Fields in Classic
In Classic ABAP, sy-subrc is used constantly โ after every SELECT, CALL FUNCTION, and BAPI. This is where you practice it most.
๐ Step 1: Build a Program That Uses Variables (Both Paths)
Let’s put it all together. You will build a small program that stores employee data in variables, uses constants, groups fields into a structure, and reads system fields.
๐ข Free Path (ABAP Cloud in Eclipse ADT)
Open your ABAP Cloud project. Create a new class called ZCL_DATA_TYPES_CH4 in your package ZHELLO_WORLD.
CLASS zcl_data_types_ch4 DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
CLASS-METHODS run.
ENDCLASS.
CLASS zcl_data_types_ch4 IMPLEMENTATION.
METHOD run.
" Simple variables
DATA lv_name TYPE string.
DATA lv_age TYPE i.
DATA lv_city TYPE string.
DATA lv_salary TYPE p DECIMALS 2.
" Constant
CONSTANTS c_company TYPE string VALUE 'eLearning Solutions'.
" Structure
TYPES: BEGIN OF ty_employee,
name TYPE string,
age TYPE i,
city TYPE string,
salary TYPE p DECIMALS 2,
END OF ty_employee.
DATA ls_employee TYPE ty_employee.
" Fill the simple variables
lv_name = 'Rahul'.
lv_age = 28.
lv_city = 'Pune'.
lv_salary = '45000.00'.
" Fill the structure
ls_employee-name = lv_name.
ls_employee-age = lv_age.
ls_employee-city = lv_city.
ls_employee-salary = lv_salary.
" Output the data
cl_demo_output=>write( '--- Employee Info ---' ).
cl_demo_output=>write( |Name: { ls_employee-name }| ).
cl_demo_output=>write( |Age: { ls_employee-age }| ).
cl_demo_output=>write( |City: { ls_employee-city }| ).
cl_demo_output=>write( |Salary: { ls_employee-salary }| ).
" Output constants and system fields
cl_demo_output=>write( '--- Context ---' ).
cl_demo_output=>write( |Company: { c_company }| ).
cl_demo_output=>write( |Today: { sy-datum }| ).
cl_demo_output=>write( |Time: { sy-uzeit }| ).
cl_demo_output=>write( |User: { sy-uname }| ).
cl_demo_output=>display( ).
ENDMETHOD.
ENDCLASS.
Save with Ctrl + S, activate with Ctrl + F3, and run with F9. You should see all the employee information, the company constant, and the system fields displayed in the console.
๐ต Paid Path (Classic ABAP in SE38)
Now open SAP GUI, go to SE38, and create a report called ZDATATYPES_CH4_REPORT with the same logic. The syntax is nearly identical โ only the output method changes.
REPORT zdatatypes_ch4_report.
DATA: lv_name TYPE string,
lv_age TYPE i,
lv_city TYPE string,
lv_salary TYPE p DECIMALS 2.
CONSTANTS c_company TYPE string VALUE 'eLearning Solutions'.
TYPES: BEGIN OF ty_employee,
name TYPE string,
age TYPE i,
city TYPE string,
salary TYPE p DECIMALS 2,
END OF ty_employee.
DATA ls_employee TYPE ty_employee.
lv_name = 'Rahul'.
lv_age = 28.
lv_city = 'Pune'.
lv_salary = '45000.00'.
ls_employee-name = lv_name.
ls_employee-age = lv_age.
ls_employee-city = lv_city.
ls_employee-salary = lv_salary.
WRITE: / '--- Employee Info ---',
/ 'Name:', ls_employee-name,
/ 'Age:', ls_employee-age,
/ 'City:', ls_employee-city,
/ 'Salary:', ls_employee-salary.
WRITE: / '--- Context ---',
/ 'Company:', c_company,
/ 'Today:', sy-datum,
/ 'Time:', sy-uzeit,
/ 'User:', sy-uname.
Check syntax with Ctrl + F2, activate with Ctrl + F2, and run with F8.
cl_demo_output=>write( ) in Cloud, WRITE in Classic. This is how it will be for most of the syntax you learn.
๐งฎ Step 2: Add Simple Arithmetic
Variables exist to be manipulated. Let’s add a small calculation to demonstrate.
DATA lv_hours TYPE i VALUE 160.
DATA lv_rate TYPE p DECIMALS 2 VALUE '500.00'.
DATA lv_total TYPE p DECIMALS 2.
lv_total = lv_hours * lv_rate.
cl_demo_output=>write( |Total monthly pay: { lv_total }| ).
cl_demo_output=>display( ).
ABAP supports the standard arithmetic operators: +, -, *, /, and the remainder operator MOD. The result of a calculation stored into a variable keeps the type of the target variable.
I (integer), you lose decimals. Always use type P for financial calculations. Use DECIMALS n to specify how many decimal places you need.
๐ง What Just Happened Under the Hood
Understanding how ABAP treats data at a lower level will help you avoid subtle bugs.
| Concept | What It Means in Practice |
|---|---|
| Type checking | If you try to assign a string to an integer variable, ABAP either converts it (if possible) or throws a runtime error. The type system protects you. |
| Automatic conversion | ABAP converts between compatible types automatically โ for example, integer to string, or string to integer if the string contains digits. |
| Initial values | Every variable has an “initial value”: 0 for numbers, empty string for characters, ‘00000000’ for dates. You do not need to set them explicitly. |
| sy-subrc | Updated after every operation that can fail. Always check it immediately โ the next operation can overwrite it. |
| Constants | Stored once and cannot be changed. Using them prevents accidental modification and makes refactoring easier. |
| Structures | A single variable that contains multiple fields. Accessing a field uses the hyphen syntax: ls_employee-name. |
๐ Chapter Summary
- The core ABAP data types are I (integer), P (packed decimal), C (character), N (numeric text), D (date), T (time), and STRING.
- Variables are declared with
DATA. Thelv_prefix means “local variable”. - Constants are declared with
CONSTANTSand cannot be changed. Thec_prefix means “constant”. - Structures group related variables under one name. Use the hyphen to access fields:
ls_employee-name. - System fields start with
sy-and are updated automatically by SAP. sy-subrcis the most important one โ 0 means success, non-zero means failure. Always check it.sy-tabixgives you the current row index during internal table loops.sy-datum,sy-uzeit, andsy-unamegive you the current date, time, and user.- The syntax for variables and constants is identical in Cloud and Classic ABAP. Only the output method differs.
๐ค Interview Questions & Answers
These are the most common ABAP beginner interview questions about data types, variables, and system fields. Know these answers cold.
Type I is an integer โ a whole number with no decimals, stored in 4 bytes. Use it for counts, IDs, loop counters, and other quantities that never need decimals.
Type P is a packed decimal โ a number that can hold decimals, stored in a length you specify. It is the standard type for money and financial calculations because it retains precision. Use DECIMALS n to say how many decimal places you need.
Rule of thumb: if you would never write a decimal point in the value, use I. If you might, use P.
DATA declares a variable whose value can be changed at any time during program execution.
CONSTANTS declares a value that is fixed at declaration and cannot be changed afterwards. If your code tries to modify a constant, the syntax check fails.
Use constants for values that should never change (tax rates, company names, system IDs). They prevent accidental modification and make refactoring easier.
sy-subrc stands for “system subreturn code”. It is a system field that SAP updates automatically after every operation that can succeed or fail โ database reads, function module calls, BAPI calls, and many others.
A value of 0 means success. Any non-zero value means failure or an unexpected result, with each number representing a specific error condition.
It is critically important because it is how your program knows whether an operation worked. Ignoring sy-subrc is one of the most common beginner mistakes, and interviewers will test whether you check it automatically.
A structure is a group of related variables declared under one name. Each field inside has its own type. You define it with TYPES: BEGIN OF ... END OF ..., then declare a variable of that type with DATA.
You access individual fields with the hyphen: ls_employee-name, ls_employee-age.
Use a structure when several values logically belong together โ like the fields of an employee record, or the details of an invoice line. It keeps code organised and makes it easier to pass groups of related values as a single parameter.
sy-subrc โ return code after an operation. 0 means success.
sy-tabix โ the current row index during an internal table operation, especially inside a LOOP AT.
sy-datum โ the current system date in YYYYMMDD format.
Other common ones: sy-uzeit (current time), sy-uname (logged-in user), sy-mandt (client number), sy-index (loop counter in DO/WHILE loops).
Yes. The syntax for DATA, CONSTANTS, and TYPES is essentially identical in both environments. The type letters (I, P, C, N, D, T, STRING) work the same way, and the naming conventions (lv_, c_, ls_) are the same.
The main differences are in output: ABAP Cloud uses cl_demo_output, while Classic ABAP can use either cl_demo_output or the traditional WRITE statement. Some system fields (like sy-subrc after Open SQL) are used more heavily in Classic ABAP, but they exist in both.
Learning the fundamentals in either environment prepares you for both.
๐ Practice Task for This Chapter
Complete the tasks for your path before moving to Chapter 5. Each takes about 20 minutes.
๐ข Free Path (BTP Trial)
- Create a new class called
ZCL_PRACTICE_CH4in your package. - Add a static method called
run. - Inside the method, declare:
- A variable for your name (string)
- A variable for your age (integer)
- A variable for your monthly salary (packed decimal, 2 decimals)
- A constant for your company name
- Create a structure called
ty_profilewith fields for name, age, city, and salary. - Fill the structure with values.
- Output everything using
cl_demo_output=>write( ), includingsy-datum,sy-uzeit, andsy-uname. - Save, activate (Ctrl + F3), and run (F9). Take a screenshot.
๐ต Paid Path (SAP System)
- Complete all the free path steps above in ABAP Cloud (Eclipse ADT).
- Then open SE38 in SAP GUI and create a report called
ZPRACTICE_CH4_REPORT. - Write the same logic using the same variables, constants, and structure โ but use
WRITEfor output. - Bonus: after your WRITE statements, output
sy-subrc. Notice its value (should be 0 since no DB operation occurred). - Activate with Ctrl + F2 and run with F8. Screenshot both outputs.
Test: Kon Banega Crorepati?
5 questions. โน1 Crore. Prove you understood Chapter 4. No pressure โ restart anytime.
Explore Our Instructor-Led SAP & IT Courses
Self-study works. But if you want guided training, real project practice, and placement support โ here are the courses we offer. Book a free demo before you decide.
WhatsApp us