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

nested control statements

Former Member
0 Likes
1,460

Hi ,

I want to nest two fields Field1 and field4 then change cnt as below example

I want output as cnt

cnt should increse when there is change in anlage and field4.

Field1 firld2 field3 field4 cnt

4003911794|IH-OFKW-B |20100129|20100301 | 1 |

4003911794|IH-ONKW-B |20100129|20100301 | 1 |

4003911794|IH-OFKW-B |20091230|20100128 | 2 |

4003911794|IH-ONKW-B |20100101|20100128 | 2|

4003911795|IH-ONKW-B |20091230|20091231 | 1 |

4003911795|IH-OFKW-B |20091126|20091229 | 2 |

4003911796|IH-ONKW-B |20091126|20091229 | 2 |

4003911797|IH-OFKW-B |20090929|20091125 | 1 |

4003911797|IH-ONKW-B |20091001|20091125 | 1 |

4003911797|IH-ONKW-B |20090929|20090930 | 2 |

Thanks,

Asha

1 ACCEPTED SOLUTION
Read only

former_member217544
Active Contributor
0 Likes
1,362

Hi Asha,

your table is like:

first 2 records which has same field1 and field2 has count-1

next two records has count- 2

again count1, count2..

Can you explain how exactly you want?

Also post your code as well so that it si easy to suggest the changes.

Thanks & Regards,

Swarna Munukoti

14 REPLIES 14
Read only

GauthamV
Active Contributor
0 Likes
1,362

Using control break statement at new for field1 and field4

you can define a new variable var and increment it everytime

and pass the same to cnt.

Read only

Former Member
0 Likes
1,362

I did like this

sort it_ETTIFN3 descending by field1 field4.

loop at it_ETTIFN3 into wa_ETTIFN3.

cnt = cnt + 1.

wa_ETTIFN3-cnt = cnt.

modify it_ETTIFN3 from wa_ETTIFN3 transporting cnt.

at end of field1. clear: cnt, wa_ETTIFN3-cnt. endat.

endloop.

output showing as

eld1 firld2 field3 field4 cnt need output as

4003911794|IH-OFKW-B |20100129|20100301 | 1 | 1

4003911794|IH-ONKW-B |20100129|20100301 | 2 | 1

4003911794|IH-OFKW-B |20091230|20100128 | 3 | 2

4003911794|IH-ONKW-B |20100101|20100128 | 4| 2

4003911795|IH-ONKW-B |20091230|20091231 | 1 | 1

4003911795|IH-OFKW-B |20091126|20091229 | 2 | 2

4003911796|IH-ONKW-B |20091126|20091229 | 1 |

4003911797|IH-OFKW-B |20090929|20091125 | 1 |

4003911797|IH-ONKW-B |20091001|20091125 | 2 |

4003911797|IH-ONKW-B |20090929|20090930 | 3 |

thanks

Read only

former_member217544
Active Contributor
0 Likes
1,363

Hi Asha,

your table is like:

first 2 records which has same field1 and field2 has count-1

next two records has count- 2

again count1, count2..

Can you explain how exactly you want?

Also post your code as well so that it si easy to suggest the changes.

Thanks & Regards,

Swarna Munukoti

Read only

0 Likes
1,362

Here I want to check two fields field1 and field4

both fields field1 and field4 are same no increse in counter, if fields1 and field 4 diff then counter increses.

preset output as

Field1 firld2 field3 field4 cnt

4003911794|IH-OFKW-B |20100129|20100301 | 1 |

4003911794|IH-ONKW-B |20100129|20100301 | 2 |

4003911794|IH-OFKW-B |20091230|20100128 | 3 |

4003911794|IH-ONKW-B |20100101|20100128 | 4|

4003911795|IH-ONKW-B |20091230|20091231 | 1 |

4003911795|IH-OFKW-B |20091126|20091229 | 2 |

4003911796|IH-ONKW-B |20091126|20091229 | 1 |

4003911797|IH-OFKW-B |20090929|20091125 | 1 |

4003911797|IH-ONKW-B |20091001|20091125 | 2 |

4003911797|IH-ONKW-B |20090929|20090930 | 3 |

