Application Development 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: 

MATNR Count

Former Member
0 Kudos

Hi All,

My internal table has 7 material numbers, ITAB-MATNR1 to ITAB-MATNR7. I have to count(mcnt) the number of MATNR which are not initial. Can some one please help me how to count this.

If 2 MATNR has value then MCNT should be 2, if 4 of them has value then MCNT should be 4.

I will really appriciate your help.

Thanks,

Veni.

6 REPLIES 6

Former Member
0 Kudos

Hi,

Try this code..

FIELD-SYMBOLS: <FS>.
DATA: V_COUNT TYPE CHAR5.
DATA: V_FIELD   TYPE CHAR30.
DATA: V_MCNT   TYPE INT4.

LOOP AT ITAB INTO WA.

* Clear.
  CLEAR: v_mcnt.

  DO 7 TIMES.

    V_COUNT = SY-INDEX.
    CONCATENATE 'MATNR' V_COUNT INTO V_FIELD.
    CONDENSE V_FIELD.

    ASSIGN COMPONENT V_FIELD OF STRUCTURE WA TO <FS>.

    IF SY-SUBRC = 0.

       IF NOT <FS> IS INITIAL.

* Incremement the counter if it has some value.
          V_MCNT = V_MCNT + 1.

       ENDIF.

    ENDIF.

  ENDDO.

* Modify the internal table based on the count.
  WA-MCNT = V_MCNT.
  MODIFY ITAB FROM WA.

ENDLOOP.

Thanks

Naren

Vijay
Active Contributor
0 Kudos

hi,

i think you will have to specifically check each column if it it blank or having value and keep on incrementing the variable in case of blank.

regards

vijay

harimanjesh_an
Active Participant
0 Kudos

Hi Veni,

Try to add one more field 'COUNT' for your internal table. So that you can store the count for the corresponding row in the same row.

LOOP AT itab ASSIGNING <fs>.

CLEAR <fs>-count.

IF <fs>-matnr1 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

IF <fs>-matnr2 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

IF<fs>-matnr3 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

IF <fs>-matnr4 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

IF <fs>-matnr5 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

IF <fs>-matnr6 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

IF <fs>-matnr7 IS NOT INITIAL.

INCREMENT <fs>-count BY 1.

ENDIF.

ENDLOOP.

Now, Column count is having the exact number of materials having values in the respective rows of the internal table.

Reward me if useful.

Thanks,

Harimanjesh AN

Edited by: Harimanjesh AN on Sep 17, 2008 7:50 PM

0 Kudos

Hi All,

Thanks a lot for all your replies. Here is my internal table. If matnr1 only has value then mcnt = 1, if only matnr1 and matnr2 and matnr3 has value then mcnt = 3, if all has value then mcnt = 7.

TYPES: BEGIN OF ty_zcoop,

mandt TYPE mandt,

kunnr TYPE kunnr,

matnr1 TYPE matnr,

matnr2 TYPE matnr,

matnr3 TYPE matnr,

matnr4 TYPE matnr,

matnr5 TYPE matnr,

matnr6 TYPE matnr,

matnr7 TYPE matnr,

iamount TYPE ziamt,

oamount TYPE zoamt,

ref TYPE unsez,

erdat TYPE erdat,

index TYPE sy-index,

END OF ty_zcoop.

DATA: gt_zcoop TYPE TABLE OF ty_zcoop WITH HEADER LINE.

Thanks,

Veni.

Edited by: veni reddy on Sep 17, 2008 8:32 PM

0 Kudos

Hi,

Can anyone please help me. I tried all but nothing is working out.

Thanks,

Neelima.

Former Member
0 Kudos

Hi,

Did you check my code..Please let me know..

Thanks

Naren