Hello,
I was wondering if the if/else statements in SAS can be used with "or" to merge multiple variables. For instance, if using the following code:
if time0person='Yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' then visit_Time0="phone";
I have another 3 variables called Most0person, most0tele, and most0phone. is there some way to combine all 'Yes" answers for time0person with all 'Yes' answers for Most0person into the same variable "Visit_type_0"="in-person"?
Something like
if time0person='Yes' or Most0person='yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' or Most0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' or Most0phone='Yes' then visit_Time0="phone";
Please let me know if this question makes sense. Thanks.
@lawye010 wrote:
Hello,
I was wondering if the if/else statements in SAS can be used with "or" to merge multiple variables. For instance, if using the following code:
if time0person='Yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' then visit_Time0="phone";
I have another 3 variables called Most0person, most0tele, and most0phone. is there some way to combine all 'Yes" answers for time0person with all 'Yes' answers for Most0person into the same variable "Visit_type_0"="in-person"?
Something like
if time0person='Yes' or Most0person='yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' or Most0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' or Most0phone='Yes' then visit_Time0="phone";Please let me know if this question makes sense. Thanks.
If this were my data I would say you are incomplete. You do not have any value assigned to Visit_type_Time0 if all of the 4 variables used are "no". That might be an issue in some forms of analysis.
Like when you run a simple frequency on the variable and the number of observations do not match the number of records. So the percentages may be skewed. The more often none of the variables are yes the more significant this issue might become.
By the way, coding as "yes" "no" with character values is making you code more complex than needed if those are the only two possible values. SAS will treat a numeric value of 1 (actually pretty much anything other than 0 and missing) as "true" and 0 as false.
So creating 1/0 coded variables would allow writing code like:
if time0person or Most0person then visit_type_Time0= "in-person"; else if Time0tele or Most0tele then visit_type_Time0="telehealth";
Another potential advantage of 1/0 coding is that a sum of the variable becomes the count of 1 (or Yes), the mean is a percentage of 1 as a decimal.
And when you have multiple variables you can use some of the other functions such as max, min with them.
if time0person='Yes' or Most0person='yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' or Most0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' or Most0phone='Yes' then visit_Time0="phone";
What is wrong with the above code that makes you ask if it is possible?
@lawye010 wrote:
Hello,
I was wondering if the if/else statements in SAS can be used with "or" to merge multiple variables. For instance, if using the following code:
if time0person='Yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' then visit_Time0="phone";
I have another 3 variables called Most0person, most0tele, and most0phone. is there some way to combine all 'Yes" answers for time0person with all 'Yes' answers for Most0person into the same variable "Visit_type_0"="in-person"?
Something like
if time0person='Yes' or Most0person='yes' then visit_type_Time0= "in-person";
else if Time0tele='Yes' or Most0tele='Yes' then visit_type_Time0="telehealth";
else if time0phone='Yes' or Most0phone='Yes' then visit_Time0="phone";Please let me know if this question makes sense. Thanks.
If this were my data I would say you are incomplete. You do not have any value assigned to Visit_type_Time0 if all of the 4 variables used are "no". That might be an issue in some forms of analysis.
Like when you run a simple frequency on the variable and the number of observations do not match the number of records. So the percentages may be skewed. The more often none of the variables are yes the more significant this issue might become.
By the way, coding as "yes" "no" with character values is making you code more complex than needed if those are the only two possible values. SAS will treat a numeric value of 1 (actually pretty much anything other than 0 and missing) as "true" and 0 as false.
So creating 1/0 coded variables would allow writing code like:
if time0person or Most0person then visit_type_Time0= "in-person"; else if Time0tele or Most0tele then visit_type_Time0="telehealth";
Another potential advantage of 1/0 coding is that a sum of the variable becomes the count of 1 (or Yes), the mean is a percentage of 1 as a decimal.
And when you have multiple variables you can use some of the other functions such as max, min with them.
Hello @lawye010,
I agree with ballardw that 1/0 coded Yes/No variables are more convenient. Either way, coding IF-THEN/ELSE statements can be boring at times and also prone to typos (cf. 'yes' vs. 'Yes' and visit_Time0 vs. visit_type_Time0). You can avoid repetitions (and make your work more interesting 🙂 ) by using arrays and SAS functions.
Example (using character variables):
data have;
input (Time0person Most0person Time0tele Most0tele Time0phone Most0phone) (:$3.);
cards;
No Yes Yes Yes No No
No No Yes No Yes No
No No No No No Yes
No No No No No No
;
data want;
set have;
array vt[0:6] $10 _temporary_ (' ' 2*'in-person' 2*'telehealth' 2*'phone');
array tm[*] Time0person--Most0phone;
visit_type_time0=vt[whichc('Yes', of tm[*])];
run;
To make the comparison case-insensitive, you can use something like
visit_type_time0=vt[(find(cat(of tm[*]),'yes','i')-1)/3+1];
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.