Entering content frameProcessing Table Entries in Loops Locate the document in its SAP Library structure

You can use the LOOP statement to process special loops for any internal table.

LOOP AT <itab> <result> <condition>.
  <statement block>
ENDLOOP.

This reads the lines of the table one by one as specified in the <result> part of the LOOP statement. You can then process them in the statements within the LOOP... ENDLOOP control structure. You can either run the loop for all entries in the internal table, or restrict the number of lines read by specifying a <condition>. Control level processing is allowed within the loop.

The sequence in which the lines are processed depends on the table type:

The lines are processed according to the linear index. Within the processing block, the system field SY-TABIX contains the index of the current line.

As long as the table has not been sorted, the lines are processed in the order in which you added them to the table. Within the processing block, the system field SY-TABIX is always 0.

You can nest LOOP blocks. When you leave the loop, SY-TABIX has the same value as when you entered it. After the ENDLOOP statement, SY-SUBRC is zero if at least one table entry was processed. Otherwise, it is 4.

The loop may not contain any operations on entire internal tables that change the table. However, you should remember that even saving a global internal table with the LOCAL statement in a procedure is a change operation on the entire table, since it exchanges the table contents. When you call procedures within loops, you should therefore check that it does not change the entire internal table. If you change the table, the loop can no longer work properly.

If you insert or delete a table entry within a loop pass, it is taken into account in subsequent loop passes as follows:

Specifying the Extra Processing Option

The processing option specifies how a table line is available in the statement block of the list.

Using a Work Area

To place the current loop line into a work area, specify <result> as follows:

LOOP AT <itab> INTO <wa> <condition>.

The contents of the table lines must be convertible into the data type of the work area <wa>. In each loop pass, one line of the table is copied into the work area. The end of the loop does not affect the work area, that is, the contents of <wa> are the same after the ENDLOOP statement as they were in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition <condition>, the work area is not changed.

Using a Field Symbol

To assign the contents of the current loop line to a field symbol, specify <result> as follows:

LOOP AT <itab> ASSIGNING <FS> <conditions>.

In each loop pass, the field symbol <FS> points to the table entry read in that pass. If the line type is structured, you should specify the same type for the field symbol when you declare it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.

The end of the loop does not affect the field symbol, that is, after ENDLOOP it is still assigned to the same line as in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition <condition>, the field symbol is not changed.

For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.

Suppressing the Assignment of Lines

If you do not need to transfer the contents of the current table line to a work area or assign them to a field symbol, you can use the following statement:

LOOP AT <itab> TRANSPORTING NO FIELDS <condition>.

This form of the LOOP statement is useful if you want to find the index of a particular internal table, or the number of lines in a table that meet a particular condition.

Specifying Conditions

To avoid reading all of the lines in an internal table, you can specify a condition for the line selection as follows:

LOOP AT <itab> <result> WHERE <cond>.

This processes all of the lines that meet the logical condition <cond>. The logical condition can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression TABLE LINE. The comparison then applies to the entire line.

Control level processing

Control level processing is allowed within a LOOP over an internal table. This means that you can divide sequences of entries into groups based on the contents of certain fields.

Internal tables are divided into groups according to the sequence of the fields in the line structure. The first column defines the highest control level and so on. The control level hierarchy must be known when you create the internal table.

The control levels are formed by sorting the internal table in the sequence of its structure, that is, by the first field first, then by the second field, and so on. Tables in which the table key occurs at the start of the table are particularly suitable for control level processing.

The following diagram illustrates control level processing in a sorted table, where different field contents in the first three fields are indicated by different colors:

This graphic is explained in the accompanying text

Each change of color in a column indicates a control level change in the corresponding hierarchy level. Within the processing block of a loop, you can use the control level statement AT to react to a control level change. This enables you to restrict statements to a certain set of lines. You can thus use the SUM statement to calculate totals from subsets of all lines.

The AT statement introduces a statement block that you end with the ENDAT statement.

AT <level>.
  <statement block>
ENDAT.

You can react to the following control level changes:

<level>

Meaning

FIRST

First line of the internal table

LAST

Last line of the internal table

NEW <f>

Beginning of a group of lines with the same contents in the field <f> and in the fields left of <f>

END Of <f>

End of a group of lines with the same contents in the field <f> and in the fields left of <f>

You can use control level statements to react to control breaks in internal tables instead of programming them yourself with logical expressions. Within the loop, you must order the AT-ENDAT statement blocks according to the hierarchy of the control levels. If the internal table has the columns <f1>, <f 2>, ...., and if it is sorted by these columns, you must program the loop as follows:

LOOP AT <itab>.
  AT FIRST. ... ENDAT.
    
AT NEW <f1>. ...... ENDAT.
      AT NEW <f
