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

Help with loop

Former Member
0 Likes
2,879

Hi Experts,

This is my sample code below

LOOP AT itab1.

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

EXIT.

ENDLOOP.

IF sy-subrc = 0.

itab1-PAY_DATE = itab2-BUDAT.

itab1-PAY_AMOUNT = itab2-BETRW.

itab1-PAY_AMOUNT = itab2--BETZG.

itab1-pay_opbel = itab2-opbel.

MODIFY itab1.

ENDIF.

ENDLOOP.

In some cases, the condition LOOP AT itab2 can have >1 records in itab2 which needs to be updated to itab1. Can some one help me to add it to itab1?

thanks

Dany

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,839

Hi Dan,

I guess you are still working on this one. If you need records to be updated or added to itab1 based on itab2, you may want to try this. But I think giving an example would be the best way to explain the problem.

LOOP AT itab1.

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

move-corresponding itab2 to itab1.

itab1-PAY_DATE = itab2-BUDAT.

itab1-PAY_AMOUNT = itab2-BETRW.

itab1-PAY_AMOUNT = itab2--BETZG.

itab1-pay_opbel = itab2-opbel.

MODIFY itab1.

ENDLOOP.

ENDLOOP.

37 REPLIES 37
Read only

Former Member
0 Likes
2,839

Hi Dan

If ITAB1 and ITAB2 has same number of records, You shouldn't worry.

instead of exiting from the loop you can modify IATB1 n the loop itself

LOOP AT itab1.

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

itab1-PAY_DATE = itab2-BUDAT.

itab1-PAY_AMOUNT = itab2-BETRW.

itab1-PAY_AMOUNT = itab2--BETZG.

itab1-pay_opbel = itab2-opbel.

MODIFY itab1.

ENDLOOP.

ENDLOOP.

Otherwise you have to APPEND the details to a 3rd ITAB

LOOP AT itab1.

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

itab3-XXXX = itab1-XXXX

itab3-PAY_DATE = itab2-BUDAT.

itab3-PAY_AMOUNT = itab2-BETRW.

itab3-PAY_AMOUNT = itab2--BETZG.

itab3-pay_opbel = itab2-opbel.

APPEND ITAB3.

ENDLOOP.

ENDLOOP.

Edited by: Arun Shekhar on Jan 4, 2008 8:21 PM

Read only

Former Member
0 Likes
2,839

Arun that doesn't work.

Oh! i forgot to mention that when a valid date comes between from date & to_date, the loop at itab2 is returing sy-subrc as 4.

itab1 & itab2 have some payment dates & amounts related fields common to each other.

Pls suggest.

Read only

former_member194669
Active Contributor
0 Likes
2,839

May be this way.


data : v_flg type c.
LOOP AT itab1.

clear : v_flg.
LOOP AT itab2 WHERE budat GE itab1-from_date
                  AND budat LE itab1-to_date.
 if v_flg ne 'Y'. 
   itab1-PAY_DATE = itab2-BUDAT.
   itab1-PAY_AMOUNT = itab2-BETRW.
   itab1-PAY_AMOUNT = itab2--BETZG.
   itab1-pay_opbel = itab2-opbel.
   MODIFY itab1.
   move 'Y' to v_flg.
 else.
   move-corresponding itab1 to itab3. " Here Itab3 as same structue as itab1
   itab3-PAY_DATE = itab2-BUDAT.
   itab3-PAY_AMOUNT = itab2-BETRW.
   itab3-PAY_AMOUNT = itab2--BETZG.
   itab3-pay_opbel = itab2-opbel.
   append itab3
 endif.  

ENDLOOP.

ENDLOOP.

append lines of itab3 to itab1.

a®

Read only

Former Member
0 Likes
2,839

Hi Dan,

Let us know what are the structures of itab1 and itab2 and what is the exact requirement??

Read only

0 Likes
2,839

