Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How can i use "Collect" statement?


i have a table like that:

DATA: BEGIN OF it_display  OCCURS 0,

        zodb(20)     TYPE c,

        zmonth01     TYPE i,

        zmonth02     TYPE i,

        zmonth03     TYPE i,

        zmonth04     TYPE i,

        zmonth05     TYPE i,

    END OF it_display.

For each zodb field i found month01 values, after that for each zodb field i found month02 values ... and so on. Like that:

loop at it_odb. --> (zodb field values)

    DATAlv_pmonth(20TYPE c.

     zcount = 1.

    UNASSIGN  <fs_pmonth> .

    ztext = it_odb-cetxt.


     CONCATENATE 'it_display-zmonth0' zcount

        INTO lv_pmonth .

     ASSIGN (lv_pmonth) TO <fs_pmonth> .

     it_display-zodb = ztext.

     <fs_pmonth> = g_count. -->( i assign it_display-zmonth01,2 values ... and so problem)

     sort it_display BY zodb .

     collect it_display.--> problem accured!!!


zcount = zcount + 1.

Data sample:

it_display for first step:

zodb  month01 month02 mont03 mont04 month05  -->it_display table fields

  A         1000

  B            0             this step no problem...

  C         383

it_display for second step:

zodb  month01 month02 mont03 mont04 month05  -->it_display table fields

A        1383        734

  B         383          0

  C         766          .......

As you see, previous values was changed..i want to collect data depends on zodb field.But my code collect month01,month02...etc.  also.

How can i fix that ?

Thank you


Oh dear, where to begin,

First -  only a minor issue in this context but nevertheless worth mentioning:

COLLECT will temporarily build a hashed table as long as you don't mess with the table content and only use collect. Meaning, the moment you use sort, you'll loose that advantage. For big tables, this can be an issue.

Second - you are using obsolete concepts like implicit workareas, named the same as your table. This does not increase the readability of your code.

Third: i can't see how you proceed to step 2, 3 ...but assumed you have an outer loop,

your increment zcount = zcount + 1 is rendered useless by zcount = 1 inside your loop.

Fourth: i may have missed it, but i don't see, where you clear your implicit workarea after reassigning your month-field. So this is probably the cause of your problem since it_display-zmonth01 will still have the last value of 383, which, what a coincidence, is exactly the value added to each line

Nevertheless, consider the other points.

Best regards - Jörg

2 View this answer in context

Helpful Answer

Not what you were looking for? View more on this topic or Ask a question