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

alternative to collect statement

Former Member
0 Likes
4,705

hi friends,

i have a scenario,

there are two itabs itab1 and itab2.

both have kostl and dmbtr( amount).

itab1 has multiple kostl entries with different dmbtrs.

i got to add all the dmbtrs for every specific kostl

and put them into itab2.

can any one suggest other than collect, because, i am afraid,

other fields may get messed up , which may be numeric values.

thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,870

Hi,

Ok..Try this code..


DATA: ITAB1 TYPE STANDARD TABLE OF BSEG.
DATA: ITAB2 TYPE STANDARD TABLE OF BSEG.

DATA: WA          TYPE BSEG.
DATA: WA_TMP      TYPE BSEG.
DATA: V_AMOUNT    TYPE BSEG-DMBTR.
DATA: V_PREV      TYPE BSEG-KOSTL.

* Let's assume the data is there in the internal table ITAB1.

* Sort the internal table by KOSTL.
SORT ITAB1 BY KOSTL.

* Process the internal table ITAB1.
LOOP AT ITAB1 INTO WA.

  WA_TMP = WA.

* Sum up the amount.
  V_AMOUNT = V_AMOUNT + WA-DMBTR.

* Check if the value is not previous.
  IF WA-KOSTL <> V_PREV AND SY-TABIX <> 1.

* Populate the itab2.    
    MOVE WA TO ITAB2.
    WA-KOTSL  = V_PREV.
    WA-DMBTR = V_AMOUNT.
    APPEND WA TO ITAB2.  
  
    CLEAR V_AMOUNT.

  ENDIF.

* At the last check if the amount is not initial.
  AT LAST.
    IF NOT V_AMOUNT IS INITIAL.

* Populate the internal table.
    MOVE WA TO ITAB2..
    WA-DMBTR = V_AMOUNT.
    APPEND WA TO ITAB2.

    ENDIF.
  ENDAT.

* Store the previous number.
  V_PREV = WA_TMP-KOSTL.
ENDLOOP.

Thanks,

Naren

12 REPLIES 12
Read only

Former Member
0 Likes
2,870

See the sample program and i did not use collect command,eventhough i am displaying totals and subtotals.

&----


*& Report ZTEST_IEVENTS

*&

&----


*&

*&

&----


REPORT ZTEST_IEVENTS no standard page heading

line-count 40(2).

tables : vbap.

data : begin of i_vbap occurs 0,

vbeln like vbap-vbeln,

posnr like vbap-posnr,

matnr like vbap-matnr,

kwmeng like vbap-kwmeng,

netpr like vbap-netpr,

end of i_vbap.

data wa_vbap like line of i_vbap.

data v_flag type c.

select-options s_vbeln for vbap-vbeln obligatory.

start-of-selection.

select vbeln

posnr

matnr

kwmeng

netpr from vbap

into table i_vbap

where vbeln in s_vbeln.

sort i_vbap by vbeln posnr.

end-of-selection.

loop at i_vbap.

move i_vbap to wa_vbap.

at first.

write:/2 'Order #',15 'Item #',28 'Material #',50 'Qty', 70 'Net value'.

skip 1.

endat.

at new vbeln.

write:/2 wa_vbap-vbeln,15 wa_vbap-posnr,28 wa_vbap-matnr,

47 wa_vbap-kwmeng,65 wa_vbap-netpr.

v_flag = 'X'.

endat.

if v_flag ne 'X'.

write:/15 wa_vbap-posnr,28 wa_vbap-matnr,

47 wa_vbap-kwmeng,65 wa_vbap-netpr.

endif.

at end of vbeln.

sum.

skip 1.

write:/5 'Sub totals', 47 i_vbap-kwmeng,65 i_vbap-netpr.

skip 1.

endat.

at last .

skip 1.

sum.

write:/5 'Grand Totals',47 i_vbap-kwmeng,65 i_vbap-netpr.

skip 1.

write:/ 'end of page', 'Footer'.

endat.

clear v_flag.

endloop.

Read only

Former Member
0 Likes
2,870

Hi,

Why cant u use Append or insert command.....just check it out.

if useful reward with points.

with regards,

madhuri.

Read only

Former Member
0 Likes
2,870

Hi Saritha

Why dont u try AT Control break statements like

AT FIRST

AT LAST

AT NEW

AT END

ON CHANGE OF.

