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?
Why write it that way in the first place?
data new;
set old;
var2=0;
if 41000 <= var1 <= 41099) then var2=1;
run;
allow SAS to do the evaluation; the results are boolean, i.e. (0,1);
var2 = (41000 <= var1 <= 41099);
VAR1 must be an integer for IN(41000:41099) specification to match;
41000 <= var1 <= 41099
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.
What is actually happening is further revealed in the SAS log when IN is used in a WHERE statement.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.