AS ABAP Release 758, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing Internal Data → Internal Tables (itab) → itab - Processing Statements → LOOP AT itab → LOOP AT itab, Basic Form →
AT, Group Level Processing
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.
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 one 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 line structure and the processing order in which the LOOP reads the lines 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 by the consecutive lines of the internal table specified in accordance with the processing order of the loop. These lines have the same content in a group key defined by their initial part. Here, all lines of the internal table are respected without evaluating any restricting conditions cond.
Although it is syntactically possible 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 listed 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 executed if a corresponding group break takes place 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 line with a group break is not read due to a restricting condition cond, the corresponding statement block is not executed.
The following rules must be observed for effective group level processing:
If the addition INTO wa is used in the statement LOOP, the content of wa is overwritten when the AT- ENDAT control structure is entered, depending on the respective group level, and is reset to the content of the current line when the control structure is exited. When the additions ASSIGNING and REFERENCE INTO are used, the referenced table lines are not modified when entering and exiting the AT-ENDAT control structure.
Hints
Executable Example
... FIRST
Effect
The group level is defined by the first line of the internal table. The group break takes place when this line 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 line type, this applies to the entire work area. When the AT-ENDAT control structure is exited, the content of the current table line is reassigned to the work area wa.
Hint
If the first line 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
Display of a header and the first line 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.
FINAL(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( ).
... {NEW}|{END OF} compi
Effect
Group levels are defined by the beginning or end of a group of lines 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 restriction 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 corresponding comparison rules apply to the evaluation. In the case of an elementary line 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 line type, the entire work area corresponds to the current group key and
remains unchanged. When the AT-ENDAT control structure
is exited, the content of the current table line is reassigned to the work area wa.
Hints
Example
Display of 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.
FINAL(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 line of the first line 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` ) ).
FINAL(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( ).
... LAST
Effect
The group level is defined by the last line of the internal table. The group break takes place when this line 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 line type, this applies to the entire work area. When the AT-ENDAT control structure is exited, the content of the current table line is reassigned to the work area wa.
Hint
If the last line 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
Display of cumulated content during the processing of the last line.
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.
Uncatchable Exceptions