Your first program does what it asked it to do.
data have;
input var1 $ var2 $ WANT ;
cards;
ROOMA DESK01A 1
ROOMA DESK01B 1
ROOMA DESK05B 5
ROOMA DESK06A 6
ROOMB DESK09A 9
ROOMB DESK09B 9
ROOMC DESK01B 1
ROOMC DESK05A 5
ROOMD DESK10B 10
ROOME DESK15A 15
ROOME DESK15B 15
ROOME DESK16 16
;
%let range_1 = 13;
%let range_2 = 24;
data test_1;
set have;
if compress(var2,'', 'A') >= &range_1.
and compress(var2,'', 'A') <= &range_2.
then test = 'YEAH';
run;
proc print;
run;
Results
Obs
var1
var2
WANT
test
1
ROOMA
DESK01A
1
2
ROOMA
DESK01B
1
3
ROOMA
DESK05B
5
4
ROOMA
DESK06A
6
5
ROOMB
DESK09A
9
6
ROOMB
DESK09B
9
7
ROOMC
DESK01B
1
8
ROOMC
DESK05A
5
9
ROOMD
DESK10B
10
10
ROOME
DESK15A
15
YEAH
11
ROOME
DESK15B
15
YEAH
12
ROOME
DESK16
16
YEAH
But why does it work? Because SAS helpfully automatically converted the strings like '1' and '15' that your COMPRESS() function call generated into numbers:
90 data test_1;
91 set have;
92 if compress(var2,'', 'A') >= &range_1.
93 and compress(var2,'', 'A') <= &range_2.
94 then test = 'YEAH';
95 run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
92:6 93:7
If you want to convert the value to numbers yourself then use the INPUT() function.
data test_2;
set have;
test2 = input(compress(var2,' ','A'),32.);
check= (test2 = want);
run;
Result
Obs
var1
var2
WANT
test2
check
1
ROOMA
DESK01A
1
1
1
2
ROOMA
DESK01B
1
1
1
3
ROOMA
DESK05B
5
5
1
4
ROOMA
DESK06A
6
6
1
5
ROOMB
DESK09A
9
9
1
6
ROOMB
DESK09B
9
9
1
7
ROOMC
DESK01B
1
1
1
8
ROOMC
DESK05A
5
5
1
9
ROOMD
DESK10B
10
10
1
10
ROOME
DESK15A
15
15
1
11
ROOME
DESK15B
15
15
1
12
ROOME
DESK16
16
16
1
Two things to note.
1) I explicitly asked COMPRESS() to remove spaces as well has letters. You might also instead try tell it to only keep digits.
2) I used the 32. informat to read the generated string. The INPUT() function does not care if the width of the informat being used is larger than the length of the string being read. And since 32 is the maximum width that the numeric informat supports it is best to use that when you don't know how long the string being read might be.
... View more