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: 

if statement with IN and bracket

Former Member

Dear forum,

can the below if statement

if p_waers = 'EUR' or p_waers = 'AUD' or p_waers = 'NZD' or p_waers = 'GBP'.

change to

if p_waers IN ( 'EUR' , 'AUD' , 'NZD' ) ?

i tried but not possible. why?

thanks

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor

In ABAP it is not possible this way. What I usually do instead is


case p_wears.
   when 'EUR' or 'AUD' or 'NZD' or 'GBP'.
      ...
   when others.
     ..
endcase.

Regards

Marcin

10 REPLIES 10

Former Member
0 Kudos

Hello

It is because you do not read help for Logical Expressions - Comparison with a Selection Criterion

You need to create ranges for waers, fill it with your waers and in IF statement compare with ranges.

vinod_vemuru2
Active Contributor

Hi,

You can populate all the values in to a range variable and then you can use IN. Answer for Why is, it is the way ABAP language syntax was created by SAP

r_waers-sign = 'I'.

r_waers-option = 'EQ'.

r_waers-low = 'EUR'.

APPEND r_waers.

r_waers-low = 'AUD'.

APPEND r_waers.

r_waers-low = 'NZD'.

APPEND r_waers.

IF p_waers IN r_waers.

Thanks,

Vinod.

MarcinPciak
Active Contributor

In ABAP it is not possible this way. What I usually do instead is


case p_wears.
   when 'EUR' or 'AUD' or 'NZD' or 'GBP'.
      ...
   when others.
     ..
endcase.

Regards

Marcin

Former Member

the IN logical operator requires a comparision against a select-option type structure, so would require something like



data: l_waers type waers.

select-options s_waers for l_waers no-display.


s_waers-sign   = 'I'. 
s_waers-option = 'EQ'. 
s_waers-low    = 'EUR'. 
APPEND s_waers TO s_waers.

s_waers-sign   = 'I'. 
s_waers-option = 'EQ'. 
s_waers-low    = 'AUD'. 
APPEND s_waers TO s_waers.

s_waers-sign   = 'I'. 
s_waers-option = 'EQ'. 
s_waers-low    = 'NZD
APPEND s_waers TO s_waers.

if p_waers in s_waers.



endif.

However a quicker alternative may be


if 'EUR AUD NZD' CS p_waers then.

endif.

former_member194797
Active Contributor
0 Kudos

replace

if p_waers IN ( 'EUR' , 'AUD' , 'NZD' ) 

with

if p_waers IN ('EUR','AUD','NZD') 

(same code without spaces inside the brackets)

0 Kudos

Hi Henri,

I was surprised at this solution but I thought I would give it a go.

I find that this code does not compile:

if p_waers IN ('EUR','AUD','NZD').

with or without spaces.

The best solution here is to use a ranges object as correctly describes.

Unfortunately  it is not always recommended to use a case statement in this situation.

0 Kudos

Depends on the intended usage of IN keyword.

IN keyword can be used with values like IN ('EUR','AUD','NZD') or selection table.

Within an IF, IN can be used if you are using selection table, and not if you are using values list.

Within SELECT..WHERE, IN ('EUR','AUD','NZD') will work.

Creating a range table for matching a fixed list of values in IF statement isn't worth the effort.

0 Kudos

Yes abap is a little inconsistent at the best of times.

Whether a range table is worth the effort is a case by case basis. I would say it is much better than a case statement given that there is no breaking from a case and fall through that you get in a c switch.

Former Member
0 Kudos

The reason why IN keyword does not work for you is that it can be used with WHERE keyword, but not with IF keyword.

Former Member
0 Kudos

hI,

Its always recommended to use CASE ... END CASE in this situation..

Thanks

Suraj