ABAP REDUCE operator in new ABAP syntax used when you need to calculate one final result from many values. For example, you can calculate a total quantity, count table lines, calculate a total amount, find a maximum value, or build a final string from an internal table ABAP.
In classic ABAP, this type of logic is usually written with LOOP AT and a helper variable. That is still a valid and readable approach. ABAP REDUCE becomes useful when the aggregation is small enough to describe clearly in one expression.
What does REDUCE do in ABAP?
REDUCE starts with an initial value and changes this value on every loop step. The final value is returned as the result of the expression. This final value can be a number, a string, a structure, or another suitable type, but the most common examples are totals and counters.
|
1 2 3 4 5 |
DATA(result) = REDUCE result_type( INIT accumulator = start_value FOR line IN internal_table NEXT accumulator = accumulator + line-field ). |
- INIT defines the start value.
- FOR reads the internal table.
- NEXT describes how the accumulator changes for every row.
Test data for REDUCE examples
The examples below use a small internal table with order items. The data itself is simple, so the focus stays on the REDUCE operator and not on business-specific details.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TYPES: BEGIN OF ty_item, product TYPE string, category TYPE string, quantity TYPE i, price TYPE decfloat34, END OF ty_item. TYPES tt_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY. DATA(lt_items) = VALUE tt_items( ( product = `Laptop` category = `A` quantity = 2 price = CONV decfloat34( '900' ) ) ( product = `Mouse` category = `B` quantity = 5 price = CONV decfloat34( '20' ) ) ( product = `Monitor` category = `A` quantity = 3 price = CONV decfloat34( '250' ) ) ). |
Old ABAP syntax: calculate a total with LOOP AT
Before using REDUCE, let us look at the classic ABAP version. To calculate the total quantity, we declare a variable, loop through the internal table, and add every quantity to the variable.
|
1 2 3 4 5 6 7 8 |
DATA lv_total_quantity TYPE i. DATA ls_item TYPE ty_item. CLEAR lv_total_quantity. LOOP AT lt_items INTO ls_item. lv_total_quantity = lv_total_quantity + ls_item-quantity. ENDLOOP. |
This code is clear, but it takes several statements for a very simple aggregation. REDUCE can express the same idea more directly.
New ABAP syntax: calculate sum with REDUCE
|
1 2 3 4 5 |
DATA(lv_total_quantity) = REDUCE i( INIT result = 0 FOR item IN lt_items NEXT result = result + item-quantity ). |
This REDUCE expression returns one integer value. The accumulator is called result. It starts with 0 and increases by item-quantity for every row in lt_items.
REDUCE with WHERE condition
You can also use a WHERE condition inside the FOR expression. This is useful when you need to aggregate only selected rows from an internal table.
|
1 2 3 4 5 |
DATA(lv_category_a_quantity) = REDUCE i( INIT result = 0 FOR item IN lt_items WHERE ( category = `A` ) NEXT result = result + item-quantity ). |
In this example, ABAP adds quantities only for items in category A. You do not need to create a separate filtered internal table first.
Calculate total amount with REDUCE
A common business case is calculating total amount from price and quantity. REDUCE works well when the formula is short.
|
1 2 3 4 5 |
DATA(lv_total_amount) = REDUCE decfloat34( INIT result = CONV decfloat34( 0 ) FOR item IN lt_items NEXT result = result + item-price * item-quantity ). |
For every item, the expression calculates item-price * item-quantity and adds it to the final amount.
Find maximum value with REDUCE and COND
REDUCE can also be combined with COND. In the next example, we find the highest price in the internal table.
|
1 2 3 4 5 6 7 8 |
DATA(lv_max_price) = REDUCE decfloat34( INIT result = CONV decfloat34( 0 ) FOR item IN lt_items NEXT result = COND decfloat34( WHEN item-price > result THEN item-price ELSE result ) ). |
The COND expression compares the current item price with the current result. If the item price is higher, it becomes the new maximum value.
When should you use the ABAP REDUCE operator?
Use REDUCE when the goal is one final value and the expression remains easy to read.
- Calculate a total quantity or total amount.
- Count rows that match a condition.
- Find a maximum or minimum value.
- Build one final string from several values.
- Replace a small LOOP AT aggregation with a clear expression.
When should you avoid REDUCE?
Do not use REDUCE only because it is new syntax. If the expression becomes too long, nested, or difficult to debug, a normal LOOP AT is usually better. Clean ABAP is not about using the shortest possible syntax. It is about writing code that another developer can understand quickly.
FAQ: ABAP REDUCE operator
Is REDUCE a replacement for LOOP AT? REDUCE can replace simple LOOP AT blocks that calculate one final value. It is not a replacement for complex loop logic.
Can REDUCE work with internal tables? Yes. REDUCE is commonly used together with FOR expressions to read internal tables and calculate a final value.
Can REDUCE be used with WHERE? Yes. You can add WHERE inside the FOR expression to include only selected rows in the calculation.
The ABAP REDUCE operator is a useful part of new ABAP syntax for simple aggregations. It is a good choice for sums, counters, total amounts, and maximum values. Use it when it makes the code shorter and clearer, but keep classic LOOP AT when the business logic needs more explicit steps.