LOOP AT GROUP and FOR GROUPS OF in ABAP
Learn how to use LOOP AT GROUP and FOR GROUPS OF in ABAP to group and process internal table data, replacing obsolete AT NEW statements.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
*To group internal table data and process it based on specific fields, older statements like AT NEW <field> and AT END OF <field> within loops were commonly used.
*However, these statements are now obsolete.
*But now, the new ABAP Syntax 𝗟𝗢𝗢𝗣 𝗔𝗧 𝗚𝗥𝗢𝗨𝗣 or 𝗙𝗢𝗥 𝗚𝗥𝗢𝗨𝗣𝗦 𝗢𝗙 used to achieve the same functionality.
DATA(lt_courses) = VALUE tt_course (
( employee_id = '1' course_id = '1' course_name = 'ABAP' amount = '1000' )
( employee_id = '1' course_id = '2' course_name = 'UI5' amount = '2000' )
( employee_id = '1' course_id = '3' course_name = 'Fiori' amount = '3000' )
( employee_id = '2' course_id = '1' course_name = 'ABAP' amount = '1000' )
( employee_id = '2' course_id = '2' course_name = 'UI5' amount = '2000' )
( employee_id = '2' course_id = '3' course_name = 'Fiori' amount = '3000' )
( employee_id = '3' course_id = '1' course_name = 'ABAP' amount = '1000' )
).
"Loop At Group and For in Group
DATA(lt_employee_w_loop) = VALUE tt_employee( ).
LOOP AT lt_courses INTO DATA(wa)
GROUP BY ( employee_id = wa-employee_id
size = GROUP SIZE
index = GROUP INDEX )
ASSIGNING FIELD-SYMBOL(<course>).
DATA(ls_employee) = VALUE ty_employee(
employee_id = <course>-employee_id
no_of_courses = <course>-size
total_amount = 0 ).
"Option 1
LOOP AT GROUP <course> ASSIGNING FIELD-SYMBOL(<member>).
ls_employee-total_amount += <member>-amount.
ENDLOOP.
"Option 2
ls_employee-total_amount = REDUCE #(
INIT amount = 0
FOR <member> IN GROUP <course>
NEXT amount = amount + <member>-amount
).
APPEND ls_employee TO lt_employee_w_loop.
ENDLOOP.
"For in Group
DATA(lt_employee_w_for) = VALUE tt_employee(
FOR GROUPS course OF wa IN lt_courses
GROUP BY ( employee_id = wa-employee_id
size = GROUP SIZE
index = GROUP INDEX )
( employee_id = course-employee_id
no_of_courses = course-size
total_amount = REDUCE #(
INIT amount = 0
FOR member IN GROUP course
NEXT amount = amount + member-amount
)
)
).
"Output
"EMPLOYEE_ID NO_OF_COURSES TOTAL_AMOUNT
"00000001 3 6000
"00000002 3 6000
"00000003 1 1000
This post is licensed under CC BY 4.0 by the author.