For sorted internal tables you can use the at/endat commands in a loop to test for the first or last occurence of a field:
AT FIRST Processsed at the very beginning of the table
AT NEW f Processsed when the content of table field f has changed or one of the preceding fields in the table has changed.
AT END OF f Processsed when the content of table field f will change at the next entry or one of preceding fields in the table will change at
the next entry.
AT LAST Processsed at the end of the table
Note: To use AT the fields in the table must occur in the same order as you sort the table. In the example below, carrid must be the first field in the table.
Se also: Control level reporting
Example:
sort itab by carrid.
loop at itab.
at new carrid.
write: / 'Carrid: ', carrid.
endat.
at last carrid.
write: / 'Total for', carrid, sum_carrid.
endat.
endloop.