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,498

Hello All,

I have a internal table with material number,material size and cartonwgt fields.

For the combination of mat.no and size there are different carton weights.So I want to add all the carton weights where mat no and size is same.I thought of using collect statement.Pls can ny one explain how to use that or is there any other solution for this?

Thanks,

Kumar.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,404

Hello Kumarn290,

Collect statement is used to store records in internal table while summing up their numeric fields. So it will serve your requirement. Simply read each record in an work area and then use "COLLECT WA INTO ITAB" so that you will get expected result.

Thanks & Regards,

Vaibhav Pendse

14 REPLIES 14
Read only

Former Member
0 Likes
1,404

Hi,

Use this example.

if you have data in internal table itab.

then declare itab1 same as itab.

and then.

Loop at itab.

collect itab into itab1.

Endloop.

Regards,

Vijay

Read only

Former Member
0 Likes
1,404

Hi,

Yes you can use collect statement. Just loop at the table and use collect. Make sure that the fields which you sum up are numeric and others are type c.


loop at itab.
collect itab.
endloop.

Read only

sreeramkumar_madisetty
Active Contributor
0 Likes
1,404

Hi

DATA : BEGIN OF ITAB OCCURS 0,

COL1(3) TYPE C,

COL2 TYPE I,

END OF ITAB.

ITAB-COL1 = 'ABC'.

ITAB-COL2 = 50.

COLLECT ITAB.

ITAB-COL1 = 'ABC'.

ITAB-COL2 = 100.

COLLECT ITAB.

ITAB-COL1 = 'ABC'.

ITAB-COL2 = 150.

COLLECT ITAB.

ITAB-COL1 = 'PQR'.

ITAB-COL2 = 100.

COLLECT ITAB.

ITAB-COL1 = 'PQR'.

ITAB-COL2 = 100.

COLLECT ITAB.

ITAB-COL1 = 'XYZ'.

ITAB-COL2 = 200.

COLLECT ITAB.

LOOP AT ITAB.

WRITE : / ITAB-COL1, ITAB-COL2.

ENDLOOP.

SORT ITAB.

LOOP AT ITAB.

AT FIRST.

WRITE: / 'AT NEW STATEMENT'.

ULINE.

ENDAT.

AT NEW COL1.

WRITE: / ' AT FIRST OF :', ITAB-COL1.

ENDAT.

WRITE : / ITAB-COL1, ITAB-COL2.

AT LAST.

ULINE.

ENDAT.

ENDLOOP.

Regards,

Sreeram

Read only

Former Member
0 Likes
1,404

hi kumar,

welcome to SDN.

when a table itab2 is of type

|matnr__ |size__ |weight

chartype|chartype|numeric type

loop at itab into is.
  is2- matnr = is-matnr.
  is2-size = is-size.
  is2-weight = is-weight..
  collect is2 into itab2.
endloop.

collect keeps the char fields intact and adds the numeric type. please take a note of this

Edited by: Soumyaprakash Mishra on Sep 18, 2009 10:35 AM

Read only

Former Member
0 Likes
1,405

Hello Kumarn290,

Collect statement is used to store records in internal table while summing up their numeric fields. So it will serve your requirement. Simply read each record in an work area and then use "COLLECT WA INTO ITAB" so that you will get expected result.

Thanks & Regards,

Vaibhav Pendse

Read only

0 Likes
1,404

Hello All,

Thanks a tonn for all your replies My carton weight field is of type QUAN.

Actually I need to frame a formula i.e..,

Per unit net weight = Sum of CARWGT(Carton weight) Divided by sum of MENGE(Quantity)

Where Material & size Value is same .

MENGE(Quantity) is also of same QUAN type..

can ny one give an idea to how to do this..plz

Thanks,

Kumar.

Read only

0 Likes
1,404

say itab1 has matnr, size, carwgt,menge. which has multiple records for one matnr and size.

and itab2 is what we need of type matnr, size, perunit

now.

sort itab1 by matnr, size.
data: gv_matnr type matnr,
   size like itab1-size.

