Hello
I am using select when in data set (I know that I can do it easily with IF but I wan to learn it ).
There is an error with "when(between 11 and 100) Ind='10-100';"
What is the correct way to write it please?
The error is
__
22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, *, **, +, ',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE,
GT, LE, LT, MAX, MIN, NE, NG, NL, OR, [, ^=, {, |, ||, ~=.
data tbl2;
set tbl1;
length Ind $10;
select(X);
when(0)Ind='0';
when(1,2,3) Ind='1-3';
when(4,5,6)Ind='4-6';
when(7,8,9,10)Ind='7-10';
when(between 11 and 100) Ind='10-100';
otherwise Ind='100+';
end;
run;
"Between " is seems to be not available. Instead use:
when(x > 10 and x <= 100) Ind='10-100';
"Between " is seems to be not available. Instead use:
when(x > 10 and x <= 100) Ind='10-100';
For single variable ranges I find formats easier in most cases:
proc format library=work; value X_ind; 0 = '0' 1,2,3 = '1-3' 4,5,6 = '4-6' 7,8,9,10='7-10' 10<-100 ='10-100' 100<-high= '100+' ; run; Proc print data=tbl1; var x; format x x_ind. ; run;
The select becomes much more useful when you have multiple things to do for each group of values or when another , or more, variables are needed to complete the recode.
Generally you would only use BETWEEN in Proc SQL not a data step.
You can use syntax like
when (10 < x <=100)
which has the minor advantage of better readability than placing AND between the upper and lower range.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.