Variables and Types in ABAP
Learn about variables, data types, and type conversions in ABAP with practical Docker-ready examples using the open-abap transpiler
ABAP’s type system was built from the ground up for business applications. Where most languages offer generic integers and floats, ABAP provides packed decimals for currency, fixed-length character fields for form data, and dedicated date and time types — all reflecting its heritage as SAP’s enterprise workhorse.
As a statically and strongly typed language, ABAP requires you to declare variables before use with the DATA statement. Every variable has a fixed type that the compiler enforces. This strictness is intentional — in financial systems processing millions of transactions, implicit type conversions and loose typing can lead to costly bugs.
In this tutorial you’ll learn how ABAP declares variables with DATA, explore the built-in elementary types, work with type conversions, and see how ABAP’s business-oriented types handle real-world data like currency amounts and dates.
Variable Declarations and Elementary Types
ABAP variables are declared with the DATA statement, specifying a name and type. The naming convention uses prefixes to indicate scope and kind: lv_ for local variables, gv_ for global variables, lt_ for local tables, and so on. This Hungarian notation is deeply embedded in ABAP culture.
Create a file named variables.abap:
| |
Running with Docker
The open-abap transpiler converts ABAP to JavaScript and runs it on Node.js. The Docker command sets up the transpiler, configuration files, and executes the program.
| |
Expected Output
=== Basic Variables ===
count: 42
year: 2,026
price: 1,234.56
rate: 0.0750
code: AB12
name: ABAP Developer
zipcode: 00501
date: 20260226
time: 143000
pi: 3.1415926500000001E+00
=== Chained Declarations ===
a: 10 b: 20 a + b: 30
=== Constants ===
max: 100
pi: 3.14159
label: ABAP Constants
=== Type Conversions ===
String to int: 256 -> 256
Int to string: 999 -> 999
Int to packed: 42 -> 42.00
Packed to float: 123.45 -> 1.2345000000000000E+02
=== Date Arithmetic ===
Today: 20260226
Future: 20260401
Days between: 34
=== Structures ===
Employee ID: 1,001
Name: Ada Lovelace
Dept: Engineering
Salary: 95,000.00
Key Concepts
- DATA statement — All ABAP variables must be declared with
DATAbefore use, specifying a name and type - Packed decimal (type p) — ABAP’s answer to floating-point rounding errors in financial calculations; stores exact decimal values with a specified number of decimal places
- Naming conventions — ABAP uses Hungarian notation prefixes:
lv_for local variables,gv_for global,ls_for local structures,lt_for local tables,lc_for local constants - Type n (numeric text) — Stores digit strings that preserve leading zeros, ideal for zip codes, account numbers, and document IDs that look like numbers but aren’t used in arithmetic
- Date type (d) — Stores dates as 8-character strings in YYYYMMDD format; supports arithmetic so you can add days or subtract dates to get differences
- CONSTANTS — Unlike many languages with convention-only constants, ABAP enforces immutability at compile time with the
CONSTANTSkeyword - Automatic type conversion — ABAP performs implicit conversions between compatible types (string to integer, packed to float) following well-defined conversion rules
- Structures (TYPES/BEGIN OF) — Group related fields into a composite type, accessed with the dash operator (
ls_emp-name), forming the foundation for ABAP’s internal tables
Comments
Loading comments...
Leave a Comment