loop at itab1 into is1.
  if is1-matnr = gv_matnr and is1-size = gv_size.
     gv_wt =gv_wt + is1-carwgt.
     gv_men = gv_men + is1-menge.
     clear is1.
    continue.
  endif.
  "when a new record comes.. 
   gv_net = gv_wt / gv_men.
    is2-matnr = gv_matnr.
    is2-size = gv_size.
    is2-perunit = gv_net.
    append is2 to itab2. " new record inserted.
    clear: gv_net, gv_matnr,gv_size, gv_wt, gv_men, is2.

   "now for current new records in itab1.
     gv_matnr = is1-matnr.
     gv_size = is1-size.
     clear : is1.
endloop.

Read only

0 Likes
1,404

Thanks a lot,

But is there a way to use collect statement and solve the issue.So that the performance also wil be improved I guess.

Thanks,

Kumar

Read only

0 Likes
1,404

the code provided me will do without any performance issue. because any ways you will loop one more time to get the perunit.

Read only

0 Likes
1,404

HI,

Yes you can use collect statement for better performance here. look at this sample code,



data: begin of itab occurs 0,
      matnr(10) type c,
      val1 type i,
      val2 type i,
      end of itab.

data: begin of itab1 occurs 0,
      matnr(10) type c,
      val1 type i,
      val2 type i,
      avg(3) type p decimals 2,
      end of itab1.

      itab-matnr = 'm1'.
       itab-val1 = '30'.
       itab-val2 = '40'.
       append itab.
       clear itab.

       itab-matnr = 'm1'.
       itab-val1 = '30'.
       itab-val2 = '40'.
       append itab.
       clear itab.
       itab-matnr = 'm1'.
       itab-val1 = '70'.
       itab-val2 = '30'.
       append itab.
       clear itab.
       itab-matnr = 'm1'.
       itab-val1 = '80'.
       itab-val2 = '30'.
       append itab.
       clear itab.
      itab-matnr = 'm2'.
       itab-val1 = '70'.
       itab-val2 = '10'.
       append itab.
       clear itab.
       itab-matnr = 'm2'.
       itab-val1 = '90'.
       itab-val2 = '20'.
       append itab.
       clear itab.

       loop at itab.
       move-corresponding itab to itab1.
       collect itab1.
       endloop.

       loop at itab1.
       itab1-avg = itab1-val1 / itab1-val2.
       modify itab1.
       endloop.

       loop at itab1.
       write:/ itab1-matnr, itab1-val1, itab1-val2, itab1-avg.
       endloop.

Regards,

Vikranth

Read only

0 Likes
1,404

>

> HI,

> Yes you can use collect statement for better performance here. look at this sample code,

>


>"FIRST LOOP
>        loop at itab.
>        move-corresponding itab to itab1.
>        collect itab1.
>        endloop.
>"SECOND LOOP
>        loop at itab1.
>        itab1-avg = itab1-val1 / itab1-val2.
>        modify itab1. """
>        endloop.
> 

>

do you think this will increase the performance? rather than doing the entire thing in one single loop?

Read only

0 Likes
1,404

It is atleast better than manually summing up with messy coding rather than let the collect statement do the work for us. I should have said more programmer friendly rather than performance though i dont see any critical performance degradation looping on distinct entries.

Read only

Former Member
0 Likes
1,404

Example


TYPES: BEGIN OF COMPANY, 
        NAME(20) TYPE C, 
        SALES    TYPE I, 
      END OF COMPANY. 

DATA: COMP    TYPE COMPANY, 
      COMPTAB TYPE HASHED TABLE OF COMPANY 
                                WITH UNIQUE KEY NAME. 

COMP-NAME = 'Duck'.  COMP-SALES = 10. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Duck'.  COMP-SALES = 30. COLLECT COMP INTO COMPTAB. 
Table COMPTAB now has the following contents: 

          NAME    | SALES 
          --------------- 
          Duck    |   40 
          Tiger   |   20 

Read only

Former Member
0 Likes
1,404

hi

its better u use AT NEW and AT END OF..

EX :

LOOP.

AT NEW 2ND FIELD.

MOVE IST FIELD TO WA.

2ND FIELD TO WA..

ENDAT.

TAKE A COUNTER VARIBALE .TO GET ADDITION OF ALL WEIGHTS.

AT END OF 2ND FIELD.

APPEND.

ENDAT......

ENDLOOP.

NOW IF THE COMBINATION OF THE FIELDS CHANGE THEN ANOTHER ROW WLD BE APPENDED IN TABLE .

THANKS

DHRUV