Application Development 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: 

Use of inserting record with sy-tabix in at new and at end

former_member209900
Participant
0 Kudos

Hi all,

I have some logical issue while using at new and at end of concept .

My requirement, calculated record will insert on the at end of section.

Pls see my code here.

IT2[] = IT1[]   " it contains more than 50 records.

sort it1 by date plant material type

sort it2 by date plant material type

my input it1,

date             plant    material   type

19072016       A           X         sale

19072016       A           X         stock

19072016       B           X         stock

19072016       C           X         sale

19072016       C           X         stock

 

loop at it1 into wa1

at new matnr.

clear : wa1.

end at.

if wa1-type = 'stock'

wa1-st = value.

else

wal1-sa = value.

endif.

at end matnr.

if wa1-type = 'sale'

sal1-sa =value.

else

wa1-st = value.

endif.

wa1-cal = wa1-st / wa1-sal.

sy tab = sy tab + 1.

insert wa1 to it2 index into sy tabix.

Here, same material have sale and stock means, loop will executed twice and record will inserted third place.

another material only have sale or stock means, loop will executed once and record to be inserted on fifth place.

the above two scenario will come next records.

How it can be achieved. Please help.

1 ACCEPTED SOLUTION

Jelena
Active Contributor
0 Kudos

As Matthew suggested, simply running this through debugger should explain how AT... events work. You'll also see in debugger that inside AT... ENDAT only certain fields have values and others are replaced with the asterisks. These events are tricky.

I didn't get how sy-tabix is involved here exactly or even what are you trying to achieve, sorry. It does seem like you rather need COLLECT but it's not really clear what's the goal here. You might want to clarify.

7 REPLIES 7

matt
Active Contributor
0 Kudos

Your program won't work in any sensible way, since loop at it1 into wa1 causes WA1 to contain exactly the next record in all parts, every time. Your updates in the loop will all be lost.

You must run it in the debugger - then you can see how the data changes, step by step.

Jelena
Active Contributor
0 Kudos

As Matthew suggested, simply running this through debugger should explain how AT... events work. You'll also see in debugger that inside AT... ENDAT only certain fields have values and others are replaced with the asterisks. These events are tricky.

I didn't get how sy-tabix is involved here exactly or even what are you trying to achieve, sorry. It does seem like you rather need COLLECT but it's not really clear what's the goal here. You might want to clarify.

0 Kudos

Ok..

Please see my exact code

FORM month.

DATA : opt1 type i , opt2 type i, opt3 type i, opt4 type i, m TYPE sy-tabix.

   SORT it4 by fkdat werks matnr sal_stk.

   it5[] = it4[].

   LOOP AT IT4 into wa4  .

     clear : m.

   at new matnr.

     clear : opt1, opt2, opt3.

         endat.

   if wa4-fkdat+4(2) = '04'.

     if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-apr.

     else.

       opt2 = wa4-apr.

       endif.

   endif.

        if wa4-fkdat+4(2) = '05'.

    if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-may.

     else.

       opt2 = wa4-may.

     endif.

     endif.

        if wa4-fkdat+4(2) = '06'.

    if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-jun.

     else.

       opt2 = wa4-jun.

     endif.

    endif.

        if wa4-fkdat+4(2) = '07'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-jul.

     else.

       opt2 = wa4-jul.

       endif.

     endif.

        if wa4-fkdat+4(2) = '08'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-aug.

     else.

       opt2 = wa4-aug.

     endif.

     endif.

        if wa4-fkdat+4(2) = '09'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-sep.

     else.

       opt2 = wa4-sep.

     endif.

     endif.

    if wa4-fkdat+4(2) = '10'.

      if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-oct.

     else.

       opt2 = wa4-oct.

     endif.

     endif.

        if wa4-fkdat+4(2) = '11'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-nov.

     else.

       opt2 = wa4-nov.

     endif.

     endif.

        if wa4-fkdat+4(2) = '12'.

        if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-dec.

     else.

       opt2 = wa4-dec.

     endif.

     endif.

        if wa4-fkdat+4(2) = '01'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-jan.

     else.

       opt2 = wa4-jan.

     endif.

     endif.

        if wa4-fkdat+4(2) = '02'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-feb.

     else.

       opt2 = wa4-feb.

     endif.

     endif.

        if wa4-fkdat+4(2) = '03'.

          if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-mar.

     else.

       opt2 = wa4-mar.

     endif.

     endif.

   at end of matnr.

   if wa4-fkdat+4(2) = '04'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '05'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '06'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '07'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '08'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '09'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '10'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

