<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculation in Loop at ITAB in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972184#M1158754</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If u want to sum based on PERNR and DATE, then declare another internal table with fields PERNR DATE and ENT_AMT, now move the corresponding fields of ITAB data to this table, and LOOP this table and sum.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Eg: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA : begin of itab1 occurs 0.&lt;/P&gt;&lt;P&gt;              pernr&lt;/P&gt;&lt;P&gt;              date&lt;/P&gt;&lt;P&gt;              ent_amt&lt;/P&gt;&lt;P&gt;end of&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LOOP at itab.&lt;/P&gt;&lt;P&gt;  move-corresponding itab to itab1.&lt;/P&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SORT itab1 by pernr date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;loop at itab1.&lt;/P&gt;&lt;P&gt;   XXXXX = XXXXX + itab1-emt_amt.&lt;/P&gt;&lt;P&gt;   &lt;/P&gt;&lt;P&gt;  at end of date.&lt;/P&gt;&lt;P&gt;    clear XXXXX.&lt;/P&gt;&lt;P&gt;endat.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope it helps!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Pavan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 16 Jan 2009 03:57:43 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2009-01-16T03:57:43Z</dc:date>
    <item>
      <title>Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972183#M1158753</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i have multiple values in ITAB for multiple PERNR, now i have to do calculation of amount on date basis.&lt;/P&gt;&lt;P&gt;but i m confuse that how wud i do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB : &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PERNR      LGA01     BET01      BEGDA           BETRG     ENT_AMT&lt;/P&gt;&lt;P&gt;8430         4004         5004        16.07.2007 &lt;/P&gt;&lt;P&gt;8254         4004         5004        19.07.2006 &lt;/P&gt;&lt;P&gt;8254         4004         10000       01.04.2008&lt;/P&gt;&lt;P&gt;7145         4004         5004         01.02.2006 &lt;/P&gt;&lt;P&gt;7145         4004         10000       01.04.2007 &lt;/P&gt;&lt;P&gt;7145         4004         20000       01.04.2008&lt;/P&gt;&lt;P&gt;......&lt;/P&gt;&lt;P&gt;.......&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now i have to calculate ENT_AMT with respect to PERNR and respective date. i have to take difference &lt;/P&gt;&lt;P&gt;first BEGDA to second BEGDA in months and have to multiply it with BET01. and at end of every PERNR, BEGDA must be subtracted by SY_DATUM but when i m doing for first PERNR then its going wrong for second PERNR. i m getting confused. please help me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards saurabh.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 03:45:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972183#M1158753</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T03:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972184#M1158754</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If u want to sum based on PERNR and DATE, then declare another internal table with fields PERNR DATE and ENT_AMT, now move the corresponding fields of ITAB data to this table, and LOOP this table and sum.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Eg: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA : begin of itab1 occurs 0.&lt;/P&gt;&lt;P&gt;              pernr&lt;/P&gt;&lt;P&gt;              date&lt;/P&gt;&lt;P&gt;              ent_amt&lt;/P&gt;&lt;P&gt;end of&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LOOP at itab.&lt;/P&gt;&lt;P&gt;  move-corresponding itab to itab1.&lt;/P&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SORT itab1 by pernr date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;loop at itab1.&lt;/P&gt;&lt;P&gt;   XXXXX = XXXXX + itab1-emt_amt.&lt;/P&gt;&lt;P&gt;   &lt;/P&gt;&lt;P&gt;  at end of date.&lt;/P&gt;&lt;P&gt;    clear XXXXX.&lt;/P&gt;&lt;P&gt;endat.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope it helps!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Pavan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 03:57:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972184#M1158754</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T03:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972185#M1158755</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Saurabh,&lt;/P&gt;&lt;P&gt;Could you explain your scenario with any sample entries?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Manoj Kumar P&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 03:59:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972185#M1158755</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T03:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972186#M1158756</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;you can try like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;get pernr.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;do calculation here .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;end of selection .&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 04:02:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972186#M1158756</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T04:02:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972187#M1158757</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanx for reply,&lt;/P&gt;&lt;P&gt;Suppose i have the following values in itab, then when i apply the loop on that then it will go for first PERNR - 8430, bcose of single entry of PERNR it must subtract the date 16.07.2007 from SY-DATUM&lt;/P&gt;&lt;P&gt;and then get the value in month after that month value shud multiply with BET01 and then update it &lt;/P&gt;&lt;P&gt;in ENT_AMT field in same row.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;after that when we go for second time loop then it will get two value of PERNR-8254, now for first row of this PERNR it will subtract the date like (01.04.2008 - 19.07.2006) and get the value in month then multiply month value with 5000 and update it ENT_AMT in first row of PERNR-8254. now for second row of this PERNR it will subtract the date 01.04.2008 from SY-DATUM and get the value in month and multiply it with 10000 and update it in ENT_AMT.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now continue the same process for next pernr.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB : &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PERNR  LGA01 BET01 BEGDA BETRG ENT_AMT&lt;/P&gt;&lt;P&gt;8430 4004 5004 16.07.2007 &lt;/P&gt;&lt;P&gt;8254 4004 5000 19.07.2006 &lt;/P&gt;&lt;P&gt;8254 4004 10000 01.04.2008&lt;/P&gt;&lt;P&gt;7145 4004 5004 01.02.2006 &lt;/P&gt;&lt;P&gt;7145 4004 10000 01.04.2007 &lt;/P&gt;&lt;P&gt;7145 4004 20000 01.04.2008&lt;/P&gt;&lt;P&gt;......&lt;/P&gt;&lt;P&gt;.......&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;can anyone please help me that how wud i do?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards saurabh.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: saurabh srivastava on Jan 16, 2009 7:00 AM&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: saurabh srivastava on Jan 16, 2009 7:12 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 05:37:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972187#M1158757</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T05:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972188#M1158758</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;can u show ur code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This may help us.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 06:19:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972188#M1158758</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T06:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972189#M1158759</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;in the Loop, use the event.. at end of pernr, and then in that subtract the value. Wirte like this in the loop:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;at end of pernr.
