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

Former Member
0 Kudos

Hi guys,

How does AT END OF works? Thanks a lot!

Rgds,

mark

1 ACCEPTED SOLUTION

rahul2000
Contributor
0 Kudos

Hi mark...

first c the following code..

SORT IMARD1 BY MATNR.

LOOP AT IMARD1 INTO WMARD1.

LW_LABST = LW_LABST + WMARD1-LABST.

AT END OF MATNR.

MOVE:LW_LABST TO WMARD1-LABST.

MODIFY IMARD1 FROM WMARD1 TRANSPORTING LABST.

CLEAR:LW_LABST.

ENDAT.

CLEAR:WMARD1.

ENDLOOP.

IMARD1 is declared as follows..

TYPES:BEGIN OF ITAB_MARD1 , "Storage Location Data for Material

MATNR TYPE MARD-MATNR , "Material Number

WERKS TYPE MARD-WERKS , "Plant

LGORT TYPE MARD-LGORT , "Storage location

LABST TYPE MARD-LABST , "Valuated stock with unrestricted use

INSME TYPE MARD-INSME , "Stock in quality inspection

SPEME TYPE MARD-SPEME , "Blocked stock

END OF ITAB_MARD1 .

Note taht here MATNR is the first field of the internal table.

So whenevr it changes you AT END OF event will get triggered.

Hence it is important for you to sort it by MATNR.

Now consider that MATNR is not the first of u r internal table but u still want to use AT END OF MATNR..

eg

Data for Material

LGORT TYPE MARD-LGORT , "Storage location

MATNR TYPE MARD-MATNR , "Material Number

WERKS TYPE MARD-WERKS , "Plant

LABST TYPE MARD-LABST , "Valuated stock with unrestricted use

INSME TYPE MARD-INSME , "Stock in quality inspection

SPEME TYPE MARD-SPEME , "Blocked stock

END OF ITAB_MARD1 .

Then here your AT END OF event will get triggered even if LGORT changes even though MATNR has not changed..

So in this case u might have to sort it storage location wise

or both storage location and material wise...

I hope u got what u require..

Do reward if this helps you.

14 REPLIES 14

Former Member
0 Kudos

Hi,

At the end of a control group ( AT END OF , AT LAST ), there are two types of control level information between AT and ENDAT :

If the sort key of the extract dataset contains a non-numeric field h (particularly in the field group HEADER ), the field CNT(h) contains the number of control breaks in the (subordinate) control level h .

For extracted number fields g (see also ABAP/4 number types ), the fields SUM(g) contain the relevant control totals.

Notes

The fields CNT(h) and SUM(g) can only be addressed after they have been sorted. Otherwise, a runtime error may occur.

The fields CNT(h) and SUM(g) are filled with the relevant values for a control level at the end of each control group ( AT END OF , AT LAST ), not at the beginning ( AT FIRST , AT NEW ).

When calculating totals with SUM(g) , the system automatically chooses the maximum field sizes so that an overflow occurs only if the absolute value area limits are exceeded.

You can also use special control break control structures with LOOP s on internal tables.

Ex.

DATA: NAME(30),

SALES TYPE I.

FIELD-GROUPS: HEADER, INFOS.

INSERT: NAME INTO HEADER,

SALES INTO INFOS.

...

LOOP.

AT NEW NAME.

NEW-PAGE.

ENDAT.

...

AT END OF NAME.

WRITE: / NAME, SUM(SALES).

ENDAT.

ENDLOOP.

Notes

If the extract dataset is not sorted before processing with LOOP , no control level structure is defined and the statements following AT NEW or AT END OF are not executed.

Fields which stand at hex zero are ignored by the control break check with AT NEW or AT END OF . This corresponds to the behavior of the SORT statement, which always places unoccupied fields (i.e. fields which stand at hex zero) before all occupied fields when sorting extract datasets, regardless of whether the sort sequence is in ascending or descending order.

Regards,

Bhaskar

Former Member
0 Kudos

This is a control break statement it works with in the

loop.... endloop.

there are many purposes of at end of statement one of them is it calulates the sum of integer values within the loop.

Former Member
0 Kudos

Hi ,

AT END OF is a event it is used to calculate the grand totals in the table .It is control statement event

if it is helpful reward points

0 Kudos

sample code:

loop at wtab.

at new waers.

........

endat.

at end of waers.

.................

endat.

endloop.

how does at end of work?

0 Kudos

hi Mark,

let's say you have internal table like:

line waers ...

1 EUR

2 EUR

3 USD

4 USD

5 USD

(pls. note that line is not a field of the internal table, I just numbered the lines of the internal table because of better understanding!)

code:

loop at wtab.

at new waers.

==> line 1 and 3 will be processed

endat.

at end of waers.

==> line 2 and 5 will be processed

endat.

endloop.

you also have to take into account, that there should NOT be any more fields in the itab before waers (otherwise it will work differently) and the internal table has to be sorted acc. to waers.

hope this helps

ec

Former Member
0 Kudos

It happens when you get the last record of an ordered sequence, before starting the next.



LOOP AT itab INTO wa.
   AT END OF field1.
*     DO SOMETHING.
   ENDAT.
ENDLOOP.

For instance, you have ten records, five of them contains "1" in the field1 field and five contains "2". If the table is sorted by that field, the AT END OF event will raise when the last "1" field value is being executed.

0 Kudos

HI Rodrigo,

Thanks for that...

What would happen id the table is not sorted by field1? its sorted with different field name?

0 Kudos


If you have sorted an extract dataset by the fields <f1>, <f2>, ..., the processing of the control levels should be written between the other control statements as follows:

