Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to use modify inside a loop at group block?

former_member625844
Participant
0 Likes
4,297

I have internal table like below.

Month number Index

-----------------------------------

1 5

1 6

2 3

---------------------------------------

Now I want to add index based on month. The result will like below

Month number Index

-----------------------------------

1 5 1

1 6 2

2 3 1

---------------------------------------

I want to use loop group to implement.

The code is like below.

loop at itab into data(wa)
    location = sy-tabix.
    loop at group wa into data(wa1)
       wa1-index = sy-tabix - location.
       modify itab from wa1.
    endloop.
end loop.

But the modify line has error like

"DELETE itab." 和 "MODIFY itab FROM wa." can't use in  "LOOP AT itab ... USING KEY keyname ..." <br>

Then what's the correct syntax to write back to the itab? Thx.

1 ACCEPTED SOLUTION
Read only

venkateswaran_k
Active Contributor
0 Likes
3,567

Hi

loop at itab into data(wa)
    location = sy-tabix.
    loop at group wa into data(wa1)
       wa-index = sy-tabix - location.            <== use wa-index
       modify itab from wa INDEX location.        <== use INDEX keyword
    endloop.
end loop.

I have internal table like below.

Month number Index

-----------------------------------

1 5

1 6

2 3

---------------------------------------

Now I want to add index based on month. The result will like below

Month number Index

-----------------------------------

1 5 1

1 6 2

2 3 1

---------------------------------------

I want to use loop group to implement.

The code is like below.

loop at itab into data(wa)
    location = sy-tabix.
    loop at group wa into data(wa1)
       wa1-index = sy-tabix - location.
       modify itab from wa1.
    endloop.
end loop.

But the modify line has error like

"DELETE itab." 和 "MODIFY itab FROM wa." can't use in  "LOOP AT itab ... USING KEY keyname ..." <br>

Then what's the correct syntax to write back to the itab? Thx.

5 REPLIES 5
Read only

MateuszAdamus
Active Contributor
3,567

Hi loki_luo15

You cannot modify the internal table inside the group loop: "The internal table itab cannot be modified in the group loop unless the addition WITHOUT MEMBERS is specified.".

https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abaploop_at_itab_group_by.htm.

Regards,
Mateusz

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,567

Your code doesn't make sense: how can you use LOOP AT GROUP inside a loop which doesn't use GROUP BY? Moreover, probably you didn't understand that MODIFY itab FROM wa1 modifies the line currently iterated by LOOP AT itab, so it's useless doing it several times (inside a secondary loop). As what you want to achieve is unclear, I cannot propose a solution.

Read only

former_member625844
Participant
0 Likes
3,567

@sandra.rossi. My mistake. The outer loop should be loop at itab into data(wa) group by wa-month. Then inside I will loop through this month's record and asign index to each.

Read only

nandini_borse
Participant
0 Likes
3,567

Issue resolve ? if yes, please put your code here.

Thanks !!

Read only

venkateswaran_k
Active Contributor
0 Likes
3,568

Hi

loop at itab into data(wa)
    location = sy-tabix.
    loop at group wa into data(wa1)
       wa-index = sy-tabix - location.            <== use wa-index
       modify itab from wa INDEX location.        <== use INDEX keyword
    endloop.
end loop.