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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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