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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1476 views
  • 0 likes
  • 3 in conversation