BookmarkSubscribeRSS Feed
weweaw
Obsidian | Level 7

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?

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why write it that way in the first place?

data new;

     set old;

     var2=0;

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

run;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

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

data_null__
Jade | Level 19

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;

6-4-2015 11-26-55 AM.png
Steelers_In_DC
Barite | Level 11

41000 <= var1 <= 41099

seeLowGreen
Calcite | Level 5

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.

data_null__
Jade | Level 19

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);

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 9511 views
  • 1 like
  • 6 in conversation