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

ABAP Program Question

Former Member
0 Likes
1,742

Hi.

I have made a small program to read material nr. and extrapolate data from MARA-MSTAE into T141-MMSTA when certain conditions are met.

My problem is that I am not able to see material nr., it starts off with material '1000' but then the material field is no longer populated.

This is the code:

REPORT ZRYMTEST.

Tables: MARC, MARA, t141, rmmg1.

Data: xmmsta like t141-mmsta,

xmstae like mara-mstae,

xmatnr like mara-matnr.

Select matnr from mara into xmatnr where matnr NE ' '.

Select mstae from mara into xmstae.

If xmstae EQ '98' or xmstae EQ '99'.

Move xmstae to xmmsta.

Elseif xmstae NE '98' or xmstae ne '99'.

xmmsta = 'NA'.

endif.

clear xmatnr.

clear xmstae.

clear xmmsta.

Endselect.

Endselect.

I am sure that my code for getting material nr. is incorrect but I have not been able to fix it.

Hope you can help.

Sincerely,

Ryan

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
1,619

Your first SELECT loop selects all materials from MARA that aren't blank, into xmatnr.

Your second SELECT loop then runs through ALL MARA records getting the value of mstae.

As it stands your program does nothing but loop through MARA many times, setting variables then repeatedly clearing them.

I'm rather at a loss as to what you expect it to do.


TABLES: marc, mara, t141, rmmg1.

DATA: xmmsta LIKE t141-mmsta,
      xmstae LIKE mara-mstae,
      xmatnr LIKE mara-matnr.

SELECT matnr FROM mara INTO xmatnr WHERE matnr NE ' '. " Start loop through all materials that aren't blank
  SELECT mstae FROM mara INTO xmstae. " Loop through all materials, getting xmstae

* Set xmmsta according to xmstae
    IF xmstae EQ '98' OR xmstae EQ '99'.
      MOVE xmstae TO xmmsta.
    ELSEIF xmstae NE '98' OR xmstae NE '99'.
      xmmsta = 'NA'.
    ENDIF.

* Now clear everything
    CLEAR xmatnr.
    CLEAR xmstae.
    CLEAR xmmsta.

  ENDSELECT. " Continue loop through all materials
ENDSELECT. " Continue loop through all materials that aren't blank

Maybe this gives you an idea

SELECT matnr mstae FROM mara INTO (xmatnr, xmstae) WHERE matnr NE ' '.
  IF xmstae EQ '98' OR xmstae EQ '99'.
    MOVE xmstae TO xmmsta.
  ELSEIF xmstae NE '98' OR xmstae NE '99'.
    xmmsta = 'NA'.
  ENDIF.

* Now do something with xmatnr, xmstae and xmmsta.

  CLEAR xmatnr.
  CLEAR xmstae.
  CLEAR xmmsta.

ENDSELECT.

matt

8 REPLIES 8
Read only

matt
Active Contributor
0 Likes
1,620

Your first SELECT loop selects all materials from MARA that aren't blank, into xmatnr.

Your second SELECT loop then runs through ALL MARA records getting the value of mstae.

As it stands your program does nothing but loop through MARA many times, setting variables then repeatedly clearing them.

I'm rather at a loss as to what you expect it to do.


TABLES: marc, mara, t141, rmmg1.

DATA: xmmsta LIKE t141-mmsta,
      xmstae LIKE mara-mstae,
      xmatnr LIKE mara-matnr.

SELECT matnr FROM mara INTO xmatnr WHERE matnr NE ' '. " Start loop through all materials that aren't blank
  SELECT mstae FROM mara INTO xmstae. " Loop through all materials, getting xmstae

* Set xmmsta according to xmstae
    IF xmstae EQ '98' OR xmstae EQ '99'.
      MOVE xmstae TO xmmsta.
    ELSEIF xmstae NE '98' OR xmstae NE '99'.
      xmmsta = 'NA'.
    ENDIF.

* Now clear everything
    CLEAR xmatnr.
    CLEAR xmstae.
    CLEAR xmmsta.

  ENDSELECT. " Continue loop through all materials
ENDSELECT. " Continue loop through all materials that aren't blank

Maybe this gives you an idea

SELECT matnr mstae FROM mara INTO (xmatnr, xmstae) WHERE matnr NE ' '.
  IF xmstae EQ '98' OR xmstae EQ '99'.
    MOVE xmstae TO xmmsta.
  ELSEIF xmstae NE '98' OR xmstae NE '99'.
    xmmsta = 'NA'.
  ENDIF.

* Now do something with xmatnr, xmstae and xmmsta.

  CLEAR xmatnr.
  CLEAR xmstae.
  CLEAR xmmsta.

ENDSELECT.

matt

Read only

Former Member
0 Likes
1,619

Hi Matt.

Correct, the program does loop in MARA several times,

a) to find the material

b) to find the MSTAE value related to the material and copy it to XMSTAE field and

c) If The XMSTAE value is 98 or 99 then value is copied to XMMSTA field.

but my code might not be correct to get the above data.

The idea is to incorporate the program as an include in main material program so when material is saved with value 98 or 99 is assigned to field MSTAE then value will be copied to field MMSTA.

Ryan

Read only

matt
Active Contributor
0 Likes
1,619

But your inner-loop doesn't do b). It loops through ALL materials, not just the one you selected in the outerloop. You do know that setting a field defined by DATA, does not update the database table? ( Furthermore, it is extremely ill-advised to modify a standard SAP table ).

I strongly suggest you read the ABAP help on the SELECT and DATA statements. There seems to be a profound misunderstanding on how ABAP works.

matt

Read only

Former Member
0 Likes
1,619

Matt,

you are right but I am not a programmer therefore the profound misunderstanding that you refer to. Thanks for the advice will look at the Help.

As for the code, I have adopted your solution after testing it as it seems to work the way I had intended.

I included a statement so that when t141-mmsta is blank and xmstae value is 98 or 99 then the value is copied over.

Will have to test it further but thanks for all the help.

Ryan

Read only

matt
Active Contributor
0 Likes
1,619

>

> Matt,

>

> you are right but I am not a programmer therefore the profound misunderstanding that you refer to.

That's explains it!

btw, once you've got the value you want, you might want to use EXIT to exit the loop.

matt

Read only

Former Member
0 Likes
1,619

OK will add EXIT after endselect statement.

Thanks for the advice.

Now I just need to figure out where to include it in the main program. 🐵

Read only

Former Member
0 Likes
1,619

Hi,

I'm not sure what you are trying to do, but your coding isn't ok. It should be something like:

REPORT ZRYMTEST.

Data:

xmmsta like t141-mmsta,

zls_mara TYPE mara.

Select *

from mara

into zls_mara

where matnr NE ' '.

If zls_mara-mstae EQ '98' or

zls_mara-mstae EQ '99'.

Move zls_mara-mstae to xmmsta.

Else.

xmmsta = 'NA'.

endif.

Endselect.

Regards,

John.

Read only

0 Likes
1,619

Hi John.

Thank you for your help. Matts solution is more akin to what I had in mind though.

Sincerely

Ryan