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 stmts

Former Member
0 Likes
1,181

Can anyone explain the concepts of Control break statements

Give me a small simple sample program..

Can anyone explain the concepts of Control break statements

Give me a small simple sample program..

5 REPLIES 5
Read only

Former Member
0 Likes
815

these statements are used inside loops.

these statements generate headings, subheadings, subtotals, grand totals etc.

some of control break statements r

LOOP AT <IT_TAB>

ONCHANGEOF

ENDON

AT FIRST<FLD_NME>

ENDAT

AT NEW <FLD_NME>

ENDAT

AT ENDOF<FLD_NME>

ENDAT

AT LAST

ENDAT

ENDLOOP

Read only

Former Member
0 Likes
815

HI,

control break statements are

at new field

at first

at end of field

at last

on changing of

for eg: purchase order details

ponum po item weight

1 10 10

1 20 30

1 30 20

2 10 20

2 20 25

3 30 35

if you want to print like

ponum po item weight

new Purchase order

1 10 10

1 20 30

1 30 20

new Purchase order

2 10 20

2 20 25

3 30 35

then

sort itab by po num

loop at itab.

at new po.

write:/ 'New Purchase order'.

endat.

write:/ itab-ponum, itab-poitem, itab-weight.

endloop.

regards,

Venkatesh

Read only

Former Member
0 Likes
815

Hi ,

Check in 21 days of ABAP book or in abap docu.

Control break statements are used to stop the control at a particular point.

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.

Note

Runtime errors

SUM_OVERFLOW : Overflow when calculating totals with SUM .

Variant 1

AT NEW f.

Variant 2

AT END OF f.

Effect

f is a sub-field of an internal table processed with LOOP . The sequence of statements which follow it is executed if the sub-field f or a sub-field in the current LOOP line defined (on the left) before f has a differnt value than in the preceding ( AT NEW ) or subsequent ( AT END OF ) table line.

Example

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.

The AT statements refer to the field COMPANIES-NAME .

Notes

If a control break criterion is not known until runtime, you can use AT NEW (name) or AT END OF (name) to specify it dynamically as the contents of the field name . If name is blank at runtime, the control break criterion is ignored and the sequence of statements is not executed. If name contains an invalid component name, a runtime error occurs.

By defining an offset and/or length, you can further restrict control break criteria - regardless of whether they are specified statically or dynamically.

A field symbol pointing to the LOOP output area can also be used as a dynamic control break criterion. If the field symbol does not point to the LOOP output area, a runtime error occurs.

Note

Runtime errors

AT_BAD_PARTIAL_FIELD_ACCESS : Invalid sub-field access when dynamically specifying the control break criterion.

AT_ITAB_FIELD_INVALID : When dynamically specifying the control break criterion via a field symbol, the field symbol does not point to the LOOP output area.

ITAB_ILLEGAL_COMPONENT : When dynamically specifying the control break criterion via (name) the field name does not contain a valid sub-field name.

Variant 3

AT FIRST.

Variant 4

AT LAST.

Effect

Executes the appropriate sequence of statements once during the first ( AT FIRST ) or last ( AT LAST ) loop pass.

Example

DATA: BEGIN OF COMPANIES OCCURS 20,

NAME(30),

PRODUCT(20),

SALES TYPE I,

END OF COMPANIES.

...

LOOP AT COMPANIES.

AT FIRST.

SUM.

WRITE: 'Sum of all SALES:',

55 COMPANIES-SALES.

ENDAT.

WRITE: / COMPANIES-NAME, COMPANIES-PRODUCT,

55 COMPANIES-SALES.

ENDLOOP.

ON CHANGE OF and AT NEW are same and we don't use On change of now.

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.

regards,

Prabhu

Reward if it is helpful

Read only

Former Member
0 Likes
815

Control break statements are used to do some operations (execute abap commands) inside the Loop when you are iterating internal table.

AT FIRST.

...

ENDAT.

AT LAST.

...

AT END.

ATNEW.

...

ENDAT.

AT ENDOF.

...

ENDAT

AT CHANGE OF.

ENDAT.

========================================================

Example:

REPORT ZTEST_VIV.

types: BEGIN OF IS_TEST,

v_int TYPE i,

v_str TYPE string,

END OF IS_TEST.

DATA: wa_itab TYPE IS_TEST,

itab type standard table of is_test.

START-OF-SELECTION.

wa_itab-v_int = '1'.

wa_itab-v_str = 'vivek'.

append wa_itab to itab.

LOOP at itab INTO wa_itab.

AT FIRST.

WRITE: 'FIRST RECORD'.

ENDAT.

ENDLOOP.

Please award points if it is useful....

Thanks,

Vivek LR

Read only

Former Member
0 Likes
815

Hi,

<u><b>Control level processing</b></u>

Control level processing is allowed within a LOOP over an internal table. This means that you can divide sequences of entries into groups based on the contents of certain fields.