Need ur reward points if its helpful.

Regards

Ravi

Read only

Former Member
0 Likes
2,870

can some one plz help me with the code.

thank you.

Read only

Former Member
0 Likes
2,870

clear : itab2,itab2[].

loop at itab1.

read table itab2 with key kostl = itab1-kostl.

if sy-subrc = 0.

itab2-dmbtr = itab1-dmbtr + itab2-dmbtr.

modify itab2 index sy-tabix.

else.

append itab1 to itab2.

endif.

clear itab2.

endloop.

both are header line int tables so for without header line you have to make use of work area.

regards

shiba dutta

Read only

Former Member
0 Likes
2,870

Hi,

Please post the internal table declaration for ITAB1 & ITAB2.. I will get you the code without using collect statement..

Thanks,

Naren

Read only

Former Member
0 Likes
2,870

Hi,

The best way to replace collect statement is loop& at end of [field]

For exp.

suppose itab1 has two fileds:

key1 key2 quan

If you want use key2 for collecting.

1.Append records to the internal table.

2.Sort the internal table by key2

3.Loop the internal table

loop itab.

str_itab = itab. " Storage the record to a structure like itab

tmp_quan = tmp_quan + itab-quan. " Collect the quantity

at end of key2.

tmp_itab = str_itab. " Collect the record to a tmp internal table

tmp_itab-quan = tmp_quan.

append tmp_itab.

clear: tmp_quan.

endat.

endloop.

4. The tmp_itab stored the collect result

5. Clear: itab,itab[].

6. itab[] = tmp_itab[].

Hope this helps

Bob

Read only

Former Member
0 Likes
2,870

thank you very much, every one,

narendra, as you asked , both the itab1 and itab2 have bseg structure.

Read only

0 Likes
2,870

Hi Saritha,

Can you please be more specific with requirement, as you said both are type BSEG, then lets say you added all the DMBTR for a particular KOSTL then what do you want to do with this entry.

As you may want to move it to another ITAb2, but what about all other fields of BSEG.

If your requirement is just to get the total of all the KOSTL then define ITAB2 with only two fields which are KOSTL and DMBTR and use anyone of the logic given above to get the sum. Even you can use COLLECT then as table will have only two fields.

Reward points if useful.

Regards,

Atish

Read only

Former Member
0 Likes
2,870

Hi

Do like this.........

Sort itab1 by kostl.

Loop at itab1.

at end of kostl.

sum.

endat.

move-corresponding itab1 to itab2.

append itab2.

endloop.

Try this and in case of some error write back..

Reward All Helpfull Answers.........

Read only

Former Member
0 Likes
2,871

Hi,

Ok..Try this code..


DATA: ITAB1 TYPE STANDARD TABLE OF BSEG.
DATA: ITAB2 TYPE STANDARD TABLE OF BSEG.

DATA: WA          TYPE BSEG.
DATA: WA_TMP      TYPE BSEG.
DATA: V_AMOUNT    TYPE BSEG-DMBTR.
DATA: V_PREV      TYPE BSEG-KOSTL.

* Let's assume the data is there in the internal table ITAB1.

* Sort the internal table by KOSTL.
SORT ITAB1 BY KOSTL.

* Process the internal table ITAB1.
LOOP AT ITAB1 INTO WA.

  WA_TMP = WA.

* Sum up the amount.
  V_AMOUNT = V_AMOUNT + WA-DMBTR.

* Check if the value is not previous.
  IF WA-KOSTL <> V_PREV AND SY-TABIX <> 1.

* Populate the itab2.    
    MOVE WA TO ITAB2.
    WA-KOTSL  = V_PREV.
    WA-DMBTR = V_AMOUNT.
    APPEND WA TO ITAB2.  
  
    CLEAR V_AMOUNT.

  ENDIF.

* At the last check if the amount is not initial.
  AT LAST.
    IF NOT V_AMOUNT IS INITIAL.

* Populate the internal table.
    MOVE WA TO ITAB2..
    WA-DMBTR = V_AMOUNT.
    APPEND WA TO ITAB2.

    ENDIF.
  ENDAT.

* Store the previous number.
  V_PREV = WA_TMP-KOSTL.
ENDLOOP.

Thanks,

Naren

Read only

Former Member
0 Likes
2,870

thank you very much every one for all your pains.

thanks naren.