ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

Classic ABAP internal tables: SORT, DELETE ADJACENT DUPLICATES, COLLECT and AT END OF

The problem

A list of sales lines (customer, product, amount) must become two results: the list of distinct customers, and the total amount per customer.

How we solve it

  1. 1Data and SORTsales lines
  2. 2Remove duplicatesADJACENT DUPLICATES
  3. 3TotalsCOLLECT, AT END OF
  4. 4Fast lookupBINARY SEARCH

The result

Distinct customers, totals per customer from COLLECT, and the same totals from AT END OF, all from one sorted table.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A list of sales lines (customer, product, amount) must become two results: the list of distinct customers, and the total amount per customer. The data is already in memory.

2. How we design it

Sort once by customer, so equal customers sit next to each other. Many classic statements rely on that order. Then take two roads to the same totals: COLLECT adds amounts into a result table by key, and a LOOP with AT END OF and SUM prints a total each time the customer changes. A READ with BINARY SEARCH shows the fast lookup that sorted data allows.

Why we decided it this way

  1. 1SORT firstDELETE ADJACENT DUPLICATES, AT END OF and BINARY SEARCH all assume sorted data. Without it the result is wrong, with no error.
  2. 2A separate result table for COLLECTCOLLECT adds numeric fields for rows with the same character-like key, so the table must hold only the key and the amount.
  3. 3Explicit work areasNo table header lines: they are obsolete and hide where data is.

Before you start

  • An on-premise system or classic ABAP environment and SE38 or ADT. No database table is needed.

3. The code, step by step

Step 1 of 4: Declare, fill and sort

Four sales lines, sorted by customer.

ABAP · Report ZDEMO_ITAB_CLASSIC
" Types and tables
REPORT zdemo_itab_classic.

TYPES: BEGIN OF ty_sale,
         customer TYPE c LENGTH 10,
         product  TYPE c LENGTH 10,
         amount   TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_sale,
       BEGIN OF ty_total,
         customer TYPE c LENGTH 10,
         amount   TYPE p LENGTH 8 DECIMALS 2,
       END OF ty_total.

DATA: gt_sales TYPE STANDARD TABLE OF ty_sale  WITH DEFAULT KEY,
      gt_total TYPE STANDARD TABLE OF ty_total WITH DEFAULT KEY,
      gt_cust  TYPE STANDARD TABLE OF ty_sale  WITH DEFAULT KEY,
      gs_sale  TYPE ty_sale,
      gs_total TYPE ty_total.
" Fill and sort
START-OF-SELECTION.
  gt_sales = VALUE #(
    ( customer = 'ACME'   product = 'PUMP'  amount = '100.00' )
    ( customer = 'GLOBEX' product = 'VALVE' amount = '80.00'  )
    ( customer = 'ACME'   product = 'VALVE' amount = '150.00' )
    ( customer = 'ACME'   product = 'PUMP'  amount = '100.00' ) ).
  SORT gt_sales BY customer product.
Part by part (plain text)
  1. Types and tables. The line type has two character fields and a packed amount. DEFAULT KEY makes all non-numeric fields the table key, which COLLECT needs. The totals table has only customer and amount.
  2. Fill and sort. VALUE fills the table with four lines. SORT BY customer product orders them, so all lines of one customer are next to each other.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

Distinct customers, totals per customer from COLLECT, and the same totals from AT END OF, all from one sorted table.

What you should see

Customers: ACME, GLOBEX Collect: ACME 350.00, GLOBEX 80.00 At end of: ACME 350.00, GLOBEX 80.00 Binary search for GLOBEX: 80.00

Try it and check the result

  • Run with F8. The list shows the four blocks: customers, collect, at end of, binary search.
  • Remove the first SORT line (SORT gt_sales BY customer product) and run again: DELETE ADJACENT DUPLICATES may leave a customer twice, and AT END OF prints several partial totals. This is the classic bug.
  • Add a line with a new customer and run again: both totals blocks pick it up.

Common mistakes

  • BINARY SEARCH on an unsorted table does not fail. It just returns wrong answers.
  • COLLECT adds numeric fields only for tables where the other fields form the key. A table with an extra text field in the line will collect each line separately.
  • In AT END OF blocks, character fields after the group key show '*' and the other fields their initial values. Read what you need before the block, or use only the key and the summed fields.
  • For new code, GROUP BY in a loop or a sorted or hashed table with a key is usually clearer. These statements still matter because most existing programs use them.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: Classic ABAP on SAP NetWeaver 7.40 SP08 or higher (ECC 6.0 EHP7+) and S/4HANA on-premise (SAP GUI). Not available in ABAP Cloud development. Check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.