subtract.

" here give the field name which you want to subtract.
multiply.

"here give the field names which you want to multiply.
endat.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vishwa.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 06:22:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972189#M1158759</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T06:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972190#M1158760</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Saurabh, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Nice requirement &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SORT itab BY pernr begda ASCENDING.

LOOP AT itab ASSIGNING &amp;lt;fs&amp;gt;.
  v_index = sy-index + 1.
  READ TABLE itab INTO wa INDEX v_index.
  IF sy-subrc = 0.
    IF wa-pernr = &amp;lt;fs&amp;gt;-pernr.
      CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
        EXPORTING
          i_datum_bis = wa-begda
          i_datum_von = &amp;lt;fs&amp;gt;-begda
        IMPORTING
          e_monate    = v_months.
      IF sy-subrc = 0.
        &amp;lt;fs&amp;gt;-ent_amt = &amp;lt;fs&amp;gt;-bet01 * v_months.
        CLEAR v_months.
      ENDIF.

    ELSE.
      CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
        EXPORTING
          i_datum_bis = sy-datum
          i_datum_von = &amp;lt;fs&amp;gt;-begda
        IMPORTING
          e_monate    = v_months.
      IF sy-subrc = 0.
        &amp;lt;fs&amp;gt;-ent_amt = &amp;lt;fs&amp;gt;-bet01 * v_months.
        CLEAR v_months.
      ENDIF.

    ENDIF.
  ELSE. "For the last record of the internal table
    CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
      EXPORTING
        i_datum_bis = sy-datum
        i_datum_von = &amp;lt;fs&amp;gt;-begda
      IMPORTING
        e_monate    = v_months.
    IF sy-subrc = 0.
      &amp;lt;fs&amp;gt;-ent_amt = &amp;lt;fs&amp;gt;-bet01 * v_months.
      CLEAR v_months.
    ENDIF.
  ENDIF.
ENDLOOP.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try this code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Suhas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 06:29:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972190#M1158760</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2009-01-16T06:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972191#M1158761</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Saurabh,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Check the below code which will work for your requirement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SORT it_tab BY pernr begda .

