BookmarkSubscribeRSS Feed
u58469353
Calcite | Level 5

I am trying to perform logistic regression on a dataset that contains several binomial variables. How do I convert multiple variables with "yes" and "no" as the only inputs to "0" and "1"? 

 

I have looked at some other questions on here where users asked similar questions, however, I can't seem to get it to work. When I tried, all of the values for the variable changed to 1, with no values showing 0. 

7 REPLIES 7
Tom
Super User Tom
Super User

Let's assume you mean a binary variable. (A binomial variable is a number that represents the count, or sum, of a series of binary variables. In other words a variable whose values follow a binomial distribution.).

 

If the variable contains text like YES or No then you cannot convert it into a number in the same variable.  You can either convert the strings '1' an '0'.  Or make a new variable.

 

Here is simple conversion that takes advantage of the fact that SAS will evaluate boolean expressions to 1 when True and 0 when false.

data want;
  set have;
  new_var = old_var= 'yes';
run;

But be careful.  Are the values only exact 'yes' or 'no'?  Do you ever have missing values?  If so this code will convert those to zero (false) instead of missing.  Is the case of the string consistent?  Do you ever have 'Yes' or 'YES' or 'Y' or 'y' instead of 'yes'?

u58469353
Calcite | Level 5
All of the values in the field are either 'Yes' or 'No'
u58469353
Calcite | Level 5

All of the values for the variable are wither 'Yes' or 'No'

PaigeMiller
Diamond | Level 26

@u58469353 wrote:

All of the values for the variable are wither 'Yes' or 'No'


Please examine your data and the code you are using very very carefully. 'Yes' is not the same as 'yes'. Can you see the difference?

--
Paige Miller
u58469353
Calcite | Level 5

This does not work. Causes all values in the new variable to be 0

PaigeMiller
Diamond | Level 26

@u58469353 wrote:

I am trying to perform logistic regression on a dataset that contains several binomial variables. How do I convert multiple variables with "yes" and "no" as the only inputs to "0" and "1"? 

 

I have looked at some other questions on here where users asked similar questions, however, I can't seem to get it to work. When I tried, all of the values for the variable changed to 1, with no values showing 0. 


For a Logistic regression, you should not have to convert anything. "yes" and "no" should work fine without creating 0 and 1. So make your life simple, don't create the 0 and 1 variable. (How easy is that?)

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 1409 views
  • 0 likes
  • 4 in conversation