Skip to Content
author's profile photo Former Member
Former Member

LOOP situation

Hello experts,

Could you help me with this.

Here's the scenario:

col1 col2

AA 01

AA 00

AA 01

BB 00

BB 01

BB 00

I wanted to know how many 01s and 00s per column 1. so in that scenario.. the result should be:

AA 2 1

BB 1 2

Column2: for all 01

Column3: for all 00

Thanks in advance..

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

9 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 10:17 AM

    Hi

    Hope this helps - I've not tested it but it should work you just need to declare the count variables.

    Kind regards

    Andy

    SORT itab BY col1 col2
    LOOP AT itab.
    
      IF itab-col2 = '00'.
        count_0 = count_0 + 1.
      ELSEIF itab-col2 = '01'.
        count_1 = count_0 + 1.
    
      AT END col1.
        WRITE: /itab-col1, count_1, count_2.
        count_0 = 0.
        count_1 = 0.
      ENDAT.
    ENDLOOP.
    
    

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 10:18 AM

    Hi Lana,

    try it with collect.

    copy your internal table to itab1 an add

    a field like I type I.

    loop at itab.

    itab1 = itab.

    itab1-i = 1.

    collect itab1.

    endloop.

    in Itab1 you get your Information.

    Regards, Dieter

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 10:23 AM

    Hi,

    Pl find eg below.

    LOOP AT internal table .

    l_count = l_count + 1.

    at end of column 1 .

    <logic >

    sum.

    on change of internal table- column 1.

    endon.

    endat.

    ENDLOOP.

    Pl reward if it helps.

    Cheers.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 10:33 AM

    Hi,

    follow below logic.

    sort itab by col1.

    data:begin of itab1 occurs 0 ,

    col1(2) type c,

    count00 type i,

    count01 type i,

    end of itab1.

    loop at itab.

    at new col1.

    clear lv_cnt,lv_cnt1.

    endat.

    if itab-col2 = '00'

    lv_cnt = lv_cnt + 1.

    elseif itab-col2 = '01'

    lv_cnt1 = lv_cnt1 + 1.

    endif.

    . at end of col1.

    itab1-col1 = itab-col1.

    itab1-count00 = lv_cnt.

    itab1-count01 = lv_cnt1.

    append itab1.

    clear itab1.

    endat.

    endloop.

    loop at itab1.

    write:itab1.

    endloop.

    itab1 will have output

    AA 2 1

    BB 1 2

    Regards

    amole

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 10:39 AM

    check this code it may help u.

    report z_example.

    data:begin of itab occurs 0,

    col1(3) type c,

    col2(3) type c,

    end of itab.

    data:begin of itab1 occurs 0,

    col1(3) type c,

    count1 type i,

    count2 type i,

    end of itab1.

    itab-col1 = 'AA'.

    itab-col2 = '01'.

    append itab.

    itab-col1 = 'AA'.

    itab-col2 = '00'.

    append itab.

    itab-col1 = 'AA'.

    itab-col2 = '01'.

    append itab.

    itab-col1 = 'BB'.

    itab-col2 = '01'.

    append itab.

    itab-col1 = 'BB'.

    itab-col2 = '00'.

    append itab.

    sort itab by col1.

    loop at itab.

    at new col1.

    itab1-col1 = itab-col1.

    endat.

    if itab-col2 = '01'.

    itab1-count1 = itab1-count1 + 1.

    elseif itab-col2 = '00'.

    itab1-count2 = itab1-count2 + 1.

    endif.

    at end of col1.

    append itab1.

    clear itab1.

    endat.

    endloop.

    loop at itab1.

    write:/ itab1-col1,itab1-count1,itab1-count2.

    endloop.

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 11:38 AM

    Hi,

    Below is the source code for this scenario.

    Maintain swap internal table and also Counts internal table as shown in the below source code.

    report abc.

    data:

    v_count type i,

    v_tabix type sy-tabix.

    *-- Actual Data

    data:begin of it_actual occurs 0,

    f1(3) type c,

    f2(3) type c,

    end of it_actual.

    *-- Swap Actual Data

    data:begin of it_swap occurs 0,

    f1(3) type c,

    f2(3) type c,

    end of it_swap.

    *-- Holds Counts for sorted data

    data:begin of it_sort occurs 0,

    col1(3) type c,

    col2(3) type c,

    count type i,

    end of it_sort.

    start-of-selection.

    *-- Append Test Data

    perform append.

    *-- Sort Data

    perform sort.

    &----


    *& Form append

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM append .

    it_actual-f1 = 'AA'.

    it_actual-f2 = '01'.

    append it_actual.

    it_actual-f1 = 'AA'.

    it_actual-f2 = '00'.

    append it_actual.

    it_actual-f1 = 'AA'.

    it_actual-f2 = '01'.

    append it_actual.

    it_actual-f1 = 'BB'.

    it_actual-f2 = '00'.

    append it_actual.

    it_actual-f1 = 'BB'.

    it_actual-f2 = '01'.

    append it_actual.

    it_actual-f1 = 'BB'.

    it_actual-f2 = '00'.

    append it_actual.

    *-- Your actual format

    loop at it_actual.

    write:/ it_actual-f1, it_actual-f2.

    endloop.

    skip 2.

    *-- Populate other table which swaps the fields

    loop at it_actual.

    it_swap-f1 = it_actual-f2.

    it_swap-f2 = it_actual-f1.

    append it_swap.

    clear it_swap.

    endloop.

    ENDFORM. " append

    &----


    *& Form sort

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM sort .

    *-- Sort as per requirement

    sort it_swap by f1 f2.

    loop at it_swap.

    write:/ it_swap-f1, it_swap-f2.

    endloop.

    loop at it_swap.

    *-- Populate Loop index

    v_tabix = sy-tabix.

    at new f2.

    *-- Initialize counter

    clear v_count.

    endat.

    *-- Add counter

    v_count = v_count + 1.

    at end of f2.

    *-- Read to avoid STARS

    read table it_swap index v_tabix.

    *-- Append other table

    it_sort-col1 = it_swap-f2.

    it_sort-col2 = it_swap-f1.

    it_sort-count = v_count.

    append it_sort.

    clear it_sort.

    endat.

    endloop.

    *-- Now you have the below Internal table which holds counts

    skip 2.

    loop at it_sort.

    write: / it_sort-col1, it_sort-col2, it_sort-count.

    endloop.

    *-- You can sort this again to have the proper output

    skip 2.

    sort it_sort by col1 col2.

    loop at it_sort.

    v_tabix = sy-tabix.

    at new col1.

    write: / it_sort-col1.

    endat.

    at end of col2.

    read table it_sort index v_tabix.

    write: it_sort-count.

    endat.

    endloop.

    ENDFORM. " sort

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 10:22 AM

    define itab with fields col1 cnt0 cnt1.

    itable->is the internal table with your data.

    sort itable.

    Loop at itable.

    at new col1.

    clear : lcnt0, lcnt1.

    endat.

    if itable-col2 = '00'.

    add 1 to lcnt0.

    elseif itable-col2 = '01'.

    add 1 to lcnt1.

    endif.

    at end of col1.

    move itable-col1 to itab-col1.

    move lcnt0 to itab-cnt0.

    move lcnt1 to itab-cnt1.

    append itab.

    endat.

    Endloop.

    itab has the data you require

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      I would advice you to use AT events rather than ON CHANGE, as they are now obselte. But in case you still wish to use them here is the code.

      sort itable.

      read table itable index 1.

      Loop at itable.

      if itable-col2 = '00'.

      lcnt0 = lcnt0 + 1.

      elseif itable-col2 = '01'.

      lcnt1 = lcnt1 + 1.

      endif.

      on change of itable-col1 or itable-col2.

      move itable-col1 to itab-col1.

      move lcnt0 to itab-cnt0.

      move lcnt1 to itab-cnt1.

      append itab.

      endon.

      Endloop.

      Message was edited by: Anurag Bankley

  • author's profile photo Former Member
    Former Member
    Posted on Oct 19, 2006 at 06:33 AM

    Hi,

    data:begin of itab occurs 0,

    cl1(2) type c,

    cl2(2) type c,

    end of itab.

    data:begin of itab2 occurs 0,

    cl1(2) type c,

    ca0 type i,

    ca1 type i,

    end of itab2.

    data:ca0 type i value 0,ca1 type i value 0,

    count type i value 0.

    itab-cl1 = 'AA'.

    itab-cl2 = '01'.

    append itab.

    itab-cl1 = 'AA'.

    itab-cl2 = '00'.

    append itab.

    itab-cl1 = 'AA'.

    itab-cl2 = '01'.

    append itab.

    itab-cl1 = 'BB'.

    itab-cl2 = '00'.

    append itab.

    itab-cl1 = 'BB'.

    itab-cl2 = '01'.

    append itab.

    itab-cl1 = 'BB'.

    itab-cl2 = '00'.

    append itab.

    <b>sort itab.

    loop at itab.

    on change of itab-cl1.

    clear: ca0,ca1,count.

    endon.

    if itab-cl2 = '00'.

    ca0 = ca0 + 1.

    else.

    ca1 = ca1 + 1.

    endif.

    perform outdata.

    endloop.</b>

    &----


    *& Form outdata

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    form outdata .

    if count > 0.

    if itab2-cl1 <> itab-cl1.

    itab2-cl1 = itab-cl1.

    itab2-ca0 = ca0.

    itab2-ca1 = ca1.

    append itab2.

    else.

    itab2-cl1 = itab-cl1.

    itab2-ca0 = ca0.

    itab2-ca1 = ca1.

    append itab2.

    write:/ itab2-cl1,itab2-ca0,itab2-ca1.

    endif.

    endif.

    count = count + 1.

    endform. " outdata

    Please do reward points if it solves your problem

    Regards,

    Sowjanya

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 19, 2006 at 06:49 AM

    or in simple way without using any control breaks

    loop at itab.

    if itab-cl1 = 'AA'.

    if itab-cl2 = '00'.

    ca0 = ca0 + 1.

    else.

    ca1 = ca1 + 1.

    endif.

    else.

    if itab-cl2 = '00'.

    cb0 = cb0 + 1.

    else.

    cb1 = cb1 + 1.

    endif.

    endif.

    endloop.

    Regards,

    Sowjanya

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.