Hi Chandrasekhar Jagarlamudi

This is with IS uitilites stuff.

The itab2 is getting popluated using an FM: FKK_READ_LAST_PAYMENTS

itab2 resembles a strcuture called FKKPAID which is as below:

The strutcture of itab2 is :

BUDAT Posting date in doc

CPUDT Accounting document

CPUTM Time of entry

OPBEL Contract Account doc no

WAERS Currency Key

BETRW payment amount

BETZG Total amount of payment

itab 1 has same & has other stuff in its strcuture. The Budat, BETRW & OPBEL need to be updated.

Now during LOOP AT itab2 WHERE budat GE itab1-from_date AND budat LE itab1-to_date.

there may be a match which has more than one records in itab2 which needs to be updated in itab1.

Right now only the first matched record in itab2 is updated.

Suggest accrodingly

Read only

0 Likes
2,839

@Dan

If for ITAB1, there exists more than 1 corresponding entry in ITAB2, you need to use ITAB3.

ITAB3 wil be the final int.Tab where in you can save your data.

ITAB3 should be of the same structure as ITAB1.

You can't go on modifying ITAB1..since if you do, only the last matching record details will be updated.

Read only

0 Likes
2,839

Hi Arun -

Seems you are close to the solution. Just tell me how do i use ITAB3 here?

thanks

Read only

0 Likes
2,839

Dan,

Please check my reply for how to use ITAB3

a®

Read only

0 Likes
2,839

Hi a®s ,

but what would be the value of v_flg during the 1st loop pass?

thanks

Read only

0 Likes
2,839

Dan,

May be this way.


data : v_flg type c.
LOOP AT itab1.

clear : v_flg. " V_flg is blank
LOOP AT itab2 WHERE budat GE itab1-from_date
                  AND budat LE itab1-to_date.
 if v_flg ne 'Y'.         " First it will be blank after modify itab1 it will be  'Y;
   itab1-PAY_DATE = itab2-BUDAT.
   itab1-PAY_AMOUNT = itab2-BETRW.
   itab1-PAY_AMOUNT = itab2--BETZG.
   itab1-pay_opbel = itab2-opbel.
   MODIFY itab1.
   move 'Y' to v_flg.
 else.      " Here v_flg will be Y so it will get append to itab3
   move-corresponding itab1 to itab3. " Here Itab3 as same structue as itab1
   itab3-PAY_DATE = itab2-BUDAT.
   itab3-PAY_AMOUNT = itab2-BETRW.
   itab3-PAY_AMOUNT = itab2--BETZG.
   itab3-pay_opbel = itab2-opbel.
   append itab3
 endif.  

ENDLOOP.

ENDLOOP.
" Loop of itab1 completes then you need to append from itab3 to itab1
append lines of itab3 to itab1.

a®

Read only

0 Likes
2,839

a®s

Dont we need to make use of sy-subrc here?

Read only

0 Likes
2,839

i don;t find any read statement here. Within in the loop no need to use sy-subrc

a®

Read only

0 Likes
2,839

a®s - We are using where condition in the loop at itab2 where <>..

Wont sy-subrc work here?

Read only

0 Likes
2,839

Dan,

Within loop with where condition , here sy-subrc will be always 0 no need to check



loop itab <with where condition> 
" Here if your condition passes then only control will come into this
endloop.

a®

Read only

0 Likes
2,839

Hello,

I think there's a "clear" of v_flg missing in your solution, when leaving the LOOP AT itab2. Otherwise you'll have double ITAB1 entries (the original entry and a new one from ITAB3 with the additional data from ITAB2), as v_flg will be 'Y' after the first call of lopp ITAB2.

data : v_flg type c.

LOOP AT itab1.

clear : v_flg. " V_flg is blank

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

if v_flg ne 'Y'. " First it will be blank after modify itab1 it will be 'Y;

itab1-PAY_DATE = itab2-BUDAT.

