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: 

Control break statements.

Former Member
0 Kudos

Hello,

Please give me the explanation for the following statements

At first.

At new

At end of

sum

On change of

Regards

Basavaraj

8 REPLIES 8

Former Member
0 Kudos

Hi,

The field position is important for the fields that are using in the control break statements..Also SORT internal table is required..

DATA: BEGIN OF ITAB OCCURS 0,

MATNR TYPE MATNR,

WERKS TYPE WERKS_D,

VALUE TYPE NETPR,

END OF ITAB.

ITAB-MATNR = 'ABC'.

ITAB-WERKS = '0100'.

ITAB-VALUE = '10.00'.

APPEND ITAB.

ITAB-MATNR = '1ABC'.

ITAB-WERKS = '0100'.

ITAB-VALUE = '10.00'.

APPEND ITAB.

SORT ITAB BY MATNR WERKS.

LOOP AT ITAB.

AT END OF MATNR.

SUM.

WRITE: / ITAB-MATNR, 'MATERIAL TOTAL - ', ITAB-VALUE.

ENDAT.

AT END OF WERKS.

SUM.

WRITE: / ITAB-WERKS, 'PLANT TOTAL - ', ITAB-VALUE.

ENDAT.

ENDLOOP.

Thanks,

Abhay Singh.

<b>Rewards point for useful answer.</b>

Former Member
0 Kudos

Hi,

control break statements are

at new(every new record follows)

at first (at the top of the list i.e,first statement like headers)

at end of (every record ends it get triggered)

at last ( it is useful in summation of records) these r used in loop statements only

If you want to execute some statements for certain records of the dataset only, use the control statements AT and ENDAT.

The system processes the statement blocks between the control statements for the different options of AT as follows:

AT FIRST

The system executes the statement block once for the first record of the dataset.

AT LAST

The system executes the statement block once for the last record of the dataset.

You can also use the AT and ENDAT statements for control level processing.

SUM

Basic form

SUM.

Effect

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST ).

On Change of:

Effect:

The statements ON CHANGE OF and ENDON, which are forbidden in classes, define a control structure that can contain a statement block statement_block. After ON CHANGE OF, any number of data objects dobj1, dobj2... of any data type can be added..

Example:

In a SELECT loop, a statement block should only be executed if the content of the column CARRID has changed.

DATA spfli_wa TYPE spfli.

SELECT *

FROM spfli

INTO spfli_wa

ORDER BY carrid.

...

ON CHANGE OF spfli_wa-carrid.

...

ENDON.

...

ENDSELECT.

Regards,

Priyanka.

0 Kudos

what is the effect of end on statement

0 Kudos

whart is the effect of end on statement

Former Member
0 Kudos

Hi Basavraj,

Check these links for information regarding different Events in SAP

http://help.sap.com/saphelp_46c/helpdata/EN/9f/db9a1435c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_46c/helpdata/EN/9f/dba3ae35c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_di471/helpdata/EN/9f/dba3ae35c111d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_di471/helpdata/EN/9f/dba3ae35c111d1829f0000e829fbfe/content.htm

CONTROL STATEMENTS.

Control Break Statements

Control break statements are used to create statement blocks which process only specific table lines the LOOP – ENDLOOP block.

You open such a statement block with the control level statement AT and close it with the control level statement ENDAT. The syntax is as follows:

Table should be sorted when you use control-break statements

You can break the sequential access of internal tables by using these statements.

Syntax:

At first.

<Statement block>

Endat.

This is the first statement to get executed inside the loop (remember control break statements are applicable only inside the loop)

So in this block you can write or process those statements which you want to get executed when the loop starts.

At New carrid.

Write:/ carrid.

Endat.

In this case whenever the new carrid is reached, carrid will be written.

At End of carrid.

Uline.

Endat.

In this case whenever the end of carrid is reached, a line will be drawn.

At Last.

Write:/ ‘Last Record is reached’.

Endat.

Processing of statements within this block is done when entire processing of entire internal table is over. Usually used to display grand totals.

You can use either all or one of the above control break statements with in the loop for processing internal table.

At end of carrid.

Sum.

Endat.

In above case the statement SUM (applicable only within AT-ENDAT) will sum up all the numeric fields in internal table and result is stored in same internal table variable.

http://sap.mis.cmich.edu/sap-abap/abap03/sld001.htm

pls go through this

http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db9f1f35c111d1829f0000e829fbfe/content.htm


All this AT NEW, AT FIRST, AT END OF and AT LAST are called control break statements of Internal tables and are used to calculate the TOTALS based on sertain key fields in that internal table
FIrst to use these statements the ITAB has to be sorted by the key fields on whcih you need the SUM of the fields.
Some time you will get * when mopving data from this int table to other table using these commands
so you have to use
READ TABLE ITAB INDEX SY-TABIX in AT..ENDAT..if you are using other fields between them


