BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

Hi

 

Iam using SAS 7.1

 

I am quite new to SAS, so pardon if my question sounds basic.

 

i have a range of character variable.

EG:

 

Food_Type

 

'24',  '30' , '31', '32', '33', '34'

 

I only want to select the data for Food_Type = '30' - '33'

 

 

SAS Code 1:

 

data test;

set test;

where Food_Type in ('30' -'33');

run;

 

SAS Code 2:

 

data test;

set test;

where Food_Type in ('30') or Food_Type in ('31') or Food_Type in ('32') or Food_Type in ('33') ;

run;

 

SAS Code 1 doesn't work but SAS code 2 does. However, it is very tedious to type out all the Food_Type values I want. Is there a faster way I could select '30' - '33'?

 

Thank you

6 REPLIES 6
kannand
Lapis Lazuli | Level 10

Try this:

 

where '30' LE Food_Type LE '33';

 

 

Hope it helps.... Good Luck...!!!

Kannan Deivasigamani
slchen
Lapis Lazuli | Level 10

data want;

     set have;

     where food_type between '30' and '33';   *or where  '30'<=food_type<='33';

run;

 

 

  

Patrick
Opal | Level 21
where food_type in ('24',  '30' , '31', '32', '33', '34')

 

Even if only dealing with digits don't use ranges with character variables as you might get unexpected results like below:

 

data _null_;
  if '99'>'100' then put '99>100';
  else put '99<=100';
run;

Result in log:
99>100

Astounding
PROC Star

While you have many solutions that will work with the example you gave, heed Patrick's warning.  There are many cases where you would run into trouble.  For example, if your FOOD_TYPE variable is actually 3 characters long, the solutions proposed would also include '301'.  That falls within the range of "30" to "33".

 

Once your first digit can change, the problems get worse.  For example, if you tried this:

 

where ('39' <= food_type <= '41');

 

That range also includes a few values you might not want, such as "4" and either "3F' or "4F" (depends on the operating system).

 

You will get by in this particular case, but you will need to understand how SAS compares character strings to be safe in the future.

 

 

gergely_batho
SAS Employee

This works even when your first digit changes:

where   30 <=input(Food_Type, 3.) <= 100

 

Steelers_In_DC
Barite | Level 11

This selects the range, including 30 and 33:

 

data want;
set have;
where food_type between 30 and 33;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1751 views
  • 4 likes
  • 7 in conversation