Contributor
Posts: 21

# Number range in if statement not working

I am trying to create a variable that is a 1 if it is in a range

My code looks like this:

data new;

set old;

if var1 in (41000:41099) then var2 = 1;

else var2 = 0;

run;

It turns up with only 0's.

When I run this

proc freq data = new;

table var1;

where var1 in (41000:41099);

run;

It shows 50 numbers and hundreds of numbers in the freq.  What am I doing wrong?

Super User
Posts: 9,599

## Re: Number range in if statement not working

Why write it that way in the first place?

data new;

set old;

var2=0;

if 41000 <= var1 <= 41099) then var2=1;

run;

Regular Contributor
Posts: 227

## Re: Number range in if statement not working

allow SAS to do the evaluation; the results are boolean, i.e. (0,1);

var2 = (41000 <= var1 <= 41099);

Posts: 3,852

## Re: Number range in if statement not working

VAR1 must be an integer for IN(41000:41099) specification to match;

data range;
do _n_ = 1 to 50;
r = rannor(
123);
var1 = r*1e2 + 41050;
var2 = var1 in(
41000:41099);
var3 = int(var1) in(41000:41099);
output;

end;

run;
proc print;

run;

Valued Guide
Posts: 864

## Re: Number range in if statement not working

41000 <= var1 <= 41099

New Contributor
Posts: 4

## Re: Number range in if statement not working

I think the problem you are experiencing is not in the code you are writing, which works just fine, but perhaps in the dataset you are reading in.

My hunch is that the user data _null_ could be right: make sure var1 in your dataset is actually a numeric variable.

Otherwise if you still have trouble, I'd suggest giving us a sample of the dataset you are reading in.

Posts: 3,852

## Re: Number range in if statement not working

What is actually happening is further revealed in the SAS log when IN is used in a WHERE statement.

32         proc print;
33            where var1 in(41000:41099);
34            run;

NOTE:
No observations were selected from data set WORK.RANGE.
NOTE: There were
0 observations read from the data set WORK.RANGE.
WHERE (var1=INT(var1)) and (var1>=
41000 and var1<=41099);
Discussion stats
• 6 replies
• 1175 views
• 1 like
• 6 in conversation