ABAP LOOP Statement with At New and At End
ABAP LOOP statement enables developers to iterate in an ABAP internal table. ABAP Loop command enhancements At New and At End provide additional functions for SAP developers like identifying the start and end of a group or partition according to a field in a sorted internal table. While using ABAP LOOP statement in your ABAP programs, you can think of using AT NEW field and AT END field statements to find fast solutions for your requirements.
For example, ABAP developers can create a program listing sales orders of all sales representative in a month. The order data can be stored in an internal table sorted by sales represantative. While looping over sales order data, if Loop statement is used with At New and At End extensions, it is possible to count orders of each representative during the Loop statement.
In this ABAP tutorial, I will share source codes of a similar requirement case but not this time count item details, but print item posnr values on screen.
I also copied down the source codes of the ABAP report where Loop At flow control statement is used with extensions At New and At End.
As a summary, above sample ABAP program SELECT a list of sales items from VBAP table and lists all of them using ABAP Loop command.
Within the Loop command, AT NEW statement detects when the VBAP structure has a different VBELN (document number) then the previous one.
When a new VBELN Sales Order number is detected in the loop, the order number is printed. And a bracket is opened for the item positions of that sales order.
LOOP AT END statement also detects the different vbeln number and enables ABAP developers to do work with the previous structure in the loop.
In this sample ABAP program, I used the Loop At End for closing the bracket containing the list of sales order item positions.
Here is the output of the sample ABAP program featuring ABAP Loop At New and At End statements.
Although we loop through the internal table for sales order items sequentially using the Loop command, At New field and At End field enabled developers to print sales order header data with item details