01-19-2006 5:45 PM
Hi all, can you please tell me what is the meaning of AT END of in this loop. I know it is calculating summation of a values in the structure XTAB4, but I am not able to understand why is CHARG specified.
Can anyone please help me.
LOOP AT XTAB4.
TEMP_MATNR = XTAB4-MATNR.
TEMP_WERKS = XTAB4-WERKS.
TEMP_LGORT = XTAB4-LGORT.
TEMP_CHARG = XTAB4-CHARG.
AT END OF CHARG.
SUM.
SCHD_STOCK = XTAB4-VBMNJ.
READ TABLE MCHB_INVT_BAL
WITH KEY MATNR = XTAB4-MATNR
WERKS = XTAB4-WERKS
LGORT = XTAB4-LGORT
CHARG = XTAB4-CHARG.
IF SY-SUBRC = 0.
MCHB_INVT_BAL-SDLVY = SCHD_STOCK.
MODIFY MCHB_INVT_BAL INDEX SY-TABIX.
CLEAR MCHB_INVT_BAL.
ENDIF.
ENDAT.
ENDLOOP.
the structure of
DATA : BEGIN OF XTAB4 OCCURS 0,
MATNR LIKE VBBE-MATNR,
WERKS LIKE VBBE-WERKS,
LGORT LIKE VBBE-LGORT,
CHARG LIKE VBBE-CHARG,
VBMNA LIKE VBBE-OMENG,
VBMNB LIKE VBBE-OMENG,
VBMNC LIKE VBBE-OMENG,
VBMNE LIKE VBBE-OMENG,
VBMNG LIKE VBBE-OMENG,
VBMNI LIKE VBBE-OMENG,
VBMNJ LIKE VBBE-OMENG,
VRKME LIKE VBEP-VRKME,
WMENG LIKE VBEP-WMENG,
END OF XTAB4.
thanks,
Sabrina.
01-19-2006 6:54 PM
Hi Sabrina,
Taking your example consider the following:
You will have to respect the values before CHARG to SUM:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(01)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(02)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(03)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(y) VBMNA(04)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(y) VBMNA(05)
SUM at this point by the field CHARG will be:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) = 06
MATNR (1) WERKS (1000) LGORT(p1) CHARG(y) = 09
But now consider this:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(01)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(02)
MATNR (1) WERKS (1000)
LGORT(p2)
CHARG(x) VBMNA(03)LGORT(p2)
CHARG(y) VBMNA(04)LGORT(p2)
CHARG(y) VBMNA(05)MATNR (2)
WERKS (1000) LGORT(p2) CHARG(y) VBMNA(05)SUM at this point by the field CHARG will be:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) = 03
MATNR (1) WERKS (1000) LGORT(p2) CHARG(x) = 03
MATNR (1) WERKS (1000) LGORT(p2) CHARG(y) = 04
MATNR (2) WERKS (1000) LGORT(p2) CHARG(y) = 05
The way you do the SORT of your table by the fields until field CHARG will change your final output.
Hope this helps you
Mário
01-19-2006 5:49 PM
Hi,
At end of <f> (F: Field in you internal table) between loop and end loop means :
End of a group of lines with the same contents in the field <f> and in the fields left of <f>.
Lanka
01-19-2006 5:57 PM
thanks murthy for getting back.
for example if i have the following internal table
matnr plant batch value1 value2
1 p1 x 10 12
1 p2 x 12 25
2 p1 x 24 34
2 p2 x 03 02
3 p2 y 02 10
in the above example is it that
AT END of batch
sum
calculates value1= 49 and not 51 because batch is y in the last line.
same with value2 too....
please let me know..
thanks,
Sabrina.
01-19-2006 6:53 PM
Hi Sabrina ,
You are correct.But make sure you sort the table first.
01-19-2006 7:08 PM
Hi Sabarina,
Please sortn your internal table by matner plant batch.
matnr plant batch value1 value2
1 p1 x 10 12
1 p2 x 12 25
2 p1 x 24 34
2 p2 x 03 02
3 p2 y 02 10
sort itab by matnr plant batch.
Loop at itab.
at end of batch.
sum.
endat.
endloop.
in your example:
sort XTAB4 by matnr werks lgort charg.
Loop at XTAB4.
At end of Charg.
sum.
endloop.
This will work for you.
Lanka
01-19-2006 8:43 PM
01-19-2006 11:39 PM
Hi,
everything is fine, but AT END OF works a little special in ABAP.
The Processing of Control Levels with AT NEW <f> or AT END OF <f> is done when a control break occurs. This is the case when the value of the field <f> or a superior field in the current record has a different value from the previous record (AT NEW) or the subsequent record (AT END).
For the sample table
matnr plant batch value1 value2
1 p1 x 10 12
1 p2 x 12 25
2 p1 x 24 34
2 p2 x 03 02
3 p2 y 02 10
it means, that AT END OF batch is processed for line 1, because the superior field plant (P1) is different from plant p2 in subsequent record 2.
Also processed for line 2 for the change of plant in record 3.
Processed again for line 3 for the plant change of line 4.
Also for record 4 due to change of batch (and material!) in record 5.
Processed in record 5, because it's at end of anything.
See a good description at
http://help.sap.com/saphelp_47x200/helpdata/en/9f/db9f1f35c111d1829f0000e829fbfe/frameset.htm
In our sample, the characteristics of material batches in plants would be determined. As we do have all different combinations of material, plant and batch, in this case the use of at end of and sum is useless.
The ABAP approach to control level looks strange at first but as soon as you got the concept can be extremely useful.
regards,
Clemens
01-19-2006 5:51 PM
Hi,
YOu can specify any field in the statement at end.AT end will just add up all numerical fields in the internal table and stores them in the same internal table with the same name.
Regards,
Vijay
01-19-2006 6:00 PM
Hi Sabrina,
Charg field is specified here to trigger the moment of summing values. So this summation wil take place at the last record with charg number xxxx.
Kind regards,
Michel Jonker
01-19-2006 6:54 PM
Hi Sabrina,
Taking your example consider the following:
You will have to respect the values before CHARG to SUM:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(01)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(02)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(03)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(y) VBMNA(04)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(y) VBMNA(05)
SUM at this point by the field CHARG will be:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) = 06
MATNR (1) WERKS (1000) LGORT(p1) CHARG(y) = 09
But now consider this:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(01)
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) VBMNA(02)
MATNR (1) WERKS (1000)
LGORT(p2)
CHARG(x) VBMNA(03)LGORT(p2)
CHARG(y) VBMNA(04)LGORT(p2)
CHARG(y) VBMNA(05)MATNR (2)
WERKS (1000) LGORT(p2) CHARG(y) VBMNA(05)SUM at this point by the field CHARG will be:
MATNR (1) WERKS (1000) LGORT(p1) CHARG(x) = 03
MATNR (1) WERKS (1000) LGORT(p2) CHARG(x) = 03
MATNR (1) WERKS (1000) LGORT(p2) CHARG(y) = 04
MATNR (2) WERKS (1000) LGORT(p2) CHARG(y) = 05
The way you do the SORT of your table by the fields until field CHARG will change your final output.
Hope this helps you
Mário