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: 

AT / ENDAT

Former Member
0 Kudos

Hi,

I have an internal table with 5 columns

Field1 Field2

AAA BB ......

AAA CC

BBB CC

DDD CC

Now I am using

LOOP AT itab.

AT NEW Field2.

ADD 1 TO COUNTER.

ENDAT.

ENDLOOP.

Now consider first row, since BB is new

COUNTER = 1.

Then consider second record, since CC is new

COUNTER = COUNTER +1 = 2

Then consider third record, since CC is not new, I do not want 1 to be added to counter, however in DEBUG i noticed that since Field1 = AAA for second record <> Field1 = BBB for third record (Field CC is same in second and third record),

COUNTER = COUNTER +1 = 3.

But I do not want this to happen.

FOR the following internal table , counter should be 2, however in debug, I noticed that counter = 4, i want to change counter only on change of field 2, how can I do this, please note that my table is an internal table and is not a data set.

AAA BB ......

AAA CC

BBB CC

DDD CC

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi MN,

Do like this,

LOOP AT itab.

on change of Field2.

ADD 1 TO COUNTER.

endon.

ENDLOOP.

Kiran

12 REPLIES 12

Former Member
0 Kudos

Hi,

For perfect results place Field2 as Field1 in your internal table. and use At new Field1.

Thanks & Regards,

Navneeth K.

Former Member
0 Kudos

Hi ,

change ur internal table in such a way that u keep ur field2 first then field1.

like field2 field1.

BB AAA

CC AAA

CC BBB

CC DDD

now loop at itab.

at new field2..

...

endat.

now check the counter ...

Rgds.,

subash

Former Member
0 Kudos

Hi,

When u use AT NEW <field> it considers the fields before the <field>. So any change on the fields on the left hand side of <field> will trigger the AT NEW <field> statement.

So in ur case keep the Field2 at the beginning of the table.

This will solve ur problem...

Former Member
0 Kudos

Use "On Chnage Of" instead of AT NEW.

Regards,

Karan

Former Member
0 Kudos

hi,

as per ur case :

restructure ur table as FIELD2 FIELD1 and then sort it on the basis of FIELD2.and in the loop use AT NEW FIELD2.....now it will work.....

bpawanchand
Active Contributor
0 Kudos

Hi

I think you have declared the internal table structure as follows

DATA : 
    BEGIN OF fs_tab, 
        field1 TYPE <xyz>, 
        field2 TYPE <xyz>, 
    END OF fs_tab.

Instead of that you declare the internal table as follows

DATA : 
    BEGIN OF fs_tab, 
        field2 TYPE <xyz>, 
        field1 TYPE <xyz>, 
    END OF fs_tab.

and try your issue will be solved

Former Member
0 Kudos

Check below code:

DATA:
  BEGIN OF itab OCCURS 0,
    field1(2),
    field2(3),
  END OF itab.

DATA:
counter TYPE i.

itab-field2 = 'AAA'.
itab-field1 = 'BB'.
APPEND itab.
CLEAR itab.

itab-field2 = 'AAA'.
itab-field1 = 'CC'.
APPEND itab.
CLEAR itab.

itab-field2 = 'BBB'.
itab-field1 = 'CC'.
APPEND itab.
CLEAR itab.

itab-field2 = 'DDD'.
itab-field1 = 'CC'.
APPEND itab.
CLEAR itab.

LOOP AT itab.
  AT NEW field1.
    ADD 1 TO counter.
  ENDAT.
ENDLOOP.
BREAK-POINT.

Field2 and Field1 have been interchanged.

Former Member
0 Kudos

Hi M N

It is working Check this code

TYPES : BEGIN OF TY_SFLIGHT,
        CARRID TYPE SFLIGHT-CARRID,
        CONNID TYPE SFLIGHT-CONNID,
        END OF TY_SFLIGHT.

DATA :IT_SFLIGHT TYPE TABLE OF TY_SFLIGHT,
      WA_SFLIGHT TYPE TY_SFLIGHT,
      COUNT      TYPE I.

SELECT CARRID
       CONNID
FROM SFLIGHT
INTO TABLE IT_SFLIGHT.

SORT IT_SFLIGHT BY CARRID CONNID.

LOOP AT IT_SFLIGHT INTO WA_SFLIGHT.

  AT NEW CONNID.

    COUNT = COUNT + 1.

    WRITE : / WA_SFLIGHT-CARRID,
              WA_SFLIGHT-CONNID,
              COUNT.

  ENDAT.

ENDLOOP.

regards

Former Member
0 Kudos

Hi MN,

There might be some problem with your table declaration.

Check for the below given code.

The functionality is fine.

data: begin of itab1 occurs 0,

po type i,

item type i,

transaction_event type i,

document type i,

end of itab1.

data wa1 like itab1.

data COUNTER type i.

itab1-po = 100000.

itab1-item = 1.

itab1-transaction_event = 1.

itab1-document = 560000001.

append itab1.

itab1-po = 100000.

itab1-item = 1.

itab1-transaction_event = 1.

itab1-document = 560000002.

append itab1.

itab1-po = 100000.

itab1-item = 1.

itab1-transaction_event = 2.

itab1-document = 620000004.

append itab1.

itab1-po = 100000.

itab1-item = 1.

itab1-transaction_event = 2.

itab1-document = 620000006.

append itab1.

itab1-po = 100000.

itab1-item = 1.

itab1-transaction_event = 2.

itab1-document = 620000007.

append itab1.

loop at itab1.

write:/ itab1-po,

itab1-item,

itab1-transaction_event,

itab1-document.

endloop.

LOOP AT itab1.

AT NEW transaction_event.

ADD 1 TO COUNTER.

ENDAT.

ENDLOOP.

write:/ 'Counter'.

write:/ Counter.

Here I am getting the Counter as 2 only.

Check with the declaration part.

Regards,

Amit.

Former Member
0 Kudos

Hi MN,

Do like this,

LOOP AT itab.

on change of Field2.

ADD 1 TO COUNTER.

endon.

ENDLOOP.

Kiran

0 Kudos

>

> Hi MN,

> Do like this,

>

> LOOP AT itab.

> on change of Field2.

> ADD 1 TO COUNTER.

> endon.

> ENDLOOP.

>

> Kiran

ON CHANGE is obsolete statement. try to avoid that.

Former Member
0 Kudos

Hi

Try this..

LOOP AT itab.

ON CHANGE OF itab-f2.

count = count + 1.

ENDON.

ENDLOOP.