While preparing for modern ABAP topics, I revisited one of the classic concepts we frequently use in reporting programs—control break processing. Most ABAP developers are familiar with statements such as AT NEW, AT END OF, and SUM for calculating subtotals and grand totals.
Recently, I explored how the same requirement can be implemented using LOOP AT GROUP BY and the REDUCE operator. My goal was to understand whether modern ABAP syntax could make the code more readable and easier to maintain compared to traditional control break statements.
After implementing both approaches on sample MARA data, I found that the modern syntax significantly reduces the amount of procedural code required for grouping and aggregation. Although the traditional approach is still widely used and perfectly valid, the modern approach felt cleaner and easier to follow, especially when working with complex grouping scenarios.
In this blog post, I will share the example I created, explain how grouping and subtotal calculations can be achieved using modern ABAP syntax, and highlight some observations from my implementation experience.
First, create a local structure to hold the required fields from the MARA table.
Fields used:
An internal table and a variable for storing the grand total are also declared.
Read the required records from the MARA table into the internal table.
The example fetches:
Material Number
Material Group
Material Type
Net Weight
For demonstration purposes, only the first 20 records are selected.
Before grouping, sort the internal table by MATNR.
Sorting ensures that records belonging to the same material are processed together and improves the readability of the output.
Modern ABAP introduces the GROUP BY addition in LOOP statements.
The following grouping logic is used:
Each unique MATNR becomes a group.
All records having the same MATNR belong to that group.
The group object provides access to both the group key and its members.
This eliminates the need for traditional control break statements such as AT NEW and AT END OF.
The REDUCE operator is used to calculate the subtotal of NTGEW within each group.
Benefits of REDUCE:
Compact syntax
Improved readability
Functional programming approach
Eliminates explicit accumulator loops
The subtotal is calculated by iterating through all members of the current group and summing their NTGEW values.
For every material group:
Material Number is displayed.
Material Group is displayed.
Material Type is displayed.
Net Weight is displayed.
The LOOP AT GROUP statement is used to access each member belonging to the current group.
After processing all members of a group:
The subtotal of NTGEW is displayed.
A separator line is printed using ULINE.
This provides a clear summary for each material group.
The subtotal of each group is added to the grand total variable.
After all groups are processed, the final accumulated value represents the overall grand total of NTGEW.
REPORT zrp_control_brk_stt.
TYPES: BEGIN OF ty_mara,
matnr TYPE matnr,
matkl TYPE matkl,
mtart TYPE mtart,
ntgew TYPE ntgew,
END OF ty_mara.
DATA: lt_mara TYPE TABLE OF ty_mara,
lv_grand_total TYPE ntgew.
SELECT matnr,
matkl,
mtart,
ntgew
FROM mara
INTO TABLE @LT_mara
UP TO 10 ROWS.
IF sy-subrc = 0.
SORT lt_mara BY matnr.
ENDIF.
LOOP AT lt_mara ASSIGNING FIELD-SYMBOL(<fs_mara>)
GROUP BY ( matnr = <fs_mara>-matnr )
ASSIGNING FIELD-SYMBOL(<group>).
Data(lv_sub_total) = REDUCE ntgew(
INIT lv_sum TYPE ntgew
FOR wa IN GROUP <group>
NEXT lv_sum += wa-ntgew ).
WRITE: / 'Material:' COLOR 1 , <group>-matnr.
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<member>).
WRITE: / 'Material Group :-' COLOR 2 , <member>-matkl,
30 'Material Type :-' COLOR 2, <member>-mtart,
55 'Net Weight :-' COLOR 2 , <member>-ntgew.
ENDLOOP.
WRITE: / 'Subtotal:' COLOR 5, 70 lv_sub_total COLOR 3.
lv_grand_total += lv_sub_total.
ULINE.
ENDLOOP.
WRITE: / 'Grand Total:' COLOR 5, 70 lv_grand_total COLOR 5.The report displays:
Material-wise grouped records
Subtotal for each Material Number
Overall Grand Total
This produces a clean and structured output similar to classical control break processing while using modern ABAP syntax.
After implementing this example, I found that LOOP AT GROUP BY and REDUCE provide a much cleaner alternative to classical control break processing. The biggest advantage for me was eliminating multiple AT NEW and AT END OF blocks while keeping the aggregation logic compact and easy to understand.
From a learning perspective, this exercise helped me better understand ABAP's expression-oriented programming model. While traditional control break statements remain useful when maintaining older applications, I would prefer the GROUP BY and REDUCE approach for new developments because it improves readability and reduces boilerplate code.
I would be interested to know how other ABAP developers are handling subtotal and aggregation requirements in modern ABAP projects and whether they have fully transitioned away from traditional control break statements.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 16 | |
| 7 | |
| 6 | |
| 6 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |