Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  LOOP AT itab →  LOOP AT itab - GROUP BY →  Examples of Grouping with LOOP 

Internal Tables, Grouping with LOOP in Packages

This example demonstrates a construction of the group key that does not depend on the row content.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(n) = 10.
    cl_demo_input=>request( EXPORTING text  = `Package size`
                            CHANGING  field = n ).
    IF n <= 0.
      RETURN.
    ENDIF.

    cl_demo_output=>begin_section( |Packages of { n }| ).
    DATA group LIKE itab.
    LOOP AT itab INTO DATA(wa)
         GROUP BY ( sy-tabix - 1 ) DIV n + 1.
      CLEAR group.
      LOOP AT GROUP wa ASSIGNING FIELD-SYMBOL(<wa>).
        group = VALUE #( BASE group ( <wa> ) ).
      ENDLOOP.
      cl_demo_output=>write( group ).
    ENDLOOP.

    cl_demo_output=>display( ).

Description

Grouping of an internal table text with representative binding. The group key of the group loop is constructed as a value of the type i. This value is calculated from the row index of the current row in sy-tabix. This creates groups of rows with the same definable size.

In the group loop, the rows of each group are placed in an internal table group in a member loop using the value operator with the addition BASE. These rows are then also displayed. Here, the group is addressed using the work area wa of the original output behavior of the LOOP due to the representative binding.

group could also be filled by the evaluation of a table comprehension sing FOR ... IN GROUP, instead of in a member loop LOOP AT GROUP:

group = VALUE #( FOR <wa> IN GROUP wa ( <wa> ) ).

The executable example for grouping with FOR demonstrates how the entire group loop can be implemented using expressions.