Application Development 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: 

AT END OF within LOOP does not work

oliver_ried
Explorer
0 Kudos

Hello,

I want to loop over a table and make sums per arbpl:

TYPES: BEGIN OF hd_auf,

aufnr TYPE afko-aufnr,

matnr TYPE afko-plnbez,

eckd TYPE afko-gltrp,

arbpl TYPE crhd-arbpl,

rmenge TYPE zzppauftrag-gutmenge,

rzeit TYPE zzppauftrag-ruestzeit,

ist TYPE zzppauftrag-istgeschw,

END OF hd_auf.

TYPES: auftragsinfo TYPE TABLE OF hd_auf.

DATA: auf_header TYPE hd_auf.

DATA: auf TYPE auftragsinfo.

... put data into table auf ...

SORT auf BY arbpl ASCENDING eckd ASCENDING.

LOOP AT auf INTO auf_header.

WRITE:/01 auf_header-arbpl, auf_header-aufnr, auf_header-matnr,

auf_header-eckd, auf_header-rmenge, auf_header-ist,

auf_header-rzeit, status.

AT END OF arbpl.

SUM.

WRITE: /01 auf_header-rzeit.

ENDAT.

ENDLOOP.

This does not work. I will always get one sum per line, regardless how much equal lines with the same arbpl are in the table auf.

This is the output (the line with the number is the sum-line):

D-OFF 1000840 110220-1

0,013

D-OFF 1000740 211220-1

16,667

THM-U001 1000642 250220-17373G

0,028

THM-U001 1000660 250220-10021S

0,167

UMM. 001 1000640 250220-17373G

0,111

UMM. 001 1000641 250220-17373G

0,056

UMM. 001 1000629 250220-17373G

0,111

UMM. 001 1000780 211220-10101

0,167

UMM. 001 1000741 211220-10101

0,139

But I need the following output:

D-OFF 1000840 110220-1

D-OFF 1000740 211220-1

16,68

THM-U001 1000642 250220-17373G

THM-U001 1000660 250220-10021S

0,195

UMM. 001 1000640 250220-17373G

UMM. 001 1000641 250220-17373G

UMM. 001 1000629 250220-17373G

UMM. 001 1000780 211220-10101

UMM. 001 1000741 211220-10101

0,584

What is wrong with my loop?

Could anyone please help me?

Thanks.

Regards,

Oliver

1 ACCEPTED SOLUTION

JozsefSzikszai
Active Contributor
0 Kudos

hi Oliver,

AT END OF works like that each single field are taken into account which are before <i>arbpl</i>. So in your case aufnr, matnr, eckd and arbpl are concatenated by the system and SAP sees as one string and carries out the AT END OF statement accordingly.

You have to define your internal table so, that arbpl is the first field in the table, than it works properly.

hope this helps

ec

7 REPLIES 7

Former Member
0 Kudos

Hi Oliver,

first Sort itab by aufnr and arbpl and then do sum at end of arbpl.

or keep arbpl in the first place of itab

Reward if it helps,

Satish

Message was edited by:

Satish Panakala

JozsefSzikszai
Active Contributor
0 Kudos

hi Oliver,

AT END OF works like that each single field are taken into account which are before <i>arbpl</i>. So in your case aufnr, matnr, eckd and arbpl are concatenated by the system and SAP sees as one string and carries out the AT END OF statement accordingly.

You have to define your internal table so, that arbpl is the first field in the table, than it works properly.

hope this helps

ec

Former Member
0 Kudos

Hi

U should consider this:

TYPES: BEGIN OF hd_auf,
                 aufnr  TYPE afko-aufnr,
                 matnr TYPE afko-plnbez,
                 eckd  TYPE afko-gltrp,
                 arbpl  TYPE crhd-arbpl,
.................................................

LOOP AT auf INTO auf_header.
...........................................
    AT END OF arbpl.

in this situation the event is triggered as soon as a value at the left of ARBPL is changing.

If you want be sure the event triggered in right way your table should have ARBPL as first field:

TYPES: BEGIN OF hd_auf,
                 arbpl  TYPE crhd-arbpl,
                 aufnr  TYPE afko-aufnr,
                 matnr TYPE afko-plnbez,
                 eckd  TYPE afko-gltrp,
                 ...................................

Max

Former Member
0 Kudos

HI,

when you use AT new or AT END events, it works when change all the fields before the field you indicate. So at the change of:

aufnr matnr eckd arbpl

.

rgs

ferry_lianto
Active Contributor
0 Kudos

Hi,

Please try this ... perhaps it may help.


...

SORT auf STABLE BY arbpl ASCENDING eckd ASCENDING.

...

Regards,

Ferry Lianto

Former Member
0 Kudos

Hi,

LOOP AT auf INTO auf_header.

WRITE:/01 auf_header-arbpl, auf_header-aufnr, auf_header-matnr,

auf_header-eckd, auf_header-rmenge, auf_header-ist,

auf_header-rzeit, status.

AT END OF aufnr.

SUM.

WRITE: /01 auf_header-rzeit.

ENDAT.

ENDLOOP.

Try this,

KC

oliver_ried
Explorer
0 Kudos

Guys, you are great.

I have moved the field arbpl to the first field of the

table, and now it works.

Thank you very much for the quick help !