LOOP.
  AT FIRST.... ENDAT.
    AT NEW <f1>....... ENDAT. 
      AT NEW <f2>....... ENDAT. 
     ...
          AT <fgi>..... ENDAT. 
          <single line processing without control statement>
...
      AT END OF <f2>.... ENDAT. 
    AT END OF <f1>.... ENDAT. 
  AT LAST..... ENDAT.
ENDLOOP.

check this link

http://help.sap.com/saphelp_nw04/helpdata/en/9f/db9f1f35c111d1829f0000e829fbfe/frameset.htm

Former Member
0 Kudos

Hi Mark,

while working with internal tables we have five control break statements.

1)at new

2)at first

3)at last

4)at end of

5)on change

all the above statements use contorl level.

working of at end of.

you have an internal table and in that you will enter data.for example assume that into my internal table i want to insert 4 records.the values in the internal table are a,b,c.so if i use at end of b then after inserting first record and value of b it will be triggerred,after second record and after inserting value b it will again triggerred.like that it will trigger four times because you will have to enter four records.basically you use at end of to sum up each value.for example in my four records i want to sum the values of b.if the values are 1,2,3,4 then after inserting first record the sum is 1 and after second record the sum is 3 and after third record the sum is 6 and after the fourth record the sum is 10.till now if you are not clear use the following example.i am writing a statement after each record.

data:begin of itab occurs 0,

a type i,

b type i,

c type i,

end of itab.

itab-a = 1.itab-b = 2.itab-c = 3.

append itab.

itab-a = 1.itab-b = 3.itab-c = 3.

append itab.

itab-a = 2.itab-b = 3.itab-c = 3.

append itab.

itab-a = 1.itab-b = 3.itab-c = 3.

append itab.

loop at itab.

write: / itab-a,itab-b,itab-c.

at end of b.

write: / 'sai'.

endat.

endloop.

after 2,after 3,after 3,after 3 it will be triggerred .

please reward points if useful.

rahul2000
Contributor
0 Kudos

Hi mark...

first c the following code..

SORT IMARD1 BY MATNR.

LOOP AT IMARD1 INTO WMARD1.

LW_LABST = LW_LABST + WMARD1-LABST.

AT END OF MATNR.

MOVE:LW_LABST TO WMARD1-LABST.

MODIFY IMARD1 FROM WMARD1 TRANSPORTING LABST.

CLEAR:LW_LABST.

ENDAT.

CLEAR:WMARD1.

ENDLOOP.

IMARD1 is declared as follows..

TYPES:BEGIN OF ITAB_MARD1 , "Storage Location Data for Material

MATNR TYPE MARD-MATNR , "Material Number

WERKS TYPE MARD-WERKS , "Plant

LGORT TYPE MARD-LGORT , "Storage location

LABST TYPE MARD-LABST , "Valuated stock with unrestricted use

INSME TYPE MARD-INSME , "Stock in quality inspection

SPEME TYPE MARD-SPEME , "Blocked stock

END OF ITAB_MARD1 .

Note taht here MATNR is the first field of the internal table.

So whenevr it changes you AT END OF event will get triggered.

Hence it is important for you to sort it by MATNR.

Now consider that MATNR is not the first of u r internal table but u still want to use AT END OF MATNR..

eg

Data for Material

LGORT TYPE MARD-LGORT , "Storage location

MATNR TYPE MARD-MATNR , "Material Number

WERKS TYPE MARD-WERKS , "Plant

LABST TYPE MARD-LABST , "Valuated stock with unrestricted use

INSME TYPE MARD-INSME , "Stock in quality inspection

SPEME TYPE MARD-SPEME , "Blocked stock

END OF ITAB_MARD1 .

Then here your AT END OF event will get triggered even if LGORT changes even though MATNR has not changed..

So in this case u might have to sort it storage location wise

or both storage location and material wise...

I hope u got what u require..

Do reward if this helps you.

Former Member
0 Kudos

Hi Rahul,,,,

That was so detailed that I really understand it... 10 points is not enough to thank you...

To everyone who had helped me... Thanks you so much!!!

God bless everyone!

Rgds,

Mark

0 Kudos

Thanks a ton Mark..

Cheers

Former Member
0 Kudos

Hi

sampel program for AT events

  • Using AT FIRST , AT NEW, AT THE END OF , AT LAST.

DATA: BEGIN OF ITAB OCCURS 0,

F1 TYPE I,

F2(6) TYPE C,

F3(10) TYPE N,

F4(16) TYPE P DECIMALS 2,

END OF ITAB.

DATA: SUB_TOT(10) TYPE P DECIMALS 3.

**--1

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 30.

ITAB-F4 = '3000.00'.

APPEND ITAB.

CLEAR ITAB.

*--2

ITAB-F1 = 2.

ITAB-F2 = 'TWO'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 2.

ITAB-F2 = 'TWO'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

*-- 3

ITAB-F1 = 3.

ITAB-F2 = 'THREE'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 3.

ITAB-F2 = 'THREE'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

SORT ITAB BY F1.

LOOP AT ITAB.

AT FIRST.

WRITE: /35 ' MATERIAL DETAILS:'.

ULINE.

ENDAT.

AT NEW F1.

WRITE: / 'DETAILS OF MATERIAL:' COLOR 7 , ITAB-F1.

ULINE.

ENDAT.

WRITE: / ITAB-F1, ITAB-F2, ITAB-F3, ITAB-F4.

SUB_TOT = SUB_TOT + ITAB-F4.

AT END OF F1.

ULINE.

WRITE: / 'SUB TOTAL :' COLOR 3 INVERSE ON, SUB_TOT COLOR 3 INVERSE ON.

CLEAR SUB_TOT.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: 'SUM:', ITAB-F4.

ULINE.

ENDAT.

ENDLOOP.