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

Collect statement

Former Member
0 Likes
1,164

Hi,

I have 2 internal tables, the first table which has quantity, week(WKYYYY format) matnr werks.

2nd internal table also has the same structure , now i want a flag in 2nd internal table when collect statement happens.

Eg. 1223 abc 10 112010

1223 abc 20 122010

1224 xyz 10 122011

now my 2nd internal table should have a flag like this

1223 abc 30 122010 X

1224 xyz 10 122010 this should not have X set

like this...

Loop at internal table 1.

collect into internal table 2.

endloop.

1 ACCEPTED SOLUTION
Read only

roberto_vacca2
Active Contributor
0 Likes
1,122

Hi...

That's something that could be useful for you....

DATA: BEGIN OF tab1 OCCURS 0,

key(4) TYPE c,

matnr TYPE mara-matnr,

qty TYPE i,

END OF tab1,

BEGIN OF tab2 OCCURS 0,

key(4) TYPE c,

matnr TYPE mara-matnr,

qty TYPE i,

flag TYPE c,

END OF tab2.

REFRESH: tab1, tab2.

tab1-key = '1223'.

tab1-matnr = 'TEST1'.

tab1-qty = 10.

APPEND tab1.

CLEAR tab1.

tab1-key = '1223'.

tab1-matnr = 'TEST1'.

tab1-qty = 20.

APPEND tab1.

CLEAR tab1.

tab1-key = '1224'.

tab1-matnr = 'TEST2'.

tab1-qty = 15.

APPEND tab1.

CLEAR tab1.

tab1-key = '1225'.

tab1-matnr = 'TEST3'.

tab1-qty = 34.

APPEND tab1.

CLEAR tab1.

tab1-key = '1225'.

tab1-matnr = 'TEST3'.

tab1-qty = 28.

APPEND tab1.

CLEAR tab1.

tab1-key = '1226'.

tab1-matnr = 'TEST4'.

tab1-qty = 2.

APPEND tab1.

DATA: currkey(4) TYPE c.

LOOP AT tab1.

CLEAR tab2.

READ TABLE tab2 WITH KEY currkey.

IF sy-subrc EQ 0 AND

tab1-key EQ currkey.

tab2-flag = 'X'.

MODIFY tab2 FROM tab2 INDEX sy-tabix.

ENDIF.

MOVE-CORRESPONDING tab1 TO tab2.

IF tab1-key NE currkey.

currkey = tab1-key.

CLEAR tab2-flag.

APPEND tab2.

ELSE.

COLLECT tab2.

ENDIF.

ENDLOOP.

9 REPLIES 9
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,122

Collect statement will not work for this scenario. You have to find some other logic like reading, adding and modifying.

Read only

Former Member
0 Likes
1,122

Hi

This cannot be done Simultaneously using Collect statement since the 2 table Structures are different and collect will not work.

Read only

roberto_vacca2
Active Contributor
0 Likes
1,123

Hi...

That's something that could be useful for you....

DATA: BEGIN OF tab1 OCCURS 0,

key(4) TYPE c,

matnr TYPE mara-matnr,

qty TYPE i,

END OF tab1,

BEGIN OF tab2 OCCURS 0,

key(4) TYPE c,

matnr TYPE mara-matnr,

qty TYPE i,

flag TYPE c,

END OF tab2.

REFRESH: tab1, tab2.

tab1-key = '1223'.

tab1-matnr = 'TEST1'.

tab1-qty = 10.

APPEND tab1.

CLEAR tab1.

tab1-key = '1223'.

tab1-matnr = 'TEST1'.

tab1-qty = 20.

APPEND tab1.

CLEAR tab1.

tab1-key = '1224'.

tab1-matnr = 'TEST2'.

tab1-qty = 15.

APPEND tab1.

CLEAR tab1.

tab1-key = '1225'.

tab1-matnr = 'TEST3'.

tab1-qty = 34.

APPEND tab1.

CLEAR tab1.

tab1-key = '1225'.

tab1-matnr = 'TEST3'.

tab1-qty = 28.

APPEND tab1.

CLEAR tab1.

tab1-key = '1226'.

tab1-matnr = 'TEST4'.

tab1-qty = 2.

APPEND tab1.

DATA: currkey(4) TYPE c.

LOOP AT tab1.

CLEAR tab2.

READ TABLE tab2 WITH KEY currkey.

IF sy-subrc EQ 0 AND

tab1-key EQ currkey.

tab2-flag = 'X'.

MODIFY tab2 FROM tab2 INDEX sy-tabix.

ENDIF.

MOVE-CORRESPONDING tab1 TO tab2.

IF tab1-key NE currkey.

currkey = tab1-key.

CLEAR tab2-flag.

APPEND tab2.

ELSE.

COLLECT tab2.

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
1,122

Hi,

Try this code


data : begin of it1,
         fld1,
         fld2(10),
         fld3(10),
         fld4 type i,
       end of it1.

data : begin of it2,
         fld1,
         fld2(10),
         fld3(10),
         fld4 type i,
         fld5,
       end of it1.
data : wa2 like it2.       
sort it1 by fld1 fld2 fld3.       
loop at it1.
  wa2 = it1.
  count = count + 1.
  at end of fld3.
    sum.
    wa2-fld4 = it1-fld4.
    if count gt 1.
      wa2-fld5 = 'X'.
    endif.
    clear count.
    append wa2 to it2.
    clear wa2.
  endat.
endloop.       

Thanks,

Anmol Bhat.

Edited by: anmol112 on Nov 16, 2010 3:48 PM

Read only

Former Member
0 Likes
1,122

Firstly, the first two rows will not be collected into one row because 4th field values are different.

The solution to your question is, instead of adding a flag column to the second itab, add a number column and set 1 for that column in the loop. After all the collection is complete, you should interpret any number greater than 1 as 'X'.

Read only

0 Likes
1,122

Very elegant.

Rob

Read only

Former Member
0 Likes
1,122

Try this:

DESCRIBE TABLE a LINES index.

COLLECT wa2 INTO itab2.

DESCRIBE TABLE a LINES index1.

IF index = index1.

collect = 'X'.

ELSEIF index LT index1 AND collect = 'X'.

CLEAR collect.

wa2-flag = 'X'.

MODIFY itab2 FROM wa2 INDEX index TRANSPORTING flag.

CLEAR wa2-flag.

ENDIF.

Read only

chandra_sekhar3
Explorer
0 Likes
1,122

Hi,

<< Moderator message - Cut and paste response from various sources removed. Plagiarism is not allowed in SCN >>

Edited by: chandrasek on Nov 19, 2010 4:20 PM

Edited by: Rob Burbank on Nov 19, 2010 10:41 AM

Read only

0 Likes
1,122

>

> Hi,

>

> COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or

>

> compression are unimportant, or two values with identical default key field values could not possibly occur in your particular

>

> task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the

>

> statement to use.

>

> Edited by: chandrasek on Nov 19, 2010 4:20 PM

ow... naughty naughty... i'm pretty sure you didn't write that piece yourself.