itab1-PAY_AMOUNT = itab2-BETRW.

itab1-PAY_AMOUNT = itab2--BETZG.

itab1-pay_opbel = itab2-opbel.

MODIFY itab1.

move 'Y' to v_flg.

else. " Here v_flg will be Y so it will get append to itab3

move-corresponding itab1 to itab3. " Here Itab3 as same structue as itab1

itab3-PAY_DATE = itab2-BUDAT.

itab3-PAY_AMOUNT = itab2-BETRW.

itab3-PAY_AMOUNT = itab2--BETZG.

itab3-pay_opbel = itab2-opbel.

append itab3

endif.

ENDLOOP.

CLEAR v_flg. "new

ENDLOOP.

" Loop of itab1 completes then you need to append from itab3 to itab1

append lines of itab3 to itab1.

Best regards

Stephan

Read only

0 Likes
2,839

Stephan,


LOOP AT itab1.

clear : v_flg. " V_flg is blank
LOOP AT itab2 WHERE budat GE itab1-from_date
AND budat LE itab1-to_date.

you can see the v_flg is cleared after the loop at itab1.

a®

Read only

0 Likes
2,839

Hello a®s,

sorry. You're correct. I haven't seen it.

Best regards

Stephan

Read only

Former Member
0 Likes
2,839

field-symbols: <wa> type itab1.

LOOP AT itab1 assigning <wa> .

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

<wa>-PAY_DATE = itab2-BUDAT.

<wa>-PAY_AMOUNT = itab2-BETRW.

<wa>-PAY_AMOUNT = itab2--BETZG.

<wa>-pay_opbel = itab2-opbel.

ENDLOOP.

ENDLOOP.

chekc this. no need do the modify.

Read only

Former Member
0 Likes
2,839

did you try with in loop??

>

> Hi Experts,

> This is my sample code below

DATA : idx TYPE sy-tabix.

> LOOP AT itab1.

idx = sy-tabix.

> LOOP AT itab2 WHERE budat GE itab1-from_date

> AND budat LE itab1-to_date.

itab1-PAY_DATE = itab2-BUDAT.

itab1-PAY_AMOUNT = itab2-BETRW.

itab1-PAY_AMOUNT = itab2--BETZG.

itab1-pay_opbel = itab2-opbel.

MODIFY itab1 INDEX idx.

> ENDLOOP.

>

>

> In some cases, the condition LOOP AT itab2 can have >1 records in itab2 which needs to be updated to itab1. Can some one help me to add it to itab1?

>

> thanks

> Dany

Read only

Former Member
0 Likes
2,840

Hi Dan,

I guess you are still working on this one. If you need records to be updated or added to itab1 based on itab2, you may want to try this. But I think giving an example would be the best way to explain the problem.

LOOP AT itab1.

LOOP AT itab2 WHERE budat GE itab1-from_date

AND budat LE itab1-to_date.

move-corresponding itab2 to itab1.

itab1-PAY_DATE = itab2-BUDAT.

itab1-PAY_AMOUNT = itab2-BETRW.

itab1-PAY_AMOUNT = itab2--BETZG.

itab1-pay_opbel = itab2-opbel.

MODIFY itab1.

ENDLOOP.

ENDLOOP.

Read only

0 Likes
2,839

Hi Srinivas Adavi,

I am looking for you

That was working & Well, a new issue popped up now . sometimes there may be > 1 matching record in itab2. The current one you have posted gives sy-subrc ne 0 even for matching records.

so am using loop at itab1 where <.....>

exit.

endloop.

In your last post you have said :

Let us work with an example. Let us say you have one record in itab1 with a from_date of 20070101 and to_date of 20070131. Let us say there are 3 records in itab2 with budats as 20070103, 20070128 and 20070203. Now if I am looping at itab1 and then looping at itab2 where budat is between from_date and to_date, I will get 2 records of itab2 for 1 record of itab1.

