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 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.
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,
Thank you. It worked as well.
I went with the array.
@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.
thank you this also works but I think I will do the array because I have others to rename.
@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.
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;
Thank you so much! The array did work and it will help me a lot, since I have others to rename.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.