ABAP VALUE with FOR is one of the most useful features of new ABAP syntax for developers who work with internal tables. It helps you create a new internal table ABAP from existing data, filter rows, and map one table structure to another table structure in a compact way. Instead of writing a long LOOP AT block with APPEND, you can express the result directly.
ABAP VALUE with FOR is one of the most useful features of new ABAP syntax for developers who work with internal tables. It helps you create a new internal table from existing data, filter rows, and map one table structure to another table structure in a compact way. Instead of writing a long LOOP AT block with APPEND, you can express the result directly.
What is the ABAP VALUE operator?
The ABAP VALUE operator is a constructor expression. It creates a value for a structure, an internal table, or another suitable data object. For internal tables, VALUE can add lines directly. When you combine VALUE with FOR, ABAP iterates over another table and builds the result table line by line.
The basic idea looks like this:
|
1 2 3 4 5 6 |
DATA(result_table) = VALUE table_type( FOR line IN source_table WHERE ( some_field = some_value ) ( field1 = line-field1 field2 = line-field2 ) ). |
The important keywords are easy to remember: VALUE creates the result, FOR reads the source table, WHERE is optional, and the last parentheses describe the new line of the target internal table.
Old ABAP syntax: fill and filter an internal table
First, let us prepare test data in real old ABAP style. There are no inline declarations, no VALUE operator, no conversion constructor, and no table constructor expression here. We declare an internal table and a work area, fill every field manually, and append each line.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
TYPES: BEGIN OF ty_item, id TYPE i, material TYPE string, quantity TYPE i, price TYPE p LENGTH 8 DECIMALS 2, END OF ty_item. TYPES tt_items TYPE STANDARD TABLE OF ty_item. DATA lt_items TYPE tt_items. DATA ls_item TYPE ty_item. CLEAR ls_item. ls_item-id = 1. ls_item-material = 'A-100'. ls_item-quantity = 2. ls_item-price = '10.50'. APPEND ls_item TO lt_items. CLEAR ls_item. ls_item-id = 2. ls_item-material = 'B-200'. ls_item-quantity = 1. ls_item-price = '25.00'. APPEND ls_item TO lt_items. CLEAR ls_item. ls_item-id = 3. ls_item-material = 'C-300'. ls_item-quantity = 5. ls_item-price = '7.20'. APPEND ls_item TO lt_items. |
Now imagine that we need a second internal table that contains only items where quantity is greater than 1. In classic ABAP, this usually means an explicit result table, a work area, a LOOP AT statement, an IF condition, and APPEND.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DATA lt_result TYPE tt_items. DATA ls_result TYPE ty_item. LOOP AT lt_items INTO ls_item. IF ls_item-quantity > 1. CLEAR ls_result. ls_result-id = ls_item-id. ls_result-material = ls_item-material. ls_result-quantity = ls_item-quantity. ls_result-price = ls_item-price. APPEND ls_result TO lt_result. ENDIF. ENDLOOP. |
This code is correct and easy to debug. The disadvantage is that the technical steps take more space than the actual business meaning: create a result table from selected rows.
New ABAP syntax: VALUE with FOR and WHERE
The same logic can be written with ABAP VALUE and FOR. This is the new syntax version of the previous example.
|
1 2 3 4 5 6 7 8 |
DATA(lt_result) = VALUE tt_items( FOR ls_item IN lt_items WHERE ( quantity > 1 ) ( id = ls_item-id material = ls_item-material quantity = ls_item-quantity price = ls_item-price ) ). |
The result is the same. ABAP loops over lt_items, keeps only lines where quantity is greater than 1, and creates a new line in lt_result for every matching item. This is why VALUE with FOR is very useful for simple internal table transformations.
Mapping one internal table to another structure
A common use case is mapping one internal table type to another. For example, we may not need all fields from the original table. Instead, we want a new table with material and calculated total amount.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TYPES: BEGIN OF ty_item_total, id TYPE i, material TYPE string, total TYPE p LENGTH 10 DECIMALS 2, END OF ty_item_total. TYPES tt_item_totals TYPE STANDARD TABLE OF ty_item_total. DATA(lt_totals) = VALUE tt_item_totals( FOR ls_item IN lt_items ( id = ls_item-id material = ls_item-material total = ls_item-quantity * ls_item-price ) ). |
This example is a good fit for new ABAP syntax because the calculation is simple. The code says exactly what happens: lt_totals is built from lt_items, and each result line contains id, material, and total.
When should you use VALUE with FOR?
Use ABAP VALUE with FOR when you can describe the transformation in one short expression. It works best for small and medium internal table operations where the result is easy to understand.
- Build a new internal table from an existing internal table.
- Filter rows with a simple WHERE condition.
- Map fields from one structure to another structure.
- Add simple calculated fields such as quantity * price.
- Create short helper tables for later processing.
When is LOOP AT better?
Do not replace every LOOP AT with VALUE and FOR. If the logic is complex, a classic loop can still be the better choice. Use a normal LOOP when you need several conditions, error handling, database access, method calls with side effects, or step-by-step debugging.
A good rule is simple: new ABAP syntax should make the code clearer. If VALUE with FOR makes the code harder to read, keep the classic LOOP.
FAQ: ABAP VALUE with FOR
Can VALUE with FOR replace LOOP AT in ABAP?
It can replace simple LOOP AT blocks that only build a new internal table. It should not replace complex business logic.
Can I use WHERE inside ABAP FOR expressions?
Yes. You can add WHERE after FOR to filter the source internal table before building the result.
Is VALUE with FOR available in old ABAP systems?
No. This is new ABAP syntax and is typically used in ABAP 7.40 and later releases. Always check your SAP system release.
ABAP VALUE with FOR is a practical tool for modern ABAP development. It is especially useful for internal table transformations, filtering, and simple mapping between structures. The best approach is not to use new syntax everywhere, but to use it where it makes ABAP code shorter, cleaner, and easier to understand.