So which one should be used to update this one record of itab1? That is the issue here.

If you say you want two records in itab1, one that has 20070103 and the other that has 20070128 as the pay_date, rest all fields of itab1 remaining the same, then your logic becomes complex, as you have to update the first record and append the second record to itab1. You may need another itab of type itab1.

Now suggest accordingly

Read only

0 Likes
2,839

Ok, if that is the case, all you need this additional check.

loop at itab1.

loop at itab2 where <...>.

move-corresponding itab2 to itab1.

move all other fields in your original code here.

if sy-tabix = 1.

modify itab1.

else.

append itab1.

endif.

endloop.

endloop.

Read only

0 Likes
2,839

Ok, if that is the case, all you need this additional check.

loop at itab1.

loop at itab2 where <...>.

move-corresponding itab2 to itab1.

move all other fields in your original code here.

if sy-tabix = 1.

modify itab1.

else.

append itab1.

endif.

endloop.

endloop.

Srinivas,

If he append the itab1 within the loop , those records again taken for processing when loop passes, this is wrong. am i correct?

a®

Read only

0 Likes
2,839

Sorry a®s /Dan, that is true. So you do need a itab3 of type itab1.

Read only

0 Likes
2,839

So it should be this where itab3 is like itab1.

>

> Ok, if that is the case, all you need this additional check.

>

> loop at itab1.

> loop at itab2 where <...>.

> move-corresponding itab2 to itab3.

> move all other fields in your original code here to itab3 fields.

> append itab3.

> endloop.

> endloop.

Read only

0 Likes
2,839

By the way a®s, I don't see the 'code' button any more, how are you achieving indented code segments? Sorry Dan, I know this is unrelated to your issue.

Read only

0 Likes
2,839

Srinivas,

Use curly bracket code curly braket then paste the code then

curly bracket code curly braket

a®s

Edited by: a®s on Jan 4, 2008 4:00 PM

Read only

0 Likes
2,839

You can use 'code' within the curly braces like this:

[code}

put all your code lines here between the code within curly braces

 to see the following:
 

code lines here

Hope this helps.

This is also mentioned in Suggestions thread somewhere...

Thanks

Sanjeev

Read only

0 Likes
2,839

Thank you a®s , Sanjeev.

Read only

0 Likes
2,839

Dan,

Is this working?

DATA: itab3 LIKE itab1 OCCURS 0 WITH HEADER LINE.
LOOP AT itab1.
  LOOP AT itab2 WHERE budat GE itab1-from_date
                  AND budat LE itab1-to_date.
    MOVE-CORRESPONDING itab2 TO itab3.
    itab3-pay_date   = itab2-budat.
    itab3-pay_amount = itab2-betrw.
    itab3-pay_amount = itab2--betzg.
    itab3-pay_opbel  = itab2-opbel.
    APPEND itab3.
  ENDLOOP.
ENDLOOP.

Read only

Former Member
0 Likes
2,839

Hi Dan,

Using field symbols in such situations may reduce the complexity. With field symbols, there is no need to modify the table.


field-symbols: <fs_itab1> like line of itab1,
               <fs_itab2> like line of itab2.

LOOP AT itab1 assigning <fs_itab1>.

  LOOP AT itab2 assigning <fs_itab2> 
   WHERE budat GE <fs_itab1>-from_date 
     AND budat LE <fs_itab1>-to_date.
     if sy-tabix eq 1. "only for the first found record of itab2
      <fs_itab1>-PAY_DATE = <fs_itab2>-BUDAT.
      <fs_itab1>-PAY_AMOUNT = <fs_itab2>-BETRW. 
      <fs_itab1>-PAY_AMOUNT = <fs_itab2>--BETZG.
      <fs_itab1>-pay_opbel = <fs_itab2>-opbel.
    else.
      clear itab1. "use the work area for itab1
      itab1-PAY_DATE = <fs_itab2>-BUDAT.
      itab1-PAY_AMOUNT = <fs_itab2>-BETRW. 
      itab1-PAY_AMOUNT = <fs_itab2>--BETZG.
      itab1-pay_opbel = <fs_itab2>-opbel.
      append itab1.
    endif.
  ENDLOOP.
