The ADD statement is one of the basic arithmetic statements in ABAP, specifically designed for adding numeric values.
Important note upfront: Although ADD still works, the statement is considered largely obsolete in modern ABAP. For additions, the addition operator (+) is now used almost exclusively within assignments or other expressions (result = number1 + number2.). This is shorter, more flexible, and more readable. However, understanding ADD is useful for comprehending older code.
Syntax
The most relevant forms of the ADD statement are:
1. Add a value to a variable (result overwrites the variable)
ADD <operand1> TO <variable>.- The result of
<variable> + <operand1>is stored back in<variable>. <operand1>can be a variable or a literal.<variable>must be a variable. Both should be numeric.
2. Add two values and store the result in a separate variable
ADD <operand1> TO <operand2> GIVING <result_variable>.- The result of
<operand1> + <operand2>is stored in<result_variable>. <operand1>and<operand2>can be variables or literals. Their values remain unchanged (if they are variables).<result_variable>must be a variable. All should be numeric.
There is also the rarely used and equally obsolete statement ADD-CORRESPONDING <structure1> TO <structure2>, which adds identically named, numeric components of two structures. This should be avoided.
How It Works
- The operands must be numeric data types (
I,P,F) or types that can be implicitly converted to numeric types (likeN). - The calculation follows ABAP rules for numeric precision (often calculated internally as type
P). - With
ADD ... TO <variable>, the original value of<variable>is overwritten. - With
ADD ... GIVING <result_variable>, the source operands remain unchanged.
Modern Alternative (Preferred!)
The clear and recommended method for additions is the + operator:
" Equivalent to: ADD value1 TO variable.variable = variable + value1.
" Or shorter, if possible (from ABAP 7.40 SP05):variable += value1. " Adds value1 to variable
" Equivalent to: ADD value1 TO value2 GIVING result.result_variable = value1 + value2.Examples
1. ADD ... TO ...
DATA counter TYPE i VALUE 5.WRITE: / 'Counter before:', counter. " Output: 5
ADD 3 TO counter.WRITE: / 'Counter after:', counter. " Output: 82. ADD ... GIVING ...
DATA price1 TYPE p DECIMALS 2 VALUE '10.50'.DATA price2 TYPE p DECIMALS 2 VALUE '20.00'.DATA total_price TYPE p DECIMALS 2.
ADD price1 TO price2 GIVING total_price.
WRITE: / 'Price 1:', price1. " Output: 10.50 (unchanged)WRITE: / 'Price 2:', price2. " Output: 20.00 (unchanged)WRITE: / 'Total :', total_price. " Output: 30.503. Modern Alternative with +
DATA count TYPE i VALUE 100.DATA sum TYPE i.DATA addend1 TYPE i VALUE 50.DATA addend2 TYPE i VALUE 75.
count = count + 10. " count is now 110sum = addend1 + addend2. " sum is now 125
WRITE: / 'New Count:', count.WRITE: / 'New Sum :', sum.In summary:
The ADD statement performs additions but is obsolete in modern ABAP. Use the operator + in assignments instead for clearer and more contemporary code. However, knowledge of ADD helps you read and understand older ABAP code.