ABAP REDUCE Operator: Simple Examples in New ABAP Syntax

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.

  • 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.

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.

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

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.

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.

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.

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.

Leave a Reply