DATA: sflight_tab TYPE SORTED TABLE OF sflight 
WITH UNIQUE KEY carrid connid fldate, 
sflight_wa LIKE LINE OF sflight_tab. 

SELECT * 
FROM sflight 
INTO TABLE sflight_tab. 

LOOP AT sflight_tab INTO sflight_wa. 
AT NEW connid. 
WRITE: / sflight_wa-carrid, 
sflight_wa-connid. 
ULINE. 
ENDAT. 
WRITE: / sflight_wa-fldate, 
sflight_wa-seatsocc. 
AT END OF connid. 
SUM. 
ULINE. 
WRITE: / 'Sum', 
sflight_wa-seatsocc UNDER sflight_wa-seatsocc. 
SKIP. 
ENDAT. 
AT END OF carrid. 
SUM. 
ULINE. 
WRITE: / 'Carrier Sum', 
sflight_wa-seatsocc UNDER sflight_wa-seatsocc. 
NEW-PAGE. 
ENDAT. 
AT LAST. 
SUM. 
WRITE: / 'Overall Sum', 
sflight_wa-seatsocc UNDER sflight_wa-seatsocc. 
ENDAT. 
ENDLOOP. 

<b>Reward pts if found usefull:)</b>

Regards

Sathish

Former Member
0 Kudos

Hello,

Control break stmt should be always in the LOOP ... ENDLOOP.

Check this example:

AT END OF f.

Effect

f is a sub-field of an internal table or extract dataset (EXTRACT) which is being processed with LOOP, i.e. the variants 1 and 2 only make sense within a LOOP.

Both "AT NEW f." and "AT END OF f. " introduce processing blocks which are concluded by " ENDAT.".

These processing blocks are processed whenever the contents of a field f or a sub-field defined before f change as a result of processing with LOOP. "AT NEW f." begins a new group of (table) lines with the same contents as the field f while "AT END OF f." concludes such a group.

Within the AT ... ENDAT processing of internal tables, all argument fields following f are filled with "*".

Examples

1. AT for sub-fields of an internal table

DATA: BEGIN OF COMPANIES OCCURS 20,

NAME(30),

PRODUCT(20),

SALES TYPE I,

END OF COMPANIES.

...

LOOP AT COMPANIES.

AT NEW NAME.

NEW-PAGE.

WRITE / COMPANIES-NAME.

ENDAT.

WRITE: / COMPANIES-PRODUCT, COMPANIES-SALES.

AT END OF NAME.

SUM.

WRITE: / COMPANIES-NAME, COMPANIES-SALES.

ENDAT.

ENDLOOP.

<b>rewards point for useful answer.</b>

Former Member
0 Kudos

control break stmts are used twhile displaying o/ps either to calc totals or to group the fields and count the rows rtc.

so u shud use it in the loop used to write the o/p.

check the simple ex prog using control breaks.

DATA:BEGIN OF ITAB OCCURS 0,

A1(10) TYPE C,

A3(10) TYPE C,

A2 TYPE I,

END OF ITAB.

DATA:TOT TYPE I,count TYPE I,i type i.

ITAB-A1 = 'char1'.

ITAB-A3 = 'abc'.

ITAB-A2 = '10'.

APPEND ITAB.

ITAB-A1 = 'char1'.

ITAB-A3 = 'def'.

ITAB-A2 = '20'.

APPEND ITAB.

ITAB-A1 = 'char1'.

ITAB-A3 = 'abc'.

ITAB-A2 = '30'.

APPEND ITAB.

ITAB-A1 = 'char2'.

ITAB-A3 = 'abc'.

ITAB-A2 = '10'.

APPEND ITAB.

ITAB-A1 = 'char2'.

ITAB-A3 = 'abc'.

ITAB-A2 = '20'.

APPEND ITAB.

SORT ITAB BY A1 a3.

loop at itab.

at new a1.

write:/ itab-a1.

clear i.

endat.

at new a3.

if i > 0.

write:/10 itab-a3.

else.

write:10 itab-a3.

endif.

clear count.

endat.

count = count + 1.

i = i + 1.

at end of a3.

write:20 count.

endat.

endloop.

o/p :

char1 abc 2

def 1

char2 abc 2

NOTE: don't forget to sort the table before using control breaks.

regards,

<b>*reward points for all helpful answers</b>

former_member588853
Active Contributor
0 Kudos

Hi,

<b>At first</b>. used inside loop endloop.

it triggers for the first time in the loop.

you can use it for heading

<b>At new</b> used inside loop endloop.

at new triggers for every change of the value of the field given .

. at new kunnr .. triggers when ever the value of kunnr is new..

It even triggers when values prior to the kunnr fields changes.

<b>At end of</b>

at end of kunnr : triggers at when all the values of kunnr are over loped.

use inside loop endloop.. used for totals

<b>sum</b>

it add all the numeric data types inside loop endloop.

<b>On change of</b> can be used in loop. endloop.

select endselet and other loops

if triggers when the valus alone for field given changes..

rewards if useful,

regards,

nazeer