ENDLOOP.

Hope this helps.

Thanks

Sanjeev

Read only

Former Member
0 Likes
2,839

Dan,

I think that you didn't see my post on page 1 at the bottom.

Please have a look at it and I think it fulfills your need.

Thanks

Sanjeev

Read only

Former Member
0 Likes
2,839

Dan,

I think that you didn't see my post on page 1 at the bottom.

Please have a look at it and I think it fulfills your need.

Thanks

Sanjeev

Read only

Former Member
0 Likes
2,839

Using field symbols in such situations may reduce the complexity. With field symbols, there is no need to modify the table uisng MODIFY statement.


field-symbols: <fs_itab1> like line of itab1,
               <fs_itab2> like line of itab2.
data: itab_tmp like itab1.

" copy itab1 to temp table
itab_tmp[] = itab1[].

LOOP AT itab_tmp assigning <fs_itab1>.
 
  LOOP AT itab2 assigning <fs_itab2> 
   WHERE budat GE <fs_itab1>-from_date 
     AND budat LE <fs_itab1>-to_date.
     if sy-tabix eq 1. "only for the first found record of itab2
      <fs_itab1>-PAY_DATE = <fs_itab2>-BUDAT.
      <fs_itab1>-PAY_AMOUNT = <fs_itab2>-BETRW. 
      <fs_itab1>-PAY_AMOUNT = <fs_itab2>--BETZG.
      <fs_itab1>-pay_opbel = <fs_itab2>-opbel.
    else.
      clear itab1. "use the work area for itab1
      itab1-PAY_DATE = <fs_itab2>-BUDAT.
      itab1-PAY_AMOUNT = <fs_itab2>-BETRW. 
      itab1-PAY_AMOUNT = <fs_itab2>--BETZG.
      itab1-pay_opbel = <fs_itab2>-opbel.
      append itab1.
    endif.
  ENDLOOP.
ENDLOOP.

Here itab1 is appended within the loop itself instead of appending lines from a third table later.

Thanks

Sanjeev

Read only

0 Likes
2,839

Hi Sanjeev Kumar,

You are right but we r not using fiedl symbols..and also i am fixing part of the code here.

Thanks a lot.

Dany

Read only

0 Likes
2,839

Sanjeev,

Please correct me if i am wrong


LOOP AT itab_tmp assigning <fs_itab1>.
endloop.

after this endloop appended records are in ITAB1 and modified records in ITAM_TMP. I think this not correct?

Here is pointing to .itab_tmp .

a®

Read only

0 Likes
2,839

You are right.

Actually I changed the code at the last moment. The correct one is below:


clear itab1[].
loop....
if sy-tabix eq 1. "only for the first found record of itab2
      <fs_itab1>-PAY_DATE = <fs_itab2>-BUDAT.
      <fs_itab1>-PAY_AMOUNT = <fs_itab2>-BETRW. 
      <fs_itab1>-PAY_AMOUNT = <fs_itab2>--BETZG.
      <fs_itab1>-pay_opbel = <fs_itab2>-opbel.
      append <fs_itab1> to itab1.
    else.
....
...
endloop.

<fs_itab1> should also be appended to itab1.

Also make sure to clear the itab1 before populating it using this loop.

At the end itab1 will have correct entries.

As per Dan's requirements - The same can be achieved by replacing field-symbols by work areas. Just replace <fs_itab1> by work area itab1 and <fs_itab2> by work area itab2 and this will work.

Thanks

Sanjeev

Edited by: Sanjeev Kumar on Jan 4, 2008 5:21 PM