DATA Step, Macro, Functions and more

using an "in" statement to rename a categorical variable

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

using an "in" statement to rename a categorical variable

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=.;

Accepted Solutions
Solution
‎12-31-2017 01:56 PM
PROC Star
Posts: 1,356

Re: using an "in" statement to rename a categorical variable

[ Edited ]

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


All Replies
Solution
‎12-31-2017 01:56 PM
PROC Star
Posts: 1,356

Re: using an "in" statement to rename a categorical variable

[ Edited ]

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; 

 

Super User
Posts: 13,084

Re: using an "in" statement to rename a categorical variable

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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