Beginner

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:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
REPORT zvariables.

" --- Elementary Type Declarations ---
" Integer (i) - whole numbers
DATA lv_count TYPE i.
DATA lv_year TYPE i.

" Packed decimal (p) - fixed-point numbers for currency/precision
DATA lv_price TYPE p LENGTH 8 DECIMALS 2.
DATA lv_rate TYPE p LENGTH 5 DECIMALS 4.

" Character (c) - fixed-length text
DATA lv_code TYPE c LENGTH 5.

" String - variable-length text
DATA lv_name TYPE string.
DATA lv_message TYPE string.

" Numeric text (n) - digits stored as characters
DATA lv_zipcode TYPE n LENGTH 5.

" Date (d) - 8-character date in YYYYMMDD format
DATA lv_date TYPE d.

" Time (t) - 6-character time in HHMMSS format
DATA lv_time TYPE t.

" Float (f) - IEEE 754 floating-point
DATA lv_pi TYPE f.

" Hexadecimal (x) - raw byte data
DATA lv_hex TYPE x LENGTH 2.

" --- Assigning Values ---
WRITE: / '=== Basic Variables ==='.

lv_count = 42.
lv_year = 2026.
WRITE: / 'count:', lv_count.
WRITE: / 'year:', lv_year.

" Packed decimal for money - no floating-point rounding issues
lv_price = '1234.56'.
lv_rate = '0.0750'.
WRITE: / 'price:', lv_price.
WRITE: / 'rate:', lv_rate.

" Character types
lv_code = 'AB12'.
lv_name = 'ABAP Developer'.
WRITE: / 'code:', lv_code.
WRITE: / 'name:', lv_name.

" Numeric text - preserves leading zeros
lv_zipcode = '00501'.
WRITE: / 'zipcode:', lv_zipcode.

" Date and time
lv_date = '20260226'.
lv_time = '143000'.
WRITE: / 'date:', lv_date.
WRITE: / 'time:', lv_time.

" Float
lv_pi = '3.14159265'.
WRITE: / 'pi:', lv_pi.

" --- Chained Declarations ---
WRITE: / ''.
WRITE: / '=== Chained Declarations ==='.

" The colon-comma syntax declares multiple variables
DATA: lv_a TYPE i,
      lv_b TYPE i,
      lv_c TYPE i.

lv_a = 10.
lv_b = 20.
lv_c = lv_a + lv_b.
WRITE: / 'a:', lv_a, 'b:', lv_b, 'a + b:', lv_c.

" --- Constants ---
WRITE: / ''.
WRITE: / '=== Constants ==='.

" CONSTANTS cannot be changed after declaration
CONSTANTS: lc_max TYPE i VALUE 100,
           lc_pi TYPE p LENGTH 8 DECIMALS 5 VALUE '3.14159',
           lc_label TYPE string VALUE 'ABAP Constants'.

WRITE: / 'max:', lc_max.
WRITE: / 'pi:', lc_pi.
WRITE: / 'label:', lc_label.

" --- Type Conversions ---
WRITE: / ''.
WRITE: / '=== Type Conversions ==='.

DATA lv_int TYPE i.
DATA lv_str TYPE string.
DATA lv_packed TYPE p LENGTH 8 DECIMALS 2.
DATA lv_float TYPE f.

" String to integer
lv_str = '256'.
lv_int = lv_str.
WRITE: / 'String to int:', lv_str, '->', lv_int.

" Integer to string
lv_int = 999.
lv_str = lv_int.
WRITE: / 'Int to string:', 999, '->', lv_str.

" Integer to packed decimal
lv_int = 42.
lv_packed = lv_int.
WRITE: / 'Int to packed:', lv_int, '->', lv_packed.

" Packed to float
lv_packed = '123.45'.
lv_float = lv_packed.
WRITE: / 'Packed to float:', lv_packed, '->', lv_float.

" --- Date Arithmetic ---
WRITE: / ''.
WRITE: / '=== Date Arithmetic ==='.

DATA lv_today TYPE d.
DATA lv_future TYPE d.
DATA lv_diff TYPE i.

lv_today = '20260226'.
lv_future = '20260401'.
lv_diff = lv_future - lv_today.
WRITE: / 'Today:', lv_today.
WRITE: / 'Future:', lv_future.
WRITE: / 'Days between:', lv_diff.

" --- Structures ---
WRITE: / ''.
WRITE: / '=== Structures ==='.

" A structure groups related fields together
TYPES: BEGIN OF ty_employee,
         id TYPE i,
         name TYPE string,
         department TYPE string,
         salary TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_employee.

DATA ls_emp TYPE ty_employee.
ls_emp-id = 1001.
ls_emp-name = 'Ada Lovelace'.
ls_emp-department = 'Engineering'.
ls_emp-salary = '95000.00'.

WRITE: / 'Employee ID:', ls_emp-id.
WRITE: / 'Name:', ls_emp-name.
WRITE: / 'Dept:', ls_emp-department.
WRITE: / 'Salary:', ls_emp-salary.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Pull the Node.js image
docker pull node:20

# Run the variables example
docker run --rm -v $(pwd):/work node:20 sh -c '
cd /work && mkdir -p abap_project/src && cd abap_project && \
npm init -y >/dev/null 2>&1 && \
npm install --silent @abaplint/transpiler-cli @abaplint/runtime 2>/dev/null && \
echo "{\"input_folder\":\"src\",\"output_folder\":\"output\",\"write_unit_tests\":false,\"options\":{\"ignoreSyntaxCheck\":true}}" > abap_transpile.json && \
echo "{\"global\":{\"files\":\"/src/**/*.*\"},\"syntax\":{\"version\":\"v702\",\"errorNamespace\":\".\"}}" > abaplint.json && \
cp /work/variables.abap src/zvariables.prog.abap && \
echo "<?xml version=\"1.0\"?><abapGit version=\"v1.0.0\"><asx:abap xmlns:asx=\"http://www.sap.com/abapxml\"><asx:values><PROGDIR><NAME>ZVARIABLES</NAME><SUBC>1</SUBC></PROGDIR></asx:values></asx:abap></abapGit>" > src/zvariables.prog.xml && \
./node_modules/.bin/abap_transpile >/dev/null && \
echo "import runtime from \"@abaplint/runtime\";globalThis.abap = new runtime.ABAP();await import(\"./output/zvariables.prog.mjs\");" > run.mjs && \
node run.mjs && cd /work && rm -rf abap_project'

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 DATA before 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 CONSTANTS keyword
  • 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

Running Today

All examples can be run using Docker:

docker pull node:20
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining