BookmarkSubscribeRSS Feed
Sandhya
Fluorite | Level 6

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?

Thanks in advance,

Sandy.

12 REPLIES 12
Linlin
Lapis Lazuli | Level 10

is the example helpful?

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

art297
Opal | Level 21

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;

Astounding
PROC Star

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.

Malathi13
Obsidian | Level 7

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

art297
Opal | Level 21

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;

Astounding
PROC Star

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;

art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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) ;


art297
Opal | Level 21

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

Sandhya
Fluorite | Level 6

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.

art297
Opal | Level 21

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;

Sandhya
Fluorite | Level 6

Thanks once again.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 7630 views
  • 12 likes
  • 6 in conversation