*     read table it4 into wa

   endif.

     if wa4-fkdat+4(2) = '11'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '12'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '01'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '02'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

     if wa4-fkdat+4(2) = '03'.

      IF opt2 <> 0 .

        opt3 = opt1 / opt2" stock / sales

        else.

          opt3 = opt1.

      ENDIF.

   endif.

    if it4 is not initial.

     loop at it5 into wa5 where fkdat = wa4-fkdat and werks = wa4-werks and matnr = wa4-matnr.

     at end of matnr.

        m = sy-tabix.

     endat.

       endloop.

        endif.

      if wa4-fkdat+4(2) = '04'.

      wa5-apr = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '05'.

      wa5-may = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '06'.

      wa5-jun = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '07'.

      wa5-jul = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '08'.

      wa5-aug = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '09'.

      wa5-sep = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '10'.

      wa5-oct = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '11'.

      wa5-nov = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '12'.

      wa5-dec = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '01'.

      wa5-jan = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '02'.

      wa5-feb = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

           if wa4-fkdat+4(2) = '03'.

      wa5-mar = opt3.

      wa5-sal_stk = 'cal'.

      sy-tabix = m + 1.

      endif.

      insert wa5 into it5 index sy-tabix.

   endat.

   endloop.

   ENDFORM.

matt
Active Contributor
0 Kudos

Not a fan of modularisation nor meaningful variable names then? For goodness sake change all those wa3 and it2 to proper meaningful names!

And to modularise, replace each section like:

   if wa4-fkdat+4(2) = '04'.

     if wa4-sal_stk = 'Stock - MT'.

     opt1 = wa4-apr.

     else.

       opt2 = wa4-apr.

       endif.

   endif.


with something like

PERFORM decide_option_values USING '04' wa4-fkdat wa4-apr CHANGING opt1 opt2.


where calc_stock is:

FORM decide_option_values USING i_month_number TYPE n

                                i_date        TYPE d

                                i_month_value TYPE ...

                     CHANGING x_opt1 type ...

                              x_opt2 type ...

   IF i_stock_line-fkdat+4(2) EQ i_month_number.

     IF i_stock_line-sal_stk EQ c_required_stock_type. " constant equal to 'Stock - MT'.

       x_opt1 = i_month_value.

     ELSE.

       x_opt2 = i_month_value.

     ENDIF.

   ENDIF.

ENDFORM.


There are other places you can do similar. Now, once you've got your code into some kind of state resembling readable,


Do what Jelena and I said


....run this through debugger should explain how AT... events work. You'll also see in debugger that inside AT... ENDAT only certain fields have values and others are replaced with the asterisks. These events are tricky.


When you've done that, I'll consider looking at your code if you've still got difficulty.


Modularisation and using meaningful variable names. These are foundational to a successful career in ABAP. They also make your own job much easier, since you can easily see what's going on.

0 Kudos

Thanks for the simplified.

I am very new to the ABAP. Hence, wrote like this.

Hence,my earlier code is working fine.

next, i have another requirement.

I need to calculate the average of previous month.

Example : from date : 10-2012   to date : 08-2013

Will take 07-2013 output,

          Month    plant      material   type    value

          07-2013     A           X          sale    10

          07-2013     A           X          stock   20

          07-2013     A           X          diff (cal) 20

          07-2013     A           X          Average

here average type value have one formula.

from date month - oct to jul , i need to calculate average sales of the material.

then current month stock - avg sales of previous month sales.

Please guide. How to proceed on this.

Please help me to find the previous month records while in current month...

matt
Active Contributor
0 Kudos

If you have a new question, then post a new question. However, calculating averages and the like is basic programming, and out of the scope of this site. Other sites are available.

satyapriyanka_vana
Active Participant
0 Kudos

Hi,

May I know why you are clearing the wa1 at new matnr? (Instead you can copy wa1 to a temporary wok area wa2)

Because after that statement you have few more lines.At the line if wa1-type = 'stock' the wa1 is empty.

Correct me if I am wrong.

Regards,

Priyanka.