i mention as

at end of field1.

clear cnt.

endat.

need output as

Field1 firld2 field3 field4 cnt

4003911794|IH-OFKW-B |20100129|20100301 | 1 |

4003911794|IH-ONKW-B |20100129|20100301 | 1 |

4003911794|IH-OFKW-B |20091230|20100128 | 2

4003911794|IH-ONKW-B |20100101|20100128 | 2

4003911795|IH-ONKW-B |20091230|20091231 | 1 |

4003911795|IH-OFKW-B |20091126|20091229 | 2 |

4003911796|IH-ONKW-B |20091126|20091229 | 1 |

4003911797|IH-OFKW-B |20090929|20091125 | 1 |

4003911797|IH-ONKW-B |20091001|20091125 | 1 |

4003911797|IH-ONKW-B |20090929|20090930 | 2 |

thanks

Read only

0 Likes
1,362

Hi Asha,

Keep one more field field5 in the internal table and

concatenate field1 and field4 and keep it in field5.

now in loop use

at end field5.

Regards,

Swarna Munukoti.

Read only

0 Likes
1,362

Hello,

Try something like this

Restructure your internal table. Set Field4 as your second field in the internal table. so your table would be like

field1

field4

field2

field3

cnt

Now run a loop on on the internal table

Loop at <internaltab> into <work area>.

at new <field 4>

cnt = cnt + 1.

endat.

modify the internal table with value of cnt.

at end of <field 4>

cnt = 0.

endat.

endloop.

Modify the above pseudocode as per your requirement.

Regards

Sachin.

Read only

0 Likes
1,362

Hi,

Hi,

You can solves this problem using loop and read at the table. use below code, it will definitely work.

Loop at itab into wa_itab.
v_index = sy-tabix.
if v_index = 1.
 move-corresponding wa_itab to wa_final.
 wa_final-count = 1.
 v_count = wa_final-count.
 append wa_final to i_final.
else.
 v_index = v_index - 1.
 read table itab into wa_itab_new with index v_index.
  if wa_itab-field1 EQ wa_itab_new-field1.
   if wa_itab-field4 NE wa_itab_new-field4.
    move-corresponding wa_itab to wa_final_new.
    wa_final-count = v_count + 1.
    append wa_final to i_final.
    v_count = wa_final-count.
   endif.
  elseif wa_itab-field1 NE wa_itab_new-field1.
   if wa_itab-field4 NE wa_itab_new-field4.
    v_count = 1.
    move-corresponding wa_itab to wa_final.
    wa_final-count = v_count.
    append wa_final to i_final.
    v_count = wa_final-count.
   endif.
  elseif wa_itab-field1 NE wa_itab_new-field1.
   if wa_itab-field4 EQ wa_itab_new-field4.
    move-corresponding wa_itab to wa_final_new.
    wa_final-count = v_count.
    append wa_final to i_final.
    v_count = wa_final-count.
   endif.
  elseif wa_itab-field1 EQ wa_itab_new-field1.
   if wa_itab-field4 EQ wa_itab_new-field4.
    move-corresponding wa_itab to wa_final_new.
    wa_final-count = v_count.
    append wa_final to i_final.
    v_count = wa_final-count.
   endif.
  endif.
endif.

Thanks,

Archana

Read only

0 Likes
1,362

Here,

I want to get result based on two fields

f1 f2 1

f1 f2 1

f1 f3 2

f2 f11 1

f2 f22 2

Read only

0 Likes
1,362

Hi Asha,

As you need to change count based on two fields, you can't use At endat. You need to consider all conditions based on field1 and field4. So, in the above code i gave, i am considering all those conditions and based on that count will be set.

So, please try out that code and let me know if it is working fine.

Thanks,

Archana

Read only

0 Likes
1,362


REPORT  ZEX_VIJ.

data : begin of itab occurs 0,
       f1(10) type c,
       f2(10) type c,
       f3 type d,
       f4 type d,
       f5(2) type c,
       end of itab.

*****************internal table ...
*1
itab-f1 = '4003911794'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20100129'.
itab-f4 = '20100301'.
append itab.

*2
itab-f1 = '4003911794'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20100129'.
itab-f4 = '20100301'.
append itab.