Internal tables are divided into groups according to the sequence of the fields in the line structure. The first column defines the highest control level and so on. The control level hierarchy must be known when you create the internal table.

The control levels are formed by sorting the internal table in the sequence of its structure, that is, by the first field first, then by the second field, and so on. Tables in which the table key occurs at the start of the table are particularly suitable for control level processing.

Within the processing block of a loop, you can use the control level statement AT to react to a control level change. This enables you to restrict statements to a certain set of lines. You can thus use the SUM statement to calculate totals from subsets of all lines.

The AT statement introduces a statement block that you end with the ENDAT statement.

AT <level>.

<statement block>

ENDAT.

You can react to the following control level changes:

FIRST - First line of the internal table

LAST - Last line of the internal table

NEW <f> - Beginning of a group of lines with the same contents in the field <f> and in the fields left of <f>

END Of <f> - End of a group of lines with the same contents in the field <f> and in the fields left of <f>

You can use control level statements to react to control breaks in internal tables instead of programming them yourself with logical expressions. Within the loop, you must order the ATENDAT statement blocks according to the hierarchy of the control levels. If the internal table has the columns <f1>, <f2>, ...., and if it is sorted by these columns, you must program the loop as follows:

LOOP AT <itab>.

AT FIRST. ... ENDAT.

AT NEW <f1>. ...... ENDAT.

AT NEW <f2>. ...... ENDAT.

.......

<single line processing>

.......

AT END OF <f2>. ... ENDAT.

AT END OF <f1>. ... ENDAT.

AT LAST. .... ENDAT.

ENDLOOP.

The innermost hierarchy level <single line processing> processes the table lines that do not correspond to a control level change. You do not have to use all control level statements. But you must place the used ones in the above sequence. You should not use control level statements in loops where the line selection is restricted by WHERE or FROM and TO. Neither should the table be modified during the loop.

If a control level field <fi> is not known until runtime, you can specify it dynamically as (<ni>) where <ni> contains the field of <fi>. If <ni> is empty at runtime, the criterion for changing the control level is ignored. You can restrict the search to partial fields by specifying offset and length.

If you are working with a work area <wa>, it does not contain the current line in the AT... ENDAT statement block. All character fields to the right of the current group key are filled with asterisks (*). All other fields to the right of the current group key contain their initial value.

Within an AT...ENDAT block, you can calculate the contents of the numeric fields of the corresponding control level using the SUM statement.

SUM.

You can only use this statement within a LOOP. If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area (see example in ). If you use the SUM statement outside an AT - ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in

AT...ENDAT blocks.

If the table contains a nested table, you cannot use the SUM statement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.

Ex1:

DATA: BEGIN OF LINE,

COL1 TYPE C,

COL2 TYPE I,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE

WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.

DO 3 TIMES.

LINE-COL2 = SY-INDEX.

LINE-COL3 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 'B'.

DO 3 TIMES.

LINE-COL2 = 2 * SY-INDEX.

LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

AT END OF COL1.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

SKIP.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDAT.

ENDLOOP.

The program creates a hashed table ITAB, fills it with six lines, and sorts it. In the

LOOP - ENDLOOP block, the work area LINE is output for each loop pass. The first field of the table key, COL1, is used for control level processing. The total for all numeric fields is always calculated when the contents of COL1 change and when the system is in the last loop pass.

Ex2:

DATA: BEGIN OF LINE,

CARRID TYPE SBOOK-CARRID,

CONNID TYPE SBOOK-CONNID,

FLDATE TYPE SBOOK-FLDATE,

CUSTTYPE TYPE SBOOK-CUSTTYPE,

CLASS TYPE SBOOK-CLASS,

BOOKID TYPE SBOOK-BOOKID,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY TABLE

LINE.

SELECT CARRID CONNID FLDATE CUSTTYPE CLASS BOOKID

FROM SBOOK INTO CORRESPONDING FIELDS OF TABLE ITAB.

LOOP AT ITAB INTO LINE.

AT FIRST.

WRITE / 'List of Bookings'.

ULINE.

ENDAT.

AT NEW CARRID.

WRITE: / 'Carrid:', LINE-CARRID.

ENDAT.

AT NEW CONNID.

WRITE: / 'Connid:', LINE-CONNID.

ENDAT.

AT NEW FLDATE.

WRITE: / 'Fldate:', LINE-FLDATE.

ENDAT.

AT NEW CUSTTYPE.

WRITE: / 'Custtype:', LINE-CUSTTYPE.

ENDAT.

WRITE: / LINE-BOOKID, LINE-CLASS.

AT END OF CLASS.

ULINE.

ENDAT.

ENDLOOP.

In this example, the sorted internal table ITAB is filled with data from the database

table SBOOK using the Open SQL statement SELECT. The sequence of the

columns in the internal table defines the control level hierarchy. Since the table key is the entire line, the sort sequence and the control level hierarchy are the same. The sequence of the AT-ENDAT blocks within the LOOP and ENDLOOP statements is important.

Regards,

Bhaskar