Hi, I have been trying for hours (in SAS University Version) to re-code SEX by a dummy variable Z (female: Z = 0 and male: Z = 1), such as IF SEX=’female’ THEN Z=0;
ELSE Z=1;
The output results in blank spaces. I can't figure it out. Here is the data if someone could help me, please. Thank you so much!
YEAR X SEX COLLEGE
1975 1 female 4.4
1976 2 female 4.7
1977 3 female 4.8
1978 4 female 4.7
1979 5 female 5
1980 6 female 6
1981 7 female 6.2
1982 8 female 6.4
1983 9 female 6.3
1984 10 female 6.3
1985 11 female 6.6
1986 12 female 6.6
1987 13 female 6.7
1988 14 female 7.2
1989 15 female 7.2
1990 16 female 7.4
1991 17 female 7.6
1992 18 female 7.8
1993 19 female 7.6
1975 1 male 5.3
1976 2 male 5.3
1977 3 male 5.4
1978 4 male 5.1
1979 5 male 5
1980 6 male 5.4
1981 7 male 5.6
1982 8 male 5.9
1983 9 male 6
1984 10 male 6
1985 11 male 5.9
1986 12 male 5.8
1987 13 male 6
1988 14 male 5.9
1989 15 male 6
1990 16 male 6.2
1991 17 male 6.4
1992 18 male 6.2
1993 19 male 6.3
First of all, you can't convert a character variable to a numeric one on the fly. You need to create a new variable.
Several methods can be used.
With an informat
proc format;
informat insex
"female" = 0
"male" = 1
;
run;
data want;
set have;
sex_num = input(sex,insex.);
run;
or by using a condition
data want;
set have;
if sex = "female"
then sex_num = 0;
else sex_num = 1;
run;
or the same, using a function
data want (drop=_sex);
set have (rename=(sex=_sex));
sex = ifn(_sex = "female",0,1);
run;
In the last one, you can see how to completely replace the variable by using dataset options.
First of all, you can't convert a character variable to a numeric one on the fly. You need to create a new variable.
Several methods can be used.
With an informat
proc format;
informat insex
"female" = 0
"male" = 1
;
run;
data want;
set have;
sex_num = input(sex,insex.);
run;
or by using a condition
data want;
set have;
if sex = "female"
then sex_num = 0;
else sex_num = 1;
run;
or the same, using a function
data want (drop=_sex);
set have (rename=(sex=_sex));
sex = ifn(_sex = "female",0,1);
run;
In the last one, you can see how to completely replace the variable by using dataset options.
Thanks a lot for your response. The second code you gave me is very similar to the one I was using. The only difference is that I was using 'female' instead of "female" I do not know why it turned out to be a big difference. I got the dummy variable with that change. Thanks a lot!
Joa14
PS it is essential to know the current status. SEX might already be numeric and have a format attached, or the contents might not be exactly what you see (leading blanks etc, ). Also consider that the condition will be case-sensitive ("Female" <> "female"!).
It is rare when you need to covert categorical variables to dummy variables. Most SAS procedures work fine with the values 'female' and 'male', and don't need dummy variables; in fact they create the dummy variables internally so you don't have to.
So, unless you are in one of those very rare situations where you actually need to have dummy variables (and nothing you have said indicates you are in one of those very rare situations), don't create your own dummy variables. Make your life simple, do it the easy way.
Paige Miller thank you so much. I agree with you, unfortunately, I have been requested to convert it by dummy variable.
Thanks!
Joa14
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.
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.