Help using Base SAS procedures

IN operator range for Charater values

Reply
Contributor
Posts: 57

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?

Thanks in advance,

Sandy.

Super Contributor
Posts: 1,636

Re: IN operator range for Charater values

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

PROC Star
Posts: 7,467

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;

Super User
Posts: 5,495

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.

PROC Star
Posts: 7,467

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;

Super User
Posts: 5,495

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;

PROC Star
Posts: 7,467

Re: IN operator range for Charater values

Posted in reply to Astounding

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
Super User
Posts: 7,035

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


PROC Star
Posts: 7,467

Re: IN operator range for Charater values

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

Contributor
Posts: 57

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.

PROC Star
Posts: 7,467

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;

Contributor
Posts: 57

Re: IN operator range for Charater values

Thanks once again.

Ask a Question
Discussion stats
  • 11 replies
  • 463 views
  • 11 likes
  • 5 in conversation