Skip to Content

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

How to assign counter in the loop?

I have one internal table i_data with 100 records likeu2026.

Material	Plant
M1	P1
M2	P1
M3	P1
M4	P1
M5	P1
M6	P1
M7	P1
M8	P2
M9	P2
M10	P2
M11	P3
M12	P3
M13	P3
M14	P3
M15	P3
M16	P3
M17	P4
M18	P4
--------
--------

I have one selection screen parameter p_tot = 5.

I_data is sorted by plant.

While making loop at I_data, I want to check thatu2026

If counter > p_tot or plant changes,

Concatenate material with u2018_1u2019

Else

Use same material

Endif.

Here, for plant P1, if t counter is > 5 than use u2018_1u2019, then rest of two records use u2018_2u2019 then plant changes (P2) so use u2018_3u2019 then plant changes (P3) so use u2018_4u2019 here it is greater than 5 so for same plant (P3) for one record, use u2018_5u2019 then for plant (P4), use u2018_6u2019 and so on.

How to do this?

Regards,

ronny

Tags:
replied

hi Ronny,

use this..

  DATA:
    ld_tabix TYPE sy-tabix,
    ld_count TYPE p,
    ld_ccnt  TYPE char10,
    ld_plant type waers.

  ld_tabix = 0.
  ld_count = 0.

  LOOP AT it_data INTO wa_data.
    if wa_data-plant ne ld_plant.
      ADD 1 TO ld_count.
      MOVE 0 TO ld_tabix.
    endif.

    ADD 1 TO ld_tabix.

    IF ld_tabix GT p_tot.
      ADD 1 TO ld_count.
      MOVE 1 TO ld_tabix.
    ENDIF.

    MOVE ld_count TO ld_ccnt.

    SHIFT ld_ccnt LEFT DELETING LEADING space.

    CONCATENATE wa_data-matrl '_' ld_ccnt INTO wa_data-matrl.
    CONDENSE wa_data-matrl.

    MODIFY it_data FROM wa_data INDEX sy-tabix.

    ld_plant = wa_data-plant.
  ENDLOOP.

regards,

Peter

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