LOOP AT it_tab INTO wa_tab.
  w_ind = sy-tabix. "Current entry
  w_ind1 = sy-tabix + 1. "Next entry

  READ TABLE it_tab INTO wa_tab1 INDEX w_ind1. "Read the next value
  IF sy-subrc EQ 0 AND wa_tab-pernr EQ wa_tab1-pernr. "If an entry there and pernr are same
                                                                        "Calculate the difference date diff b/w 2 dates
    CALL FUNCTION 'HR_99S_MONTHS_BETWEEN_DATES'
         EXPORTING
              p_begda  = wa_tab-begda
              p_endda  = wa_tab1-begda
         IMPORTING
              p_months = w_diff.
  ELSE."If there is no entry or pernr are not same calculate the difference date diff b/w date and sy-datum
                                                                                CALL FUNCTION 'HR_99S_MONTHS_BETWEEN_DATES'
         EXPORTING
              p_begda  = wa_tab-begda
              p_endda  = sy-datum
         IMPORTING
              p_months = w_diff.


  ENDIF.

  wa_tab-ent_amt = wa_tab-bet01 * w_diff. "Multyplly with the difference

  MODIFY it_tab FROM wa_tab INDEX w_ind TRANSPORTING ent_amt. "Modify the current entry
  CLEAR: wa_tab1, w_diff.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;I/P:

7145 4004          5.004,00  01.02.2006 
7145 4004         10.000,00  01.04.2007
7145 4004         20.000,00  01.04.2008
8254 4004          5.000,00  19.07.2006
8254 4004         10.000,00  01.04.2008 
8430 4004          5.004,00  16.07.2007

O/P:

7145 4004          5.004,00  01.02.2006         70.056,00
7145 4004         10.000,00  01.04.2007        120.000,00
7145 4004         20.000,00  01.04.2008        200.000,00
8254 4004          5.000,00  19.07.2006        100.000,00
8254 4004         10.000,00  01.04.2008        100.000,00
8430 4004          5.004,00  16.07.2007         90.072,00&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Manoj Kumar P&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 06:59:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972191#M1158761</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T06:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972192#M1158762</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi rajkumar,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i m sending u the codes plz. see and help us.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;TABLES : pa0000,
         pa0015 ,   "HR Master Record: Infotype 0015 (Additional Payments)
         pa0589,
         pa0001 .   "HR Master Record: Infotype 0001 (Org. Assignment)

DATA : BEGIN OF itab OCCURS 0 ,
       pernr LIKE pa0000-pernr ,                       "Personel no.
       lga01 LIKE pa0589-lga01,
       bet01 LIKE pa0589-bet01,
       begda LIKE pa0589-begda ,                       "Lta start date

       lgart LIKE pa0015-lgart,                        "SUBTYPE OF INFOTYPE
       endda LIKE pa0015-endda ,                       "LTA Taken Date
       betrg LIKE pa0015-betrg,                        "LTA Amount
       zuord LIKE pa0015-zuord,                        "Assignment number
*  *       z_designation LIKE pa0001-z_designation ,       "Designation
       ename LIKE pa0001-ename ,                       "Employee name
       ent_amt type p decimals 2,
       pending TYPE p DECIMALS 2,                      "Pending LTA amount
*       amt type i,
 END OF itab .

DATA : BEGIN OF wa_itab,
       pernr LIKE pa0000-pernr ,                       "Personel no.
       ename LIKE pa0001-ename ,                       "Employee name
       lga01 LIKE pa0589-lga01,
       bet01 LIKE pa0589-bet01,
       begda LIKE pa0589-begda ,                       "Lta start date

       lgart LIKE pa0015-lgart,                        "SUBTYPE OF INFOTYPE
       endda LIKE pa0015-endda ,                       "LTA Taken Date
       betrg LIKE pa0015-betrg,                        "LTA Amount
       zuord LIKE pa0015-zuord,                        "Assignment number

 END OF wa_itab.
DATA : a TYPE d,
       b TYPE d,
       c type i,
       e type i,
      amt type i,
      amt2 type i,
      amt3 type i,
      flag type c,
      flag2 type c.

DATA : cnt TYPE i,
       cnt1 type i.
