ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  LOOP AT itab →  LOOP AT itab - Basic Form →  AT - Group Level Processing

# SUM

Other versions: 7.31 | 7.40 | 7.54

## Syntax

``````
SUM.
``````

### Effect

The statement `SUM` can only be specified within a loop starting with `LOOP`, and is only respected within a AT-`ENDAT` control structure. Prerequisites for using the statement `SUM` include using the addition `INTO` in the `LOOP` statement, and that the specified work area `wa` is compatible with the row type of the internal table. In addition, `SUM` cannot be used when the row type of the internal table `itab` contains components that are tables.

• In a group level `AT NEW compi` and `AT END OF compi`, `SUM` calculates the sums of the numeric components to the right of the current group key for all rows of the current group level and assigns these to the corresponding components of the work area `wa`. In an elementary row type, there are no components to the right of the current group key and the work area `wa` remains unchanged.
• In the group levels `FIRST`, `LAST`, and outside an `AT`-`ENDAT` control structure, for a structured row type the sum of all numeric components of all rows of the internal table is calculated and assigned to the corresponding components of the work area `wa`. For an elementary numeric row type, `SUM` sums the row values and assigns the sum to `wa`. For non-numeric elementary row types, `SUM` has no effect and the work area `wa` keeps its current value.

Note

When the `AT` control structure is exited, the content of the current table row is reassigned to the work area `wa`.

Example

Calculates a sum with `SUM` at `AT END OF`. The rows of the respective current group level are evaluated.

``````DATA:
BEGIN OF wa,
col1 TYPE i,
col2 TYPE i,
END OF wa,
itab LIKE TABLE OF wa WITH EMPTY KEY.

itab = VALUE #( FOR i = 1 UNTIL i > 5
FOR j = 1 UNTIL j > i
( col1 = i col2 = j ) ).

LOOP AT itab INTO wa.
AT END OF col1.
SUM.
cl_demo_output=>write( wa ).
ENDAT.
ENDLOOP.
cl_demo_output=>display( wa ).
``````

Example

Calculates a sum with `SUM` at `AT LAST`. All rows of the internal table are evaluated.

``````DATA:
BEGIN OF wa,
col TYPE i,
END OF wa,
itab LIKE TABLE OF wa WITH EMPTY KEY.

itab = VALUE #( FOR i = 1 UNTIL i > 10 ( col = i ) ).

LOOP AT itab INTO wa.
AT LAST.
SUM.
cl_demo_output=>display( wa ).
ENDAT.
ENDLOOP.
``````

### Handleable Exceptions

CX_SY_ARITHMETIC_OVERFLOW

• Cause: Overflow when calculating totals
Runtime error: `ADDF_INT_OVERFLOW`
• Cause: Value too large when calculating totals in internal table, field too small
Runtime error: `SUM_OVERFLOW`

### Non-Handleable Exceptions

• Cause: The statement `SUM` was used outside a `LOOP` processing of an internal table.
Runtime error: `SUM_NO_INTERNAL_TABLE`
• Cause: The statement `SUM` was used within a `LOOP` processing belonging to another ABAP program.
Runtime error: `SUM_ON_FOREIGN_INTERNAL_TABLE`
• Cause: The statement `SUM` was used within a loop starting with `LOOP ... ASSIGNING`.
Runtime error: `SUM_NO_ASSIGNING`