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: 

DIFFERENCE BETWEEN "AT NEW " AND "AT FIRST" , "AT END OF" AND "AT LAST" ?

Former Member
0 Kudos

WHAT IS THE DIFFERENCE BETWEEN "AT NEW " AND "AT FIRST" , "AT END OF" AND "AT LAST" WITH REFERENCE TO CONTROL BREAK STATEMENTS ? PLEASE EXPLAIN IN DETAIL.

BEST REGARDS

RYAN.

5 REPLIES 5

Former Member
0 Kudos

Hi

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

Anji

Former Member
0 Kudos

Just go through the following documentation.

AT - Control breaks with extracts

Variants:

1. AT NEW f.

2. AT END OF f.

3. AT FIRST.

4. AT LAST.

5. AT fg.

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 is

executed whenever a control break occurs.

You can use these key words for control break processing with

extract datasets only if the active LOOP statement is

processing 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 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 Number Types),

the fields SUM(g) contain the relevant control totals.

Notes 1. The fields CNT(h) and SUM(g) can only be addressed after

they have been sorted. Otherwise, a runtime error may

occur.

2. 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).

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

4. You can also use special control break control structures

with LOOPs on internal tables.

Variant 1 AT NEW f.

Variant 2 AT END OF f.

Effect f is a field from the field group HEADER. The enclosed

sequence of statements is executed if

- the field f occurs in the sort key of the extract dataset

(and thus also in the field group HEADER) and

- the field f or a superior sort criterion has a different

value in the current LOOP line than in the preceding (AT

NEW) or subsequent (AT END OF) record of the extract

dataset.

If f is not an assigned field symbol, the control break

criterion is ignored, and the subsequent sequence of

statements is not executed. If a field symbol is assigned, but

does not point to the HEADER field group, the system triggers

a runtime error.

Example

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

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

Variant 3 AT FIRST.

Variant 4 AT LAST.

Effect Executes the relevant series of statements just once - either

on the first loop pass (with AT FIRST) or on the last loop

pass (with AT LAST).

Variant 5 AT fg.

Addition:

... WITH fg1

Effect This statement makes single record processing dependent on the

type of extracted record.

The sequence of statements following AT fg are executed

whenever the current LOOP record is created with EXTRACT fg

(in other words: when the current record is a fg record).

Addition ... WITH fg1

Effect Executes the sequence of statements belonging to AT fg WITH

fg1 only if the record of the field group fg in the dataset is

immediately followed by a record of the field group fg1.

Reward If Helpful

Former Member
0 Kudos

Hi

i am sending you a simple program in which i had write program on that events

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.

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

<b>Reward if usefull</b>

Former Member
0 Kudos

Hi,

<u><b>AT - itab</b></u>

<b>Syntax</b>

LOOP AT itab result ...

[AT FIRST.

...

ENDAT.]

[AT NEW comp1.

...

ENDAT.

[AT NEW comp2.

...

ENDAT.

[...]]]

[ ... ]

[[[...]

AT END OF comp2.

...

ENDAT.]

AT END OF comp1.

...

ENDAT.]

[AT LAST.

...

ENDAT.]

ENDLOOP.

<b>

Extras:</b>

1. ... FIRST

2. ... |{END OF} comp 3. ... LAST <b>Effect</b> The statement block of a LOOP loop can contain control structures for control level processing. The respective control statement is AT. The statements AT and ENDAT define statement blocks that are executed at control breaks. Within these statement blocks, the statement SUM can be specified to total numeric components of a group level. In the case of output behavior result, the same applies as for LOOP AT. So that group level processing can be executed correctly, the following rules should be noted: After LOOP there should be no limiting condition cond specified. The internal table must not be modified within the LOOP loop. The work area wa specified in the LOOP statement after the INTO addition must be compatible with the line type of the table. The content of a work area wa specified in the LOOP statement after the INTO addition must not be modified. The prerequisite for control level processing is that the internal table is sorted in exactly the same sequence as the component of its line type - that is, first in accordance with the first component, then in accordance with the second component, and so on. The line structure and the corresponding sorting sequence gives a group structure of the content of the internal table, whose levels can be evaluated using AT statements. The AT-ENDAT control structures must be aligned one after the other, in accordance with the group structure. The statement blocks within the AT-ENDAT control structures are listed if an appropriate control break is made in the current table line. Statements in the LOOP-ENDLOOP control structure that are not executed within an AT-ENDAT control structure are executed each time the loop is run through. If the INTO addition is used in the LOOP statement to assign the content of the current line to a work area wa, its content is changed upon entry into the AT-ENDAT control structure as follows: The components of the current group key will remain unchanged. All components with a character-type, flat data type to the right of the current group key are set to character "*" at that position. All the other components to the right of the current group key are set to their initial value. When the AT-ENDAT control structure is exited, the content of the current table line is assigned to the entire work area wa. <b>Note</b> If the INTO addition is used in the LOOP statement, a field symbol can be specified outside of the classes after AT |{END OF}. The appropriate component of the work area wa is assigned to this field symbol.

<b>Addition 1</b>

... FIRST

<b>Effect</b>

First line of the internal table.

<b>Addition 2</b>

... |{END OF} comp

<b>Effect</b>

Beginning or end of a group of lines with the same content in the component comp1 comp2 ... and in the components to the left of comp1 comp2 .... The components comp1 comp2 ... can be specified, as described in the section Specification of Components, with the limitation that access to object attributes is not possible here.

<b>Addition 3</b>

... LAST

<b>Effect</b>

The last line of the internal table.

Example

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>Non-Catchable Exceptions</b>

Cause: Invalid partial access with dynamic specification of the control break criterion.

Runtime Error: AT_BAD_PARTIAL_FIELD_ACCESS

Cause: If you have dynamic specification of the control break criterian through the field symbol, the fields symbol does not point to the LOOP output area.

Runtime Error: AT_ITAB_FIELD_INVALID

Cause: If you have dynamic specification of the control break criterion through the (name), the name field does not contain any valid sufield label.

Runtime Error: ITAB_ILLEGAL_COMPONENT

Cause: Overflow of totals creation with SUM.

Runtime Error: SUM_OVERFLOW

Regards,

Bhaskar