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

Quick Reference

Syntax

LOOP AT itab result [cond].
  [AT FIRST.
     ...
   ENDAT.]
    [AT NEW comp1.
       ...
     ENDAT.
       [AT NEW comp2.
         ...
       ENDAT.
         [...]]]
           [ ... ]
       [[[...]
       AT END OF comp2.
         ...
       ENDAT.]
     AT END OF comp1.
       ...
     ENDAT.]
  [AT LAST.
     ...
  ENDAT.]
ENDLOOP.

Extras:

1. ...  FIRST

2. ... {NEW}|{END OF} compi

3. ...  LAST

Effect

The statement block of a LOOP without the addition GROUP BY can contain control structures for group level processing. The corresponding control statement is AT. The statements AT and ENDAT define statement blocks that are executed at group breaks, that is in the transition of a group level to another group level. The additions of the statements AT define the criteria for a group break and hence the group levels. Group breaks between group levels are a result of the row structure and the processing order in which the LOOP reads the rows of the internal table. Within the statement blocks, the statement SUM can be specified to total the numeric components of a group level.

Apart from the special variants AT FIRST and AT LAST, group levels are defined using the variants AT NEW AT END OF and the sequence of rows (in accordance with the processing order of the loop) in the internal table. These are the rows that have the same content in a group key defined in their initial parts. Here, all rows of the internal table are respected without evaluating any restricting conditions cond.

It is possible, from a syntax point of view, to arrange AT control structures in any order and to nest them, however effective group level processing is usually only achieved by the arrangement above. Here, the control structures are specified in accordance with the arrangement of the components in the group key and are not nested.

The statement blocks within the AT-ENDAT control structures are listed if an appropriate group break is made in the current table line. Statements in the LOOP-ENDLOOP control structure that are not executed within an AT-ENDAT control structure are executed in each pass of the loop. If a table row with a group break is not read due to a restricting condition cond, the statement block in question is not executed.

The following rules must be followed for effective group level processing:

If the addition INTO wa is used in the statement LOOP, the content of wa is overwritten (depending on the respective group level) when the AT-ENDAT control structure is entered, and is reset to the content of the current row when the control structure is exited. When the additions ASSIGNING and REFERENCE INTO are used, the referenced table rows are not modified when entering and exiting the AT-ENDAT control structure.

Notes

Executable Examples

Group Level Processing

Addition 1

... FIRST

Effect

The group level is defined by the first row of the internal table. The group break takes place when this row is read. In the group level AT FIRST, the current group key does not contain any components.

If the addition INTO wa is used in the statement LOOP, all character-like components of the work area wa are filled with "*" when the control structure AT FIRST is entered and all other components are set to their initial value. In the case of an elementary row type, this applies to the whole work area. When the AT-ENDAT control structure is exited, the content of the current table row is reassigned to the work area wa.

Note

If the first row of the internal table is not read due to a restricting condition cond, the statement block of the control structure AT FIRST is not executed.

Example

Displays a header and the first row of an internal table at the group level AT FIRST.

DATA itab TYPE SORTED TABLE OF scarr
               WITH UNIQUE KEY carrid.

SELECT *
       FROM scarr
       INTO TABLE itab.

DATA(out) = cl_demo_output=>new( ).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  AT FIRST.
    out->next_section( 'First' ).
    out->write( <fs> ).
  ENDAT.
ENDLOOP.
out->display( ).

Addition 2

... {NEW}|{END OF} compi

Effect

Group levels are defined by the beginning or end of a group of rows with the same content in the component compi (where i = 1, 2, and so on) and in the components on the left of compi. The content of these components determines the group key. The group breaks take place when the content of the component compi or another component on the left of compi changes.

The compi components can be specified as described in Specification of Components, with the limitation that access to data objects using references is not possible here. The following is therefore not possible:

Otherwise, the specified components can have any data type. The relevant comparison rules apply to the evaluation. In the case of an elementary row type, only table_line can be specified as the group key.

If the addition INTO wa is used in the statement LOOP, the content of wa is modified as follows when the AT-ENDAT control structure is entered:

In the case of an elementary row type, the whole work area corresponds to the current group key and does not change. When the AT-ENDAT control structure is exited, the content of the current table row is reassigned to the work area wa.

Notes

Example

Displays the carrid column as a header for the chained content of the associated flight numbers.

DATA itab TYPE SORTED TABLE OF spfli
               WITH UNIQUE KEY carrid connid.

SELECT *
       FROM spfli
       INTO TABLE itab.

DATA connids TYPE string.
DATA(out) = cl_demo_output=>new( ).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  AT NEW carrid.
    out->next_section( <fs>-carrid ).
  ENDAT.
  connids = connids && ` ` && <fs>-connid.
  AT END OF carrid.
    out->write( connids ).
    CLEAR connids.
  ENDAT.
ENDLOOP.
out->display( ).

Example

The first row of the first row group is not read due to the addition FROM 2, which means the associated statement block after AT NEW table_line is not executed. The program therefore produces a message from the extended program check.

DATA itab TYPE TABLE OF string WITH EMPTY KEY.

itab = VALUE #( ( `AA` ) ( `AA` ) ( `AA` )
                ( `BB` ) ( `BB` ) ( `BB` ) ).

DATA(out) = cl_demo_output=>new( ).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>) FROM 2.
  AT NEW table_line.
    out->write( <fs> ).
  ENDAT.
ENDLOOP.
out->display( ).

Addition 3

... LAST

Effect

The group level is defined by the last row of the internal table. The group break takes place when this row is read. In the group level AT LAST, the current group key does not contain any components.

If the addition INTO wa is used in the statement LOOP, all character-like components of the work area wa are filled with "*" when the control structure AT LAST is entered and all other components are set to their initial value. In the case of an elementary row type, this applies to the whole work area. When the AT-ENDAT control structure is exited, the content of the current table row is reassigned to the work area wa.

Note

If the last row of the internal table is not read due to a restricting condition cond, the statement block of the control structure AT LAST is not executed.

Example

Displays cumulated content during the processing of the last row.

DATA itab TYPE SORTED TABLE OF scarr
               WITH UNIQUE KEY carrid.

SELECT *
       FROM scarr
       INTO TABLE itab.

DATA carrids TYPE string.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  carrids = carrids && ` ` && <fs>-carrid.
  AT LAST.
    cl_demo_output=>display( carrids ).
  ENDAT.
ENDLOOP.

Exceptions

Non-Handleable Exceptions



Continue
SUM
ENDAT
Examples of Control Level Processing