Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  LOOP AT itab →  LOOP AT itab - Basic Form →  AT - Control Level Processing →  Examples of Control Level Processing 

Control Level Processing

This example demonstrates control level processing in internal tables.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA itab TYPE HASHED TABLE OF line
              WITH UNIQUE KEY col1 col2.

    DATA(out) = cl_demo_output=>new( ).

    itab = VALUE #(
      FOR j = 1 UNTIL j > 3
       ( col1 = 'A'
         col2 = j
         col3 = j ** 2 )
       ( col1 = 'B'
         col2 = 2 * j
         col3 = ( 2 * j ) ** 2 ) ).

    SORT itab.
    out->write( itab )->line( ).

    DATA group LIKE itab.
    LOOP AT itab INTO DATA(line).
      AT NEW col1.
        CLEAR group.
      ENDAT.
      group = VALUE #( BASE group ( line ) ).
      AT END OF col1.
        out->write( group ).
        SUM.
        out->line( )->write( line )->line( ).
      ENDAT.
      AT LAST.
        SUM.
        out->line( )->write( line ).
      ENDAT.
    ENDLOOP.
    out->display( ).

Description

A hashed table itab is created, filled with six rows, and sorted by the primary table key. In the LOOP - ENDLOOP loop, the work area line is used and is displayed for every loop pass. The first field of the primary table key col1 is used for control level processing. The sum of all numeric fields is calculated each time the content of col1 changes and once for the last table row.

The example Grouping in Control Levels shows how the same function can be provided using the addition GROUP BY.