2 >. ...... ENDAT.
        .......
          <single line processing>
.......
      
AT END OF <f2>. ... ENDAT.
    
AT END OF <f1>. ... ENDAT.
  AT LAST. .... ENDAT.
ENDLOOP.

The innermost hierarchy level <single line processing> processes the table lines that do not correspond to a control level change. You do not have to use all control level statements. But you must place the used ones in the above sequence. You should not use control level statements in loops where the line selection is restricted by WHERE or FROM and TO. Neither should the table be modified during the loop.

If a control level field <fi> is not known until runtime, you can specify it dynamically as (<n i>) where <n i> contains the field of <f i>. If <n i> is empty at runtime, the criterion for changing the control level is ignored. You can restrict the search to partial fields by specifying offset and length.

If you are working with a work area <wa>, it does not contain the current line in the AT... ENDAT statement block. All character fields to the right of the current group key are filled with asterisks (*). All other fields to the right of the current group key contain their initial value.

Within an AT...ENDAT block, you can calculate the contents of the numeric fields of the corresponding control level using the SUM statement.

SUM.

You can only use this statement within a LOOP. If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area (see example in ). If you use the SUM statement outside an AT - ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT...ENDAT blocks.

If the table contains a nested table, you cannot use the SUM statement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.

Examples

Example

DATA: BEGIN OF LINE,
         COL1 TYPE C,
         COL2 TYPE I,
         COL3 TYPE I,
      END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE
          WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.
DO 3 TIMES.
  LINE-COL2 = SY-INDEX.
  LINE-COL3 = SY-INDEX ** 2.
  INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL1 = 'B'.
DO 3 TIMES.
  LINE-COL2 = 2 * SY-INDEX.
  LINE-COL3 = ( 2 * SY-INDEX ) ** 2.
  INSERT LINE INTO TABLE ITAB.
ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
  AT END OF COL1.
    SUM.
    ULINE.
    WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
    SKIP.
  ENDAT.
  AT LAST.
    SUM.
    ULINE.
    WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
  ENDAT.
ENDLOOP.

The output is:

A 1 1

A 2 4

A          3          9
________________________________

A          6         14

B          2          4

B 4 16

B 6 36
________________________________

B         12         56

________________________________

*         18         70

The program creates a hashed table ITAB, fills it with six lines, and sorts it. In the LOOP - ENDLOOP block, the work area LINE is output for each loop pass. The first field of the table key, COL1, is used for control level processing. The total for all numeric fields is always calculated when the contents of COL1 change and when the system is in the last loop pass.

Example

DATA: BEGIN OF LINE,
        CARRID   TYPE SBOOK-CARRID,
        CONNID   TYPE SBOOK-CONNID,
        FLDATE   TYPE SBOOK-FLDATE,
        CUSTTYPE TYPE SBOOK-CUSTTYPE,
        CLASS    TYPE SBOOK-CLASS,
        BOOKID   TYPE SBOOK-BOOKID,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY TABLE LINE.

SELECT CARRID CONNID FLDATE CUSTTYPE CLASS BOOKID
       FROM SBOOK INTO CORRESPONDING FIELDS OF TABLE ITAB.

LOOP AT ITAB INTO LINE.

  AT FIRST.
    WRITE / 'List of Bookings'.
    ULINE.
  ENDAT.

    AT NEW CARRID.
      WRITE: / 'Carrid:', LINE-CARRID.
    ENDAT.

      AT NEW CONNID.
        WRITE: / 'Connid:', LINE-CONNID.
      ENDAT.

        AT NEW FLDATE.
          WRITE: / 'Fldate:', LINE-FLDATE.
        ENDAT.

          AT NEW CUSTTYPE.
            WRITE: / 'Custtype:', LINE-CUSTTYPE.
          ENDAT.

               WRITE: / LINE-BOOKID, LINE-CLASS.

            AT END OF CLASS.
              ULINE.
            ENDAT.

ENDLOOP.

In this example, the sorted internal table ITAB is filled with data from the database table SBOOK using the Open SQL statement SELECT. The sequence of the columns in the internal table defines the control level hierarchy. Since the table key is the entire line, the sort sequence and the control level hierarchy are the same. The sequence of the AT-ENDAT blocks within the LOOP and ENDLOOP statements is important.

The output is as follows:

List of Bookings
Carrid: AA
Connid: 0017
Fldate: 1998/11/22
Custtype: B
00063509 C
00063517 C
...
______________________________________________
00063532 F
00063535 F
...
______________________________________________
Custtype: P
00063653 C
00063654 C
...
______________________________________________
00063668 F
00063670 F
...
______________________________________________
Fldate: 1998/29/11
Custtype: B
00064120 C
00064121 C
...

and so on.

 

 

 

 

 

Leaving content frame