*3
itab-f1 = '4003911794'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20100129'.
itab-f4 = '20100328'.
append itab.

*4
itab-f1 = '4003911794'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20100129'.
itab-f4 = '20100328'.
append itab.

*5
itab-f1 = '4003911795'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20091230'.
itab-f4 = '20091131'.
append itab.

itab-f1 = '4003911795'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20091230'.
itab-f4 = '20091231'.
append itab.

itab-f1 = '4003911796'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20091230'.
itab-f4 = '20091231'.
append itab.

itab-f1 = '4003911797'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20091230'.
itab-f4 = '20091125'.
append itab.

itab-f1 = '4003911797'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20091230'.
itab-f4 = '20091125'.
append itab.

itab-f1 = '4003911797'.
itab-f2 = 'IH-OFKW-B'.
itab-f3 = '20091230'.
itab-f4 = '20090930'.
append itab.

*****************internal table ...
data : lv_flag type c ,
       lv_cnt type i  ,
       lv_date type d.
sort itab by f1.
  loop at itab.
    at new f1.
     lv_flag = 'X'.
    endat.

    if lv_flag = 'X'.
    lv_cnt = 1.
    lv_date = itab-f4.
    lv_flag = ''.
    endif.

    if lv_date = itab-f4.
    lv_cnt = lv_cnt.
    else.
    lv_date = itab-f4.
    lv_cnt = lv_cnt + 1.
    endif.

    itab-f5 = lv_cnt .

    modify itab index sy-tabix.

    at end of f1.
    clear: lv_flag, lv_date,lv_cnt.
    endat.
  write :/ itab-f1, itab-f2, itab-f3, itab-f4, itab-f5.
endloop.

Execute this and check if this meets ur requirement.

Br,

Vijay.

Read only

0 Likes
1,362

v_index = v_index - 1.

read table itab into wa_itab_new with index v_index.

if wa_itab-field1 EQ wa_itab_new-field1.

if wa_itab-field4 NE wa_itab_new-field4.

iam getting syntax error

could you help me in read statment

Read only

0 Likes
1,362

Excellant, Getting expected result.

Thanks

Read only

0 Likes
1,362

Hi,

Sorry for mistake. Syntax should be as below. Also, you can check syntax for such statements by pressing F1.

read table itab into wa_itab_new index v_index.

Thanks,

Archana

Read only

0 Likes
1,362

Hi Asha,

DATA : BEGIN OF itab OCCURS 0,
       f1(10) TYPE c,
       f2(10) TYPE c,
       f3 TYPE d,
       f4 TYPE d,
       f5(2) TYPE c,
       count TYPE i,  " Add this 
       END OF itab.


DATA : jtab LIKE itab OCCURS 0 WITH HEADER LINE.

jtab[] = itab[].
SORT itab BY f1 ASCENDING f4 ASCENDING.
SORT jtab BY f1 ASCENDING f4 ASCENDING.

LOOP AT itab.
  tabix = sy-tabix.
  CLEAR itab-count.
  LOOP AT jtab WHERE f1 = itab-f1 AND f4 = itab-f4.
    itab-count = itab-count + 1.
  ENDLOOP.
  MODIFY itab INDEX tabix.
  WRITE :/ itab-f1, sy-vline,
  itab-f2, sy-vline,
  itab-f3, sy-vline,
  itab-f4, sy-vline,
  itab-count, sy-vline.
ENDLOOP.
"This is the output

4003911794   IH-OFKW-B    29012010   01032010            2
4003911794   IH-OFKW-B    29012010   01032010            2
4003911794   IH-OFKW-B    29012010   28032010            2
4003911794   IH-OFKW-B    29012010   28032010            2
4003911795   IH-OFKW-B    30122009   31112009            1
4003911795   IH-OFKW-B    30122009   31122009            1
4003911796   IH-OFKW-B    30122009   31122009            1
4003911797   IH-OFKW-B    30122009   30092009            1
4003911797   IH-OFKW-B    30122009   25112009            2
4003911797   IH-OFKW-B    30122009   25112009            2

Hope this serves your purpse.

Regards

Ram