11-22-2015 03:54 AM
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.
'24', '30' , '31', '32', '33', '34'
I only want to select the data for Food_Type = '30' - '33'
SAS Code 1:
where Food_Type in ('30' -'33');
SAS Code 2:
where Food_Type in ('30') or Food_Type in ('31') or Food_Type in ('32') or Food_Type in ('33') ;
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'?
11-22-2015 04:49 PM - edited 11-22-2015 04:52 PM
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
11-22-2015 06:11 PM
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.