DATA : v_index TYPE sy-tabix.
**------------------------------  ALV DECLERATION-----------------------------------------------
DATA : w_container TYPE scrfname VALUE 'CL_GRID',
       w_cprog TYPE lvc_s_layo,
       g_repid LIKE sy-repid,
       w_save TYPE c,
       w_exit TYPE c,
       cl_grid TYPE REF TO cl_gui_alv_grid,
       cl_custom_container TYPE REF TO cl_gui_custom_container,
       it_fld_catalog TYPE slis_t_fieldcat_alv,
       wa_fld_catalog TYPE slis_t_fieldcat_alv WITH HEADER LINE ,
       layout TYPE slis_layout_alv,
       col_pos  LIKE sy-cucol ,
       alvfc TYPE slis_t_fieldcat_alv.
***------------------------------------ SELECTION SCREEN

SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE text-001 .
SELECT-OPTIONS : pernr FOR pa0000-pernr .
SELECT-OPTIONS : begda FOR pa0589-begda no intervals no-extension.

SELECTION-SCREEN : END OF BLOCK b1 .
**********************************************
PERFORM fill_catalog1 USING:

'PERNR'    'ITAB'    'Personal No.',
'ENAME'    'ITAB'    'Emp Name',
*'Z_DESIGNATION'    'ITAB'    'Designation',
'BEGDA'    'ITAB'    'LTA Assign Date',
'LGA01'    'ITAB'    'Wage Type',
'BET01'    'ITAB'    'Limit Amount',

'ENT_AMT'    'ITAB'    'Entitled amount',
'BETRG'    'ITAB'    'LTA Availed Amount',
'ZUORD'    'ITAB'    'Assignment Number',
'PENDING'  'ITAB'    'Pending LTA Amount'.
***********************************************
at selection-screen.

if begda-low is initial.
begda-low = sy-datum.
endif.

start-of-selection.

SELECT m1~pernr m3~lga01 m3~bet01 m3~begda INTO TABLE itab
                        FROM pa0000 AS m1 INNER JOIN pa0589 AS m3 ON m1~pernr = m3~pernr
                       WHERE m1~pernr IN pernr AND m1~endda = '99991231' AND m1~stat2 = '3'
                       AND m3~lga01 = '4004'. " and m3~begda in begda.
IF sy-subrc &amp;lt;&amp;gt; 0.
  MESSAGE 'Data not found, Please Enter Active Employee Only.' TYPE 'I'.
ENDIF.

SORT itab BY pernr.

flag = 'X'.

flag2 = 'X'.


LOOP AT itab.
*************************************
if flag2 = 'X'.
at new pernr.
cnt1 = sy-tabix.
if cnt1 = 1.
a = itab-begda.
b = begda-low.
e = itab-begda+6(2).
 if e lt 16.
 itab-begda+6(2) = '01'.
 a = itab-begda.
 else.
 itab-begda+4(2) =  itab-begda+4(2) + 1.
 itab-begda+6(2) = '01'.

 a = itab-begda.
endif.
amt = itab-bet01 / 12.
CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
    EXPORTING
      i_datum_bis         = b
      i_datum_von         = a
*     I_KZ_INCL_BIS       = ' '
   IMPORTING
     E_MONATE            = c
            .

       amt = amt * c.
       itab-ent_amt = amt.
    SELECT SUM( betrg ) FROM pa0015 INTO itab-betrg WHERE pernr = itab-pernr AND lgart = '4004' AND begda gt a and begda lt b .

       modify itab index cnt1 transporting betrg ent_amt.
endif.
endat.
clear flag2.
******************************
else.
if flag = 'X'.
  IF sy-tabix = 1.                                       "for first row of first pernr
  cnt = sy-tabix.
    e = itab-begda+6(2).
 if e lt 16.
 itab-begda+6(2) = '01'.
 a = itab-begda.
 else.
 itab-begda+4(2) =  itab-begda+4(2) + 1.
 itab-begda+6(2) = '01'.

 a = itab-begda.
endif.
    amt = itab-bet01 / 12.
    CONTINUE.
** else.
** a = itab-begda.
** amt = itab-bet01 / 12.

  ENDIF.
**************************************************************
  b = itab-begda.                                        "for second row of first pernr
  amt2 = itab-bet01 / 12.
  CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
    EXPORTING
      i_datum_bis         = b
      i_datum_von         = a
*     I_KZ_INCL_BIS       = ' '
   IMPORTING
     E_MONATE            = c
            .

       amt = amt * c.
       itab-ent_amt = amt.
    SELECT SUM( betrg ) FROM pa0015 INTO itab-betrg WHERE pernr = itab-pernr AND lgart = '4004' AND begda gt a and begda lt b .

       modify itab index cnt transporting betrg ent_amt.
     clear flag.
