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

Hi

This simple if then statement I am trying to use to re-code string variables X_1 X_2 and X_3 ("fever" can show up in any of the 3 X variables but not ever in all of them)  into a numeric Fever=1 is not working and I do not understand why.

 

if X_1 = "Fever" then do Fever=1;
end;
else do;
if X_2 = "Fever" then Fever=1;
if X_3 = "Fever" then Fever=1;
end;

 

I tried if (x_1 or X_2 or X_3) = "fever" then do Fever=1;

but this does not seem to work due to the fact that X is not numeric?

What to do in this case ?

Thanks,

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Mscarboncopy wrote:

Hi

This simple if then statement I am trying to use to re-code string variables X_1 X_2 and X_3 ("fever" can show up in any of the 3 X variables but not ever in all of them)  into a numeric Fever=1 is not working and I do not understand why.

 

if X_1 = "Fever" then do Fever=1;
end;
else do;
if X_2 = "Fever" then Fever=1;
if X_3 = "Fever" then Fever=1;
end;

 

I tried if (x_1 or X_2 or X_3) = "fever" then do Fever=1;

but this does not seem to work due to the fact that X is not numeric?

What to do in this case ?

Thanks,

 


Try

 

if x_1= "Fever" or X_2= "Fever" or X_3= "Fever"  then  Fever=1;

SAS has an operator that will compare a single variable to a list of values but not one that compares a list of variables to a single value.

 

The "DO" was also not needed unless you have multiple statements to execute when you find "Fever". Then you also would need an END to close the Do block.

 

Also make sure your case is consistent, your example code showed "fever" in the last code but "Fever" in the first bits.

View solution in original post

7 REPLIES 7
Reeza
Super User
 

if X_1 = "Fever" then  Fever=1;
else if X_2 = "Fever" then Fever=1;
else if X_3 = "Fever" then Fever=1;

 or

 

if whichc('FEVER', of X_1-X_3)>0 then Fever = 1;

Either of those should work. If not, please post your full log from running each. 

 


@Mscarboncopy wrote:

Hi

This simple if then statement I am trying to use to re-code string variables X_1 X_2 and X_3 ("fever" can show up in any of the 3 X variables but not ever in all of them)  into a numeric Fever=1 is not working and I do not understand why.

 

if X_1 = "Fever" then do Fever=1;
end;
else do;
if X_2 = "Fever" then Fever=1;
if X_3 = "Fever" then Fever=1;
end;

 

I tried if (x_1 or X_2 or X_3) = "fever" then do Fever=1;

but this does not seem to work due to the fact that X is not numeric?

What to do in this case ?

Thanks,

 


 

Mscarboncopy
Pyrite | Level 9

Thank you. It worked as well. 

I went with the array.

 

ballardw
Super User

@Mscarboncopy wrote:

Hi

This simple if then statement I am trying to use to re-code string variables X_1 X_2 and X_3 ("fever" can show up in any of the 3 X variables but not ever in all of them)  into a numeric Fever=1 is not working and I do not understand why.

 

if X_1 = "Fever" then do Fever=1;
end;
else do;
if X_2 = "Fever" then Fever=1;
if X_3 = "Fever" then Fever=1;
end;

 

I tried if (x_1 or X_2 or X_3) = "fever" then do Fever=1;

but this does not seem to work due to the fact that X is not numeric?

What to do in this case ?

Thanks,

 


Try

 

if x_1= "Fever" or X_2= "Fever" or X_3= "Fever"  then  Fever=1;

SAS has an operator that will compare a single variable to a list of values but not one that compares a list of variables to a single value.

 

The "DO" was also not needed unless you have multiple statements to execute when you find "Fever". Then you also would need an END to close the Do block.

 

Also make sure your case is consistent, your example code showed "fever" in the last code but "Fever" in the first bits.

Mscarboncopy
Pyrite | Level 9

thank you this also works but I think I will do the array because  I have others to rename.

 

Reeza
Super User

@ballardw  SAS has an operator that will compare a single variable to a list of values but not one that compares a list of variables to a single value.

That's what WHICHC and WHICHN are, they look for a value in a list of variables. 

FreelanceReinh
Jade | Level 19

Hi @Mscarboncopy,

 

Another option: Use an array and the IN operator.

 

Example:

data have;
input id (x_1-x_3) (:$30.);
cards;
1 Fever Fatigue Headache
2 Nausea Headache Rash
3 Headache Fever Fatigue 
4 Nausea Headache Fever
;

data want;
set have;
array x_[3];
Fever='Fever' in x_;
run;
Mscarboncopy
Pyrite | Level 9

Thank you so much! The array did work and it will help me a lot, since I have others to rename.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 928 views
  • 0 likes
  • 4 in conversation