‎2006 Jun 21 12:16 PM
hi All,
Can anyone tell me the use of At new , At First, At Last, At END of. Also tell the differnce between At new and At first with example.
Points will be rewarded.
Regards
Harpreet
‎2006 Jun 21 12:24 PM
Hi,
<b>You could have found lot of threads on this topic... if you had searched the form before posting this question.</b>
<b>Anyway... check the following...</b>
Control level processing
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.
The following diagram illustrates control level processing in a sorted table, where different field contents in the first three fields are indicated by different colors:
Each change of color in a column indicates a control level change in the corresponding hierarchy level. 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 SUMstatement 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:
level
Meaning
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 AT-ENDATstatement blocks according to the hierarchy of the control levels. If the internal table itab 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 record 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 ones that you use 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 f1, f2,.... is not known until runtime, you can specify it dynamically as (n1), (n2), where n1, n2,.... contains the field of f1, f2,.... . If n1, n2,.... is empty at runtime, the criterion for changing the control level is ignored. You can further restrict the group level fields f1, f2,.... 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-ENDATstatement 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 SUMstatement.
.
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 SUMstatement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.
REPORT demo_int_tables_at_1.
DATA: BEGIN OF line,
col1(1) 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 list output is:
A 1 1
A 2 4
A 3 9
________________________________
A 6 14
B 2 4
B 4 16
B 6 36
________________________________
B 12 56
________________________________
18 70
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.
REPORT demo_int_tables_at_2.
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 - ENDLOOP statements is important.
The output looks something like this:
List of Bookings
Carrid: AA
Connid: 0017
Fldate: 1998/11/22
Custtype: B
00063509 C
00063517 C
...
______________________________________________
00063532 F
00063535 F
...
______________________________________________
Custtype: P
00063653 C
00063654 C
...
______________________________________________
00063668 F
00063670 F
...
______________________________________________
Fldate: 1998/29/11
Custtype: B
00064120 C
00064121 C
...
and so on.
Refer: http://help.sap.com/saphelp_erp2005/helpdata/en/fc/eb3000358411d1829f0000e829fbfe/frameset.htm
Thanks and Regards,
Bharat Kumar Reddy.V
Message was edited by: Bharat Kumar Reddy Vaka
‎2006 Jun 21 12:24 PM
‎2006 Jun 21 12:25 PM
check the following Documents
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 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 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/4 number types ), the fields SUM(g) contain the relevant control totals.
Notes
The fields CNT(h) and SUM(g) can only be addressed after they have been sorted. Otherwise, a runtime error may occur.
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 ).
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.
You can also use special control break control structures with LOOP s 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 prceding ( AT NEW ) or subsequent ( AT END OF ) record of the extract dataset.
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
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.
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 .
Basic form
ON CHANGE OF f.
Addition
... OR f1
Effect
Executes the processing block enclosed by the " ON CHANGE OF f " and " ENDON " statements whenever the contents of the field f change (control break processing).
Normally, you use the statement to manipulate database fields during GET events or SELECT / ENDSELECT processing.
Note
There are special control structures for processing control breaks in LOOP s on internal tables or extract datasets AT ).
ON CHANGE OF is unsuitable for recognizing control levels in loops of this type because it always creates a global auxiliary field which is used to check for changes. This global auxiliary field can only be changed in the relevant ON CHANGE OF statement. It is not reset when the processing goes into loops or subroutines, so unwanted effects can occur if the loop or subroutine is executed again. Also, since it is set to its initial value when created (like any other field), any ON CHANGE OF processing will be executed after the first test, unless the contents of the field concerned happen to be identical to the initial value.
Example
TABLES T100.
SELECT * FROM T100 WHERE SPRSL = SY-LANGU AND
MSGNR < '010'
ORDER BY PRIMARY KEY.
ON CHANGE OF T100-ARBGB.
ULINE.
WRITE: / '**', T100-ARBGB, '**'.
ENDON.
WRITE: / T100-MSGNR, T100-TEXT.
ENDSELECT.
Displays all messages with their numbers in the logon language, provided the number is less than '010'.
Each time the message class changes, it is output.
Addition
... OR f1
Effect
Also executes the code whenever the contents of the field f1 changes.
You can use this addition several times.
Example
Logical database F1S
TABLES: SPFLI, SFLIGHT, SBOOK.
GET SBOOK.
ON CHANGE OF SPFLI-CARRID OR
SPFLI-CONNID OR
SFLIGHT-FLDATE.
ULINE.
WRITE: /5 SPFLI-CARRID, SPFLI-CONNID,
5 SFLIGHT-FLDATE, SPFLI-FLTIME,
5 SFLIGHT-SEATSMAX, SFLIGHT-SEATSOCC.
ENDON.
WRITE: / SBOOK-CUSTOMID.
The code between ON CHANGE OF and ENDON is executed only if at least one of the fields SPFLI-CARRID , SPFLI-CONNID or SFLIGHT-FLDATE has changed, i.e. there is a different flight connection (which also has bookings).
You can search for "Differnce between AT NEW AND ON CHANGE OF " for further reference
‎2006 Jun 21 12:25 PM
When you are looping at a sorted table, there events can be used at specific places.
AT FIRST / AT LAST will be fired at the starting and ending of the loop.
AT NEW / AT END fire on specific columns. When you have column which changes the values, whenever there is a change in the value, the event fires. Similary when the value end AT END fires.
I guess that answers the difference between AT FIRST and AT NEW as well.
Regards,
Ravi
‎2006 Jun 21 12:28 PM
Hi Harpreet,
AT NEW, AT FIRST, AT LAST and AT END OF are control commands.
Before performing any operations on control command you need to SORT the internal table.
AT NEW will trigger for every new value of that field changes, where as AT FIRST will trigger for the first time on that field and later it wont trigger even the value changes of that field.
AT LAST will trigger for last value of that field.
Generally you specify SUM over here to get totals and subtotals.
AT END OF will trigger at the end of that field changes.
<b>For more about control commands,just follow the link
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/at_extra.htm</b>;
Thanks,
Vinay
‎2006 Jun 21 12:48 PM
Hi,
At first-> Will be fired only once at start of loop.
At last -> Last record this event is fired used
for printing grand total.
At new -> At new empno this event is fired
At end -> At end of empno event is fired. Used
to print subtotal.
foe example
Empno sal
1 2000
1 3000
2 7000
sort itab by empno
loop at itab.
At first.
Write: 'Report'.
endat.
at new empno.
write:'Empno,itab-empno .
endat.
at end of empno.
sum.
write:/'Empno Total:',itab-sal.
//Output we get values
1 5000
2 7000
endat.
at last.
sum.
write:/ 'Grand total:',itab-sal.
//Output here
grand total:12000
endat.
endloop.
Regards,
Amole
‎2006 Jun 21 1:21 PM
Hai Harpreet Singh
go through the following Link
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/at_extra.htm
AT - Control break 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 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 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/4 number types ), the fields SUM(g) contain the relevant control totals.
Notes
The fields CNT(h) and SUM(g) can only be addressed after they have been sorted. Otherwise, a runtime error may occur.
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 ).
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.
You can also use special control break control structures with LOOP s 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 prceding ( AT NEW ) or subsequent ( AT END OF ) record of the extract dataset.
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
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.
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 .
Thanks & regards
Sreenivasulu P