Fluorite | Level 6

## IN operator range for Charater values

Hi,

I have a character variable with values like '100.0', '100.1', ...

I wanted to flag them if the values are 200.0 to 230.9 and 250.0 to 260.9 ......

I wanted to know if we can specify range while using IN operator upon Character variable?

Sandy.

12 REPLIES 12
Lapis Lazuli | Level 10

## Re: IN operator range for Charater values

data have;

informat v \$5.;

input v @@;

cards;

100.0 200.0 230.1 240.0 250.1 260.9 280.8

;

data want;

set have;

if input(v,3.) in (200,210,220,230,250,260) then flag=1;

else flag=0;

proc print;run;

v       flag

1     100.0      0

2     200.0      1

3     230.1      1

4     240.0      0

5     250.1      1

6     260.9      1

7     280.8      0

Opal | Level 21

## Re: IN operator range for Charater values

Not with the in operator, but couldn't you use something like:

data have;

informat v \$5.;

input v @@;

cards;

100.0 200.0 230.1 240.0 250.1 260.9 280.8

;

data want;

set have;

if '200.0' le v le '230.9' or

'250.0' le v le '260.9' then flag=1;

else flag=0;

run;

Opal | Level 21

## Re: IN operator range for Charater values

You have to know something about your data to do this.  If you have unexpected values, you will get unexpected results.  For example, will your variable ever take on values like this:

200

21ABC

Assuming your data actually do behave, Art's idea of using ranges works just fine.  Here is how to do it using the IN operator:

if v in: ('20', '21', '22, '230', '25', '260') then flag=1;

else flag=0;

The colon after IN says that the comparison should be based on whichever string is shorter (either V or one of the quoted strings).

Good luck.

Obsidian | Level 7

## Re: IN operator range for Charater values

Hi,

I have a question on IN operator. I'm using in operator but not  getting all the values. e.g

if State in ('NJ', 'New Jersey');

then I'm getting only NJ in the output and not New Jersey. Can you suggest a better way to get both of the values.

Thank you

M

Opal | Level 21

## Re: IN operator range for Charater values

Ok!  Here is how you can do it using the IN operator:

data have;

informat v \$5.;

input v @@;

cards;

100.0 200.0 230.1 240.0 250.1 260.9 280.8

;

data want;

set have;

if input(v,32.)*10 in (2001:2309) or

input(v,32.)*10 in (2500:2609) then flag=1;

else flag=0;

run;

Opal | Level 21

## Re: IN operator range for Charater values

Art,

Always time to play with a new toy ...   I found you can combine ranges:

if input(v,??32.)*10 in (2001:2309, 2500:2609) then flag=1;

Opal | Level 21

## Re: IN operator range for Charater values

Nice!  Then we should continue to play and reduce everything to one statement:

data have;

informat v \$5.;

input v @@;

cards;

100.0 200.0 230.1 240.0 250.1 260.9 280.8

;

data want;

set have;

flag=ifn(input(v,?? 32.)*10 in (2001:2309,2500:2609),1,0);

run;

Super User

## Re: IN operator range for Charater values

I am not a big fan of the IFx() functions. To me it is much easier to read IF/THEN/ELSE statements and I suspect that after compilation SAS is doing the exact same code.  But for storing logical expression results as 0 or 1 you do not need either.

flag = input(v,??32.)*10 in (2001:2309, 2500:2609) ;

Opal | Level 21

## Re: IN operator range for Charater values

Agreed!  Not about the IFx() functions in general, but definitely in this case I concur.

Fluorite | Level 6

## Re: IN operator range for Charater values

Thanks guys.  It was very helpful.  It worked out well for me.  I used IFx.

But still I curious on how to give range in the IN logical operator for character values.  For numeric values we can use the below code.

A IN (1:5);

instead of A IN (1, 2, 3, 4, 5);

I would like to know the equivalent for Character values?

Thanks once again.

Opal | Level 21

## Re: IN operator range for Charater values

I don't think that you can.  The closest approximation that I can think of would be to use a format.  e.g.:

proc format;

value \$chars

'200.1'-'239.9',

'250.0'-'260.9'=1

other=0;

run;

data want;

set have;

flag = put(v,\$chars.);

run;

Fluorite | Level 6

## Re: IN operator range for Charater values

Thanks once again.

Discussion stats
• 12 replies
• 6242 views
• 11 likes
• 6 in conversation