**************for third row of pernr
  else.
 cnt = cnt + 1.
 a = b.
 b = itab-begda.
* amt2 = itab-bet01 / 12.
amt3 = itab-bet01 / 12.

 CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
    EXPORTING
      i_datum_bis         = b
      i_datum_von         = a
*     I_KZ_INCL_BIS       = ' '
   IMPORTING
     E_MONATE            = c
            .

amt3 = amt3 * c.
itab-ent_amt = amt3.
*amt3 = itab-bet01 / 12.
SELECT SUM( betrg ) FROM pa0015 INTO itab-betrg WHERE pernr = itab-pernr AND lgart = '4004' AND begda gt a and begda lt b.
 modify itab index cnt transporting betrg ent_amt.
endif.

***************************for last row of pernr
   at end of pernr.
   cnt = cnt + 1.
   clear sy-tabix.
   clear a.
   clear c.
*   clear amt2.
   clear itab-betrg.
   a = b.

*   amt2 = itab-bet01 / 12.

   CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
    EXPORTING
      i_datum_bis         = begda-low
      i_datum_von         = a
*     I_KZ_INCL_BIS       = ' '
   IMPORTING
     E_MONATE            = c
            .

 amt2 = amt2 * c.
       itab-ent_amt = amt2.
         SELECT SUM( betrg ) FROM pa0015 INTO itab-betrg WHERE pernr = itab-pernr AND lgart = '4004' AND begda gt a and begda lt begda-low.

       modify itab index cnt transporting betrg ent_amt.

endat.
*endat.
endif.
ENDLOOP.


************************************************************************
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program = 'ZLTA_PENDING_TEST'
    is_layout          = layout
    it_fieldcat        = it_fld_catalog
    i_default          = 'X'
    i_save             = 'A'
  TABLES
    t_outtab           = itab
  EXCEPTIONS
    program_error      = 1
    OTHERS             = 2.
IF sy-subrc &amp;lt;&amp;gt; 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*ENDFORM.
FORM fill_catalog1  USING   p_fieldname TYPE any
                            p_ref_table TYPE any
                            p_scrtext   TYPE any.
  CLEAR : wa_fld_catalog.
  wa_fld_catalog-fieldname  = p_fieldname.
  wa_fld_catalog-tabname    = p_ref_table.
  wa_fld_catalog-seltext_s  = p_scrtext.
  wa_fld_catalog-seltext_m  = p_scrtext.
  wa_fld_catalog-seltext_l  = p_scrtext.
  APPEND wa_fld_catalog TO it_fld_catalog.

ENDFORM.                    " fill_catalog1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards saurabh.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Julius Bussche on Jan 16, 2009 1:34 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 07:02:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972192#M1158762</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T07:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972193#M1158763</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi manoj,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanx for reply, that was really nice logic. but now i need to display sum of ENT_AMT for every PERNR or u can say PERNR wise. so please tell that how wud i do?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards.&lt;/P&gt;&lt;P&gt;saurabh.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 10:24:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972193#M1158763</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T10:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation in Loop at ITAB</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972194#M1158764</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Saurabh,&lt;/P&gt;&lt;P&gt;Decalre new int table of structure it_tab say its name is it_tab1 and declare a variable w_sum of for ent_amt, then use the below logic after the logic i have mentioned in my last reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLEAR w_sum.
LOOP AT it_tab INTO wa_tab.
  ADD wa_tab-ent_amt TO w_sum.
  AT END OF pernr.
    wa_tab1-pernr = wa_tab-pernr.
    wa_tab1-ent_amt = w_sum.
    CLEAR w_sum.
"New table contains only pernr and ent_amt if you want you can add other fields acco to req
    APPEND wa_tab1 TO it_tab1. 
  ENDAT.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now the table it_tab1 contains pernr wise sum of ent_amt.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Manoj Kumar P&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Manoj Kumar on Jan 16, 2009 12:22 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Jan 2009 11:22:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/calculation-in-loop-at-itab/m-p/4972194#M1158764</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-01-16T11:22:05Z</dc:date>
    </item>
  </channel>
</rss>

