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

Hi Guru's,

I am new to ABAP.Please can anybody tell me correct difference between AT FIRST,AT LAST,AT NEW F(FIELD),AT THE END OF F(FIELD).

And what is significance of using SUM statement inside the AT AND ENDAT statement.

Thanks

priya.

1 ACCEPTED SOLUTION

former_member624107
Contributor
0 Kudos

Hi,,plese go thru this HELP.SAP.com details

In a LOOP which processes an internal table, 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 internal tables only if a loop is actively processing an internal table and reference is to the innermost currently active loop.

The control level structure with internal tables is static. It corresponds exactly to the sequence of columns in the internal table (from left to right). In this context, the criteria according to which you sort the internal table are unimportant.

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 character type 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 numeric fields (see also ABAP 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 LOOPs on extracts, there are also special control break control structures you can use.

Note

Non-Catchable Exceptions:

SUM_OVERFLOW: Overflow when calculating totals with SUM.

Variant 1

AT NEW f.

Variant 2

AT END OF f.

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas.See Compatible Work Area with Control Level Processing and Field Symbols Not Allowed as Control Level Criterion.

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 fhas a different value than in the preceding (AT NEW) or subsequent (AT END OF) table line.

Example

TYPES: BEGIN OF COMPANIES_TYPE,

NAME(30),

PRODUCT(20),

SALES TYPE I,

END OF COMPANIES_TYPE.

DATA: COMPANIES TYPE STANDARD TABLE OF COMPANIES_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 20,

WA_COMPANIES TYPE COMPANIES_TYPE.

...

LOOP AT COMPANIES INTO WA_COMPANIES.

AT NEW NAME.

NEW-PAGE.

WRITE / WA_COMPANIES-NAME.

ENDAT.

WRITE: / WA_COMPANIES-PRODUCT, WA_COMPANIES-SALES.

AT END OF NAME.

SUM.

WRITE: / WA_COMPANIES-NAME, WA_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.

If you use AT within a LOOP with an explicitly-specified output area, the area must be compatible with the line type of the internal table so that it can be initialized properly (as described above) at the start of a new control level.

You can restrict control break criteria further, regardless of whether they were defined statically or dynamically, by specifying offset and/or length.

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

TYPES: BEGIN OF COMPANIES_TYPE,

NAME(30),

PRODUCT(20),

SALES TYPE I,

END OF COMPANIES_TYPE.

DATA: COMPANIES TYPE STANDARD TABLE OF COMPANIES_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 20,

WA_COMPANIES TYPE COMPANIES_TYPE.

...

LOOP AT COMPANIES INTO WA_COMPANIES.

AT FIRST.

SUM.

WRITE: 'Sum of all SALES:',

55 WA_COMPANIES-SALES.

ENDAT.

WRITE: / WA_COMPANIES-NAME, WA_COMPANIES-PRODUCT,

55 WA_COMPANIES-SALES.

ENDLOOP.

Non-Catchable Exceptions

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

Runtime Error: AT_BAD_PARTIAL_FIELD_ACCESS

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

Runtime Error: AT_ITAB_FIELD_INVALID

Cause: When dynamically specifying the control break criterion using (name, the field name does not contain a valid subfield name.

Runtime Error: ITAB_ILLEGAL_COMPONENT

Cause: Overflow when totalling with SUM.

Runtime Error: SUM_OVERFLOW

7 REPLIES 7

Former Member
0 Kudos

Hi

see this program you can understand very easily

  • 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.

former_member624107
Contributor
0 Kudos

Hi,,plese go thru this HELP.SAP.com details

In a LOOP which processes an internal table, 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 internal tables only if a loop is actively processing an internal table and reference is to the innermost currently active loop.

The control level structure with internal tables is static. It corresponds exactly to the sequence of columns in the internal table (from left to right). In this context, the criteria according to which you sort the internal table are unimportant.

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 character type 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 numeric fields (see also ABAP 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 LOOPs on extracts, there are also special control break control structures you can use.

Note

Non-Catchable Exceptions:

SUM_OVERFLOW: Overflow when calculating totals with SUM.

Variant 1

AT NEW f.

Variant 2

AT END OF f.

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas.See Compatible Work Area with Control Level Processing and Field Symbols Not Allowed as Control Level Criterion.

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 fhas a different value than in the preceding (AT NEW) or subsequent (AT END OF) table line.

Example

TYPES: BEGIN OF COMPANIES_TYPE,

NAME(30),

PRODUCT(20),

SALES TYPE I,

END OF COMPANIES_TYPE.

DATA: COMPANIES TYPE STANDARD TABLE OF COMPANIES_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 20,

WA_COMPANIES TYPE COMPANIES_TYPE.

...

LOOP AT COMPANIES INTO WA_COMPANIES.

AT NEW NAME.

NEW-PAGE.

WRITE / WA_COMPANIES-NAME.

ENDAT.

WRITE: / WA_COMPANIES-PRODUCT, WA_COMPANIES-SALES.

AT END OF NAME.

SUM.

WRITE: / WA_COMPANIES-NAME, WA_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.

If you use AT within a LOOP with an explicitly-specified output area, the area must be compatible with the line type of the internal table so that it can be initialized properly (as described above) at the start of a new control level.

You can restrict control break criteria further, regardless of whether they were defined statically or dynamically, by specifying offset and/or length.

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

TYPES: BEGIN OF COMPANIES_TYPE,

NAME(30),

PRODUCT(20),

SALES TYPE I,

END OF COMPANIES_TYPE.

DATA: COMPANIES TYPE STANDARD TABLE OF COMPANIES_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 20,

WA_COMPANIES TYPE COMPANIES_TYPE.

...

LOOP AT COMPANIES INTO WA_COMPANIES.

AT FIRST.

SUM.

WRITE: 'Sum of all SALES:',

55 WA_COMPANIES-SALES.

ENDAT.

WRITE: / WA_COMPANIES-NAME, WA_COMPANIES-PRODUCT,

55 WA_COMPANIES-SALES.

ENDLOOP.

Non-Catchable Exceptions

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

Runtime Error: AT_BAD_PARTIAL_FIELD_ACCESS

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

Runtime Error: AT_ITAB_FIELD_INVALID

Cause: When dynamically specifying the control break criterion using (name, the field name does not contain a valid subfield name.

Runtime Error: ITAB_ILLEGAL_COMPONENT

Cause: Overflow when totalling with SUM.

Runtime Error: SUM_OVERFLOW

0 Kudos

Hi sheeba,

This i already read it but this SUM statement is not working as per above F1 what ur sayng in below program can u please see it once?

TYPES: BEGIN OF companies_type,

name(30),

product(20),

sales TYPE i,

SALES1 TYPE I,

END OF companies_type.

DATA: companies TYPE STANDARD TABLE OF companies_type WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 20,

wa_companies TYPE companies_type.

wa_companies-name = 'XYZ'.

wa_companies-product = 'SAP'.

wa_companies-sales = '1'.

wa_companies-sales1 = '1'.

APPEND wa_companies TO companies.

wa_companies-name = 'ABC'.

wa_companies-product = 'ABAP'.

wa_companies-sales = '4'.

wa_companies-sales1 = '4'.

APPEND wa_companies TO companies.

wa_companies-name = 'PQR'.

wa_companies-product = 'HR'.

wa_companies-sales = '3'.

wa_companies-sales1 = '3'.

APPEND wa_companies TO companies.

LOOP AT companies INTO wa_companies.

AT NEW name.

sum.

NEW-PAGE.

WRITE / wa_companies-name.

ENDAT.

WRITE: / wa_companies-product, wa_companies-sales,wa_companies-sales1.

AT END OF NAME.

SUM.

WRITE: / wa_companies-name, wa_companies-sales,wa_companies-sales1 .

ENDAT.

ENDLOOP.

0 Kudos

Hi..

SUM calculates the control totals of all fields of type I, F and P and places them in the LOOP output area( that is inetrnal table header or explict work area mentioned...)..

So in ur program it will sum the sales fileds at evry new company name..

but u hav only 3 diff company names.. so der s nothing to sum.. it wil output wt ever s der in the internal table.

0 Kudos

HI Sheeba,

whatever u told it hold good for AT NEW name but there is another SUM statement is there in AT END OF NAME.

so it shd calculate total of sales and sales1 for every name and it shd display the output.

Thanks & Regards

Priyalatha

former_member185054
Active Participant
0 Kudos

hi priya,

Control break statements are like events inside the loop. There are 5 control break statements in ABAP. These are used within loop.(Except ON CHANGE OF which can be used outside the loop as well)


  • AT FIRST - ENDAT
  • AT NEW - ENDAT
  • AT END OF - ENDAT
  • AT LAST - ENDAT
  • ON CHANGE OF


Explanation:


AT FIRST : Will trigger at the first run of the loop.


AT LAST: Will trigger at the last run of the loop.


The below 3 events are normally used when the table is sorted.

AT END OF : When we use At end for a field, it will trigger whenever there is any change in any of the fields from the left to that of the particular field. The trigger point will be the at the last occurrence  of the same value for the field.


AT NEW: When we use At new for a field, it will trigger whenever there is any change in any of the fields from the left to that of the particular field.The trigger point will be the at the first occurrence of the new value for the field.


ON CHANGE OF: On change of it triggers only when there is any change in the particular field.

On change of can be used outside the loop too


Example Program:


Here is an example program which gives you the practical understanding of all control break statements.

Example Program for Control Break Statements
Example Program for Control Break Statements
NOTE: It is important to note that we need a temporary work-area apart from the work-area used in loop for the last 3 control break statements. If we directly use the work-area of the loop, then we will get **** value and not the output we are expecting.



*&---------------------------------------------------------------------*

*& Report  ZAU_CONTROLBREAK

*&

*&---------------------------------------------------------------------*

*& NEW TO SAP CONTROL BREAK EXAMPLE

*& http://www.newtosap.info

*&

*&---------------------------------------------------------------------*

REPORT  zau_controlbreak.

TYPES: BEGIN OF ty_marc,

        matnr TYPE marc-matnr,

        werks TYPE marc-werks,

       END OF ty_marc.

DATA: it_marc TYPE STANDARD TABLE OF ty_marc,

      wa_marc TYPE ty_marc,

      wa_temp TYPE ty_marc.

SELECT matnr werks FROM marc INTO TABLE it_marc UP TO 10 ROWS WHERE matnr ge 40.

SORT it_marc BY matnr.

FIELD-SYMBOLS : <matnr> type matnr.


WRITE😕 'FULL TABLE'.

LOOP AT it_marc INTO wa_marc.

  wa_temp = wa_marc.

  WRITE: / sy-tabix , wa_temp-matnr,  wa_temp-werks.

ENDLOOP.

WRITE😕 'AT FIRST AND LAST'.

LOOP AT it_marc INTO wa_marc.

  wa_temp = wa_marc.

  AT FIRST.

    WRITE😕 'First Entry'.

    WRITE😕  wa_temp-matnr,  wa_temp-werks.

  ENDAT.

  AT LAST.

    WRITE😕 'Last Entry'.

    WRITE😕  wa_temp-matnr,  wa_temp-werks.

  ENDAT.

ENDLOOP.

WRITE😕 'AT END OF'.

LOOP AT it_marc INTO wa_marc.

  wa_temp = wa_marc.

  AT END OF matnr.

    WRITE: / sy-tabix , wa_temp-matnr,  wa_temp-werks.

  ENDAT.

ENDLOOP.


WRITE😕 'AT NEW'.

LOOP AT it_marc INTO wa_marc.

  wa_temp = wa_marc.

  AT NEW matnr.

    WRITE: / sy-tabix , wa_temp-matnr,  wa_temp-werks.

  ENDAT.

ENDLOOP.

WRITE😕 'ON CHANGE OF'.

LOOP AT it_marc INTO wa_marc.

  wa_temp = wa_marc.

  ASSIGN wa_marc-matnr TO <matnr>.

  ON CHANGE OF wa_marc-matnr.

    WRITE: / sy-tabix ,wa_temp-matnr,  wa_temp-werks.

  ENDON.

ENDLOOP.

0 Kudos

Hi,

Control Break Statements are used to control the sequential execution of the statements inside loop and end loop. This can be achieved by using the break statements and it is applicable only inside the loop and End loop.

Control Break Statements are,

1. At First:

This will trigger only during the first iteration of the loop

2. At Last:

This will trigger only during the last iteration of the loop.

3. At New:

This will trigger at every new value of the specified field.

4. At End Of:

This will trigger at the end of new value on the specified field.

5. On Change of:

This is obsolete in ABAP, it same as AT new.