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

Control Break Statements

Former Member
0 Likes
2,699

What is the difference between:

At First

At Last

At End Of

Please explain with example.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,642

Hi Aishwarya.,

At First is used(generally) for putting Header .

it will trigger when the first loop pass of the internal table processing.

At Last is used for Footer Purpose

it will trigger when the Last loop pass of the internal table processing.

At End Of will trigger when the last value of the field encountered for the specified field in a group of similar fields in an internal table.

Reward points if useful

Chandra

9 REPLIES 9
Read only

Former Member
0 Likes
1,642

Hi

Hope it will help you.

Pls reward if help.

AT - Control break with internal tables

Variants

1. AT NEW f.

2. AT END OF f.

3. AT FIRST.

4. AT LAST.

Effect

In a LOOP which processes a dataset created with EXTRACT , you can use special control structures for control break processing. All these structures begin with AT and end with ENDAT . The sequence of statements which lies between them is then executed if a control break occurs.

You can use these key words for control break processing with extract datasets only if the active LOOP statement is proceesing an extract dataset.

The control level structure with extract datasets is dynamic. It corresponds exactly to the sort key of the extract dataset, i.e. to the order of fields in the field group HEADER by which the extract dataset was sorted .

At the start of a new control level (i.e. immediately after AT ), the following occurs in the output area of the current LOOP statement:

All default key fields (on the right) are filled with "*" after the current control level key.

All other fields (on the right) are set to their initial values after the current control level key.

Between AT and ENDAT , you can use SUM to insert the appropriate control totals in the number fields (see also ABAP/4 number types ) of the LOOP output area (on the right) after the current control level key. Summing is supported both at the beginning of a control level ( AT FIRST , AT NEW f ) and also the end of a control level ( AT END OF f , AT LAST ).

At the end of the control level processing (i.e. after ENDAT ), the old contents of the LOOP output area are restored.

Notes

When calculating totals, you must ensure that the totals are inserted into the same sub-fields of the LOOP output area as those where the single values otherwise occur. If there is an overflow, processing terminates with a runtime error.

If an internal table is processed only in a restricted form (using the additions FROM , TO and/or WHERE with the LOOP statement), you should not use the control structures for control level processing because the interaction of a restricted LOOP with the AT statement is currenly not properly defined.

With LOOP s on extracts, there are also special control break control structures you can use.

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.

Read only

Former Member
0 Likes
1,643

Hi Aishwarya.,

At First is used(generally) for putting Header .

it will trigger when the first loop pass of the internal table processing.

At Last is used for Footer Purpose

it will trigger when the Last loop pass of the internal table processing.

At End Of will trigger when the last value of the field encountered for the specified field in a group of similar fields in an internal table.

Reward points if useful

Chandra

Read only

Former Member
0 Likes
1,642

Hi

fetch the data into ITAB from the Database table

SORT ITAB BY VBEN POSNR

use the control break statements in the LOOP...ENDLOOP.

LOOP AT ITAB.

AT NEW VBELN.

write:/< fields you wants to display>

ENDAT.

AT END OF VBELN.

SUM.

<write the qty fields >

ENDAT.

ENDLOOP.

see the sample code for the usage of control break statements and do accordingly

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.

Reward points for useful Answers

Regards

chandu.

Read only

Former Member
0 Likes
1,642

Hi,

When you sort an extract dataset, control levels are defined in it. For general information about control levels, refer to Processing Internal Tables in Loops The control level hierarchy of an extract dataset corresponds to the sequence of the fields in the HEADER field group. After sorting, you can use the AT statement within a loop to program statement blocks that the system processes only at a control break, that is, when the control level changes.

AT NEW <f> | AT END OF <f>.

...

ENDAT.

A control break occurs 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). Field <f> must be part of the HEADER field group.

If the extract dataset is not sorted, the AT... ENDAT block is never executed. Furthermore, all extract records with the value HEX 00 in the field <f> are ignored when the control breaks are determined.

The AT... ENDAT blocks in a loop are processed in the order in which they occur. This sequence should be the same as the sort sequence. This sequence must not necessarily be the sequence of the fields in the HEADER field group, but can also be the one determined in the SORT statement.

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.

