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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.