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.
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
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;
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.
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
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;
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;
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;
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) ;
Agreed! Not about the IFx() functions in general, but definitely in this case I concur.
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.
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;
Thanks once again.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.