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

Confusion in loop inside loop

Former Member
0 Likes
1,030

Hi Experts,

i have some confution in below code:

here am trying to calculate Actual Billing and Planned Revenue. but here for each value am looping it_final table inside the main loop.

but the prob is if i loop it_final for one value it is giving value, but if i try to loop 2 times for 2 values, it is not giving first value also.

i.e if i comment loop for Planned Revenue then Actual Billing is displaying, if i try for 2 values, then its giving zeros for 2 values.

so could anyone check is there any mistake i did.

CLEAR: it_prps, it_final, wa_final, wa_it_prps.

LOOP AT it_prps.

CASE month.

WHEN '08'.

                                        • ACTUAL BILLING

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_04

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_coin OR vorga = c_rku2 ).

CLEAR act_billing.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_act_billing.

act_billing = act_billing + t_act_billing.

ENDIF.

CLEAR: it_prps, it_final.

ENDLOOP.

*************PLANNED REVENUE

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_01

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_sdor OR vorga = c_rkp5 ).

CLEAR plan_rev.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_plan_rev.

plan_rev = plan_rev + t_plan_rev.

ENDIF.

CLEAR: it_prps, it_final.

ENDLOOP.

ENDCASE.

ENDLOOP.

WRITE:/10 'Actual Billing is',act_billing.

WRITE:/20 'Actual Cost is........', actu_cost.

Thanks in advace,

sudharsan.

8 REPLIES 8
Read only

peter_ruiz2
Active Contributor
0 Likes
1,003

hi,

CLEAR act_billing.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_act_billing.

act_billing = act_billing + t_act_billing.

ENDIF.

CLEAR: it_prps, it_final.

ENDLOOP.

your problem comes from here

CLEAR: it_prps, it_final

you are clearing the header line of it_prps wherein it is used as a condition for the 2nd loop.

regards,

Peter

Read only

Former Member
0 Likes
1,003

Hi,

as peter said dont clear the internal table clear the work area only.

Regards

Sandipan

Read only

0 Likes
1,003

No Friends, if i clear work area also it is not giving values, if i try for 2 values.

please check is there any other reason?

Thanks in Advance,

sudharsan.

Read only

Former Member
0 Likes
1,003

Hi SUDHARSAN RAO

U r clearing the headers and tables. So u cannot use the values on the nested loops as u have cleared the values. There wont be anything in the workare ur using in where clause.

Please see the below comments that I kept.

LOOP AT it_prps.

ACTUAL BILLING

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_04

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_coin OR vorga = c_rku2 ).

CLEAR act_billing.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_act_billing.

act_billing = act_billing + t_act_billing.

ENDIF.

*******************************************

CLEAR: it_prps, it_final.

  • Here IT_FINAL is cleared by u... should not be cleared

********************************************

ENDLOOP.

*************PLANNED REVENUE

LOOP AT it_final INTO wa_final

WHERE posid = it_prps-posid

AND wrttp = c_01

AND beltp = c_02

AND versn = c_0

AND ( vorga = c_sdor OR vorga = c_rkp5 ).

CLEAR plan_rev.

IF sy-subrc IS INITIAL.

MOVE wa_final-wlp08 TO t_plan_rev.

plan_rev = plan_rev + t_plan_rev.

ENDIF.

  • Again ur clearing both tables.

CLEAR: it_prps, it_final.

********************************

ENDLOOP.

ENDCASE.

ENDLOOP.

WRITE:/10 'Actual Billing is',act_billing.

WRITE:/20 'Actual Cost is........', actu_cost.

Read only

peter_ruiz2
Active Contributor
0 Likes
1,003

hi sudharsan,

replace this code

CLEAR: it_prps, it_final.

with this


  append wa_final to it_final.
  clear wa_final.
 

you are not appending the data to the internal table that is why only one record is being displayed.

regards,

Peter

Read only

0 Likes
1,003

Hi Peter,

how to append wa_final TO it_final.

actually we are getting values from it_final into wa_final while looping.

btu i tried like u also, eventhrough not working.

Rgds,

sudharsan.

Read only

Former Member
0 Likes
1,003

Hi,

I see that you are clearing the variable ACT_BILLING inside the loop (LOOP at IT_FINAL).

That is the variable which holds the total and you are clearing inside the loop. that is the reason when there are multiple records it is not working fine.

Remove that clear statement. similar case for PLAN_REV.

These variables can be cleared before the loop actually begins.

Thanks and Regards,

Lakshmi.

Read only

peter_ruiz2
Active Contributor
0 Likes
1,003

i agree with Santhanalakshmi.

remove this lines.

CLEAR act_billing.

CLEAR plan_rev.

another reason for this is that your are displaying actu_cost here

WRITE:/20 'Actual Cost is........', actu_cost.

wherein you are using plan_rev.