BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lmyers2
Obsidian | Level 7

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=.;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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; 

 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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; 

 

ballardw
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 2032 views
  • 0 likes
  • 3 in conversation