Read only

Former Member
0 Likes
1,642

hi,

control break statements are of 4 types:

AT FIRST——>this is used for system field heading in

ABAP program.

AT NEW———>this is used to display the fields.

AT END———>this is used for row-wise calculation i.e,

sub-total.

AT LAST——->this is used for calculation of grand total.

these are the control statements used.

plz reward if helpful..

Read only

Former Member
0 Likes
1,642

Hi

At First

this is triggered when the control is at first record.

AtLast

this is triggered atlast record .

at end of

this is triggered when the last record of a particular feild is reached.

for example at end of kunnr.

when the last record of kunnr is reached this is triggered.

PARAMETER p_x TYPE i OBLIGATORY. "no of records

----


  • STRUCTURE DECLARATION

----


TYPES : BEGIN OF st_knc1,

kunnr TYPE knc1-kunnr, "Customer Number

bukrs TYPE knc1-bukrs, "Company Code

um01u TYPE knc1-um01u , "Sales In Posting Period

END OF st_knc1.

----


  • INTERNAL TABLE DECLARATIONS

----


DATA : it_knc1 TYPE STANDARD TABLE OF st_knc1 ,

wa_knc1 TYPE st_knc1.

----


  • START-OF-SELECTION

----


START-OF-SELECTION.

SELECT kunnr bukrs um01u

FROM knc1

INTO TABLE it_knc1

UP TO p_x ROWS.

SORT it_knc1 BY kunnr.

LOOP AT it_knc1 INTO wa_knc1.

----


  • CONTROL BREAK STATEMENTS

----


AT FIRST.

WRITE:/'customer number'.

ENDAT.

AT NEW kunnr.

WRITE:/ 'NEW RECORD'.

ENDAT.

WRITE:/ wa_knc1-kunnr, 5(10) wa_knc1-bukrs, 20(10) wa_knc1-um01u CURRENCY 'INR' .

AT END OF kunnr.

WRITE:/'END OF NEW RECORD'.

SUM.

WRITE:/'SUB TOTAL' , wa_knc1-um01u CURRENCY 'INR'. "sub total

ENDAT.

AT LAST.

WRITE:/ 'LAST RECORD IS REACHED'.

SUM.

WRITE:/ 'GRAND TOTAL',wa_knc1-um01u CURRENCY 'INR'. "grand total

ENDAT.

ENDLOOP.

----


  • TOP-OF-PAGE

----


TOP-OF-PAGE.

WRITE:/40 text-000.

Edited by: Jyothsna M on Feb 18, 2008 6:52 AM

Read only

Former Member
0 Likes
1,642

Hi,

let ut itab be as below....

FLD1 FLD2 FLD3

1        a       b
1        c       e
2        f        h
2        g       l

now

1) if u write as below

loop at itab.
AT FIRST.
*this event ll be triggered only at de first loop pass....
ENDAT.
endloop.

2) if u write as below

loop at itab.

AT LAST.

*this event ll be triggered only at de last loop pass....here fourth loop pass.

ENDAT.

endloop.

2) if u write as below

loop at itab.

AT END OF FLD1.

*this event ll be triggered only after second loop pass....bcoz after second loop pass the value of FLD1 changes from 1 to 2.

ENDAT.

endloop.

Cheers,

jose.

Read only

Former Member
0 Likes
1,642

hi,

at first----


it is used to print coulmn headings at the begining of reports.

at new-------used to print any messages at the change of coulmn values

at end-------used to calculate sub totals

have a look at this programme u ill have better understanding

TYPES : BEGIN OF st_final,

saldoc TYPE vbap-vbeln, "SALES DOCUMENT.

material TYPE vbap-matnr, "MATERIAL.

quant TYPE vbap-netwr, "QUANTITY.

date TYPE vbak-audat, "DATE.

name TYPE vbak-bname, "NAME.

dtype TYPE vbak-auart, "DATE TYPE.

pgroup TYPE vbkd-konda, "PRIME GROUP.

cugroup TYPE vbkd-kdgrp, "CUSTOMER GROUP.

END OF st_final.

******************INTERNAL TABLE DECLARATION***************

