Hello,
I'm using SAS 9.4 full edition through a virtual desktop. There is an integer variable that goes 1-11 and I'm trying to categorize it into 4 categories using an "in" statement but it's not working. Can someone fix my code below?
Thanks
Laura
elix_category=0;
if comorb_comp in(1, 2, 3, 4) then elix_category=1;
if comorb_comp in(5, 6, 7, 8, 9) then elix_category=2;
if comorb_comp in(10, 11) then elix_category=3;
if comorb_comp=. then elix_category=.;
Your code works fine when i tested:
data have;
do comorb_comp=1 to 11;
output;
end;
run;
data want;
set have;
elix_category=0;
if comorb_comp in (1, 2, 3, 4) then elix_category=1;
if comorb_comp in (5, 6, 7, 8, 9) then elix_category=2;
if comorb_comp in (10, 11) then elix_category=3;
if comorb_comp=. then elix_category=.;
run;
You could rather use "else if" as your conditions are mutual:
data want;
set have;
elix_category=0;
if comorb_comp in (1, 2, 3, 4) then elix_category=1;
else if comorb_comp in (5, 6, 7, 8, 9) then elix_category=2;
else if comorb_comp in (10, 11) then elix_category=3;
else if comorb_comp=. then elix_category=.;
run;
Your code works fine when i tested:
data have;
do comorb_comp=1 to 11;
output;
end;
run;
data want;
set have;
elix_category=0;
if comorb_comp in (1, 2, 3, 4) then elix_category=1;
if comorb_comp in (5, 6, 7, 8, 9) then elix_category=2;
if comorb_comp in (10, 11) then elix_category=3;
if comorb_comp=. then elix_category=.;
run;
You could rather use "else if" as your conditions are mutual:
data want;
set have;
elix_category=0;
if comorb_comp in (1, 2, 3, 4) then elix_category=1;
else if comorb_comp in (5, 6, 7, 8, 9) then elix_category=2;
else if comorb_comp in (10, 11) then elix_category=3;
else if comorb_comp=. then elix_category=.;
run;
The SELECT block works better than If/than/else for more than a couple of values when examining a single variable:
select (comorb_comp); when (1, 2, 3, 4) elix_category=1; when (5, 6, 7, 8, 9) elix_category=2; when (10, 11) elix_category=3; when (.) elix_category=.; otherwise elix_category=0; end;
The When lists the values of the variable on the select statement to evaluate. If you want to do multiple things when a given value is selected you can use a do /end construct.
The otherwise is the instruction when none of the listed values in all of the when statements are found.
If I had a large number of variables with the same requirement I might consider a custom informat.
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!
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.