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

I am trying to set my variables for gender to equal 1=female and 0= male this is the code that i am using:

 

data want;

set have;

if GENDER = "female"

then newgender = 0;

else newgender = 1;

run;

 

data want;

set have;

if GENDER = "male"

then newgender = 1;

else newgender = 0;

run;

However, when i run this it sets females and males to 1 and not 1= female and males=0. Is there another way to go about this?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Check the values of GENDER.

proc freq data=have;
   tables gender ;
run;

If the values are Female or FEMALE or F they will not match your test for whether they are equal to female.  You might want to make the code more flexible by use the LOWCASE() function in your test.

data want;
  set have;
  if lowcase(GENDER) = "female" then newgender = 0;
  else newgender = 1;
run;

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

Your code (or data) may not have the proper capitalization, and so everything is evaluated as else newgender = 1;

 

But really, why would you bother? Most SAS procedure can use the character variable GENDER, you don't have to turn this into zeros and ones, that seems to me to be unnecessary effort. What analysis would you do with the zeros and ones that you can't do with "male" and "female"?

--
Paige Miller
Daisy808
Calcite | Level 5

My professor wants us to set our variables to 1 or 0 so that we can apply it chi-square and logistic regression

PaigeMiller
Diamond | Level 26

Yes, @Daisy808 , you asked this already, and no one here thinks you need dummy variables to do a logistic regression or chi-squared test. Your male and female values can be used. In this case, if you want to use GENDER (which contains values "Male" or "Female"), you would use a CLASS statement in PROC LOGISTIC, something like this

 

proc logistic data=have;
    class gender;
    model y=gender;
run;

and no dummy variables are needed. SAS has already done the work so you don't have to. SAS creates the proper dummy variables behind the scenes, that's what the CLASS statement does, making your coding much simpler. And so by using the CLASS statement, you are complying with the professor's request to use dummy variables, you just let SAS do the work.

 

 

--
Paige Miller
Daisy808
Calcite | Level 5
Thank You!

I am very new to using SAS studio and I tried the code but it now says variable y doesn't exist
PaigeMiller
Diamond | Level 26

@Daisy808 wrote:
Thank You!

I am very new to using SAS studio and I tried the code but it now says variable y doesn't exist

yes that is an example. You have to use variable names and data set names that are actually in your data.

--
Paige Miller
ballardw
Super User

Chi-squares deal with counts and do not care if the value is numeric or character see:

proc freq data=sashelp.class;
   tables sex*age / chisq;
run;

You should have the SASHELP.class data set available.

This does a chi-square between a character and numeric variable (not a great example because the sample is small but the Procedure doesn't care).

Similar with proc logistic.

Daisy808
Calcite | Level 5
Thank you i did use it and it worked however, my professor wants the variables set to either equal 1 or 0. He wants to see it displayed in our tables for an assignment and the codes I've tried so far aren't working
PaigeMiller
Diamond | Level 26

@Daisy808 wrote:
Thank you i did use it and it worked however, my professor wants the variables set to either equal 1 or 0. He wants to see it displayed in our tables for an assignment and the codes I've tried so far aren't working

Tell your professor that SAS is creating the 0s and 1s for you, which is a smarter and more efficient solution that having each programmer create 0s and 1s.

--
Paige Miller
ballardw
Super User

@Daisy808 wrote:
Thank you i did use it and it worked however, my professor wants the variables set to either equal 1 or 0. He wants to see it displayed in our tables for an assignment and the codes I've tried so far aren't working

If your assignment code is "not working" then you really need to check the values that appear in your data.

Such as run

 

Proc freq data=have;

   tables gender;

run;

And then show us the result of that.

 

One common problem with some character comparisons is that you may have leading blanks in your actual data depending on where you got it and brought it into SAS. "  gender" is not equalt to "gender".

So you may end up running something like:

data want;
   set have;
   if GENDER = "female" then newgender = 0;
   else newgender = 1;
   genderlength= length(gender);
run;

proc freq data=want;
   tables genderlength*gender / list;
run;
 

If you see different Genderlength values with "female" then that is an indicator of leading blanks.

If you see "Female", "fEMALE" or other spellings then that is another issue as well and would lead to incorrect reassignment values.

You may need to run proc contents on your data a show us that as well.

Tom
Super User Tom
Super User

Check the values of GENDER.

proc freq data=have;
   tables gender ;
run;

If the values are Female or FEMALE or F they will not match your test for whether they are equal to female.  You might want to make the code more flexible by use the LOWCASE() function in your test.

data want;
  set have;
  if lowcase(GENDER) = "female" then newgender = 0;
  else newgender = 1;
run;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 10 replies
  • 3785 views
  • 1 like
  • 4 in conversation