DATA:it_vtab TYPE STANDARD TABLE OF st_final, "DECLARING INTERNAL TABLE.

wa_vtab LIKE LINE OF it_vtab. "DECLARING WORK AREA.

******************SELECT-OPTIONS***************

SELECT-OPTIONS : so_vb FOR vbap-vbeln. "SELECT-OPTIONS.

******************START OF SELECTION EVENT****************

START-OF-SELECTION. "FETCHING DATA FROM TABLES VBAP,VBAK,VBKD INTO INTERNAL TABLE.

SELECT p~vbeln

p~matnr

p~netwr

k~audat

k~bname

k~auart

d~konda

d~kdgrp

FROM ( ( vbap AS p

INNER JOIN vbak AS k ON pvbeln = kvbeln )

INNER JOIN vbkd AS d ON pvbeln = dvbeln )

INTO TABLE it_vtab

WHERE p~vbeln IN so_vb.

IF so_vb IS INITIAL.

MESSAGE text-003 TYPE 'E'.

ENDIF.

IF sy-subrc NE 0.

MESSAGE 'transaction failed' TYPE 'E'.

ELSE.

SORT it_vtab BY material. "SORTING INTERNAL TABLE.

  • ******************CONTROL-BREAK-STATEMENTS*******************

LOOP AT it_vtab INTO wa_vtab.

AT FIRST. "STARTING OF AT FIRST EVENT.

WRITE :/5 'salesdoc' COLOR 2,

40 'material' COLOR 2,

50 'quant' COLOR 2 CURRENCY 'INR',

60 'date' COLOR 2,

80 'name' COLOR 2,

90 'datetype' COLOR 2,

110 'primegroup' COLOR 2,

130 'customergroup' COLOR 2.

ENDAT.

AT NEW saldoc. "STARTING OF AT NEW EVENT.

WRITE : / text-000, wa_vtab-saldoc COLOR 4.

ENDAT.

WRITE : /5 wa_vtab-saldoc,

40 wa_vtab-material,

50 wa_vtab-quant CURRENCY 'INR',

60 wa_vtab-date,

80 wa_vtab-name,

90 wa_vtab-dtype,

110 wa_vtab-pgroup,

130 wa_vtab-cugroup.

AT END OF saldoc. "STARTING OF AT END EVENT.

SUM.

WRITE : / text-001, wa_vtab-quant CURRENCY 'INR' COLOR 3.

ENDAT.

SKIP 3.

AT LAST. "STARTING OF AT LAST EVENT.

SUM.

WRITE : / text-002, wa_vtab-quant CURRENCY 'INR' COLOR 5.

ENDAT.

ENDLOOP.

endif.

Read only

Former Member
0 Likes
1,642

hi aishwarya,

these r the events in internal table.

At First

At Last

At End Of

1.At First:

The control level is defined by the first line of the internal table. The control break takes place when this line is read.

ex: i want to print "hi see the datails" n after that the data of itab.

then i can call this event.

LOOP AT itab.

AT FIRST.

write: " hi see the datails".

ENDAT.

write: / itab-f1,itab-f2 ...........

ENDLOOP.

2.At Last:

The control level is defined by the last line of the internal table. The control break takes place when this line is read.

ex: i want to print "thats the end of details" after printing all the data of itab.

then i can call this event.

LOOP AT itab.

write: / itab-f1,itab-f2 ...........

AT LAST.

write: / "thats the end of details".

ENDAT.

ENDLOOP.

3.At End Of:

this event will get triggered after same kind of records.

let us take in itab: i have deptno, empno, empname.

data i have is in dept no: 1, i have 10 records, in deptno 2, ihave 10 records, same in dept no3 also.

then after printing 10 records od dept no:1, i want to print "thats the end of dept 1", same after dept2, & 3 also.

then for this kind of requirements, we use this event.

LOOP AT itab.

write: / itab-deptno,itab-empno, itab-empname.

AT END OF deptno.

write: / "thats the end of dept", itab-deptno.

ENDAT.

ENDLOOP.

note: to use this at end of the itab should be sorted based on the field which u want to call the event.

in the above example the itab should be sorted based on deptno.

hope this helps u.

reward if helps.