I have a data set in which the variable is gender. I have specified it as character. The contents of the variable include 'M' 'F' and 'U'.
My instructions are to convert gender from character to F=0, M=1, U=-1.
I have tried if-then statements and they're not working.
Can someone help?
Thanks.
keddra
You can either create a format or informat, or use if then statements. If you show the code you tried, it should be easy to correct whatever error you probably made.
here is what I have. I'm not sure what a format or informat statement is.
data CLASS.hospital ;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile 'K:\Midterm\hospital.csv' delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
informat hospid $4. ;
informat hospco $2. ;
informat hsa $2. ;
informat admitage 3. ;
informat patco $2. ;
informat zipcode 5. ;
informat gender $1. ;
informat race $2. ;
informat eth $1. ;
informat diag $5. ;
format hospid $4. ;
format hospco $2. ;
format hsa $2. ;
format admitage 3. ;
format patco $2. ;
format zipcode 5. ;
format gender $1. ;
format race $2. ;
format eth $1. ;
format diag $5. ;
input
hospid $
hospco $
hsa $
admitage
patco $
zipcode
gender$
race $
eth $
diag $
;
if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */
label
hospid= 'Hospital ID'
hospco= 'Hospital County'
hsa= 'Health Service Area'
admitage= 'Age at Admit'
patco= 'Patient County'
zipcode= 'Patient Zip Code'
gender= 'Gender'
race= 'Race'
eth= 'Ethnicity'
diag= 'Principle Diagnosis';
run;
data class.patients;
set class.hospital;
run;
if gender= 'F' then gender = 0.;
run;
You can't change a variable's type from character to numeric, or visa versa.
You could create a new variable, e.g.,
if gender eq 'F' then sex=0;
else sex=1;
If you wanted to correct the variable as you input the data, you could just use something like:
proc format;
invalue sex
'F'=0
'M'=1
'U'=1;
run;
data want;
informat gender sex.;
input gender;
cards;
M
F
U
;
run;
HTH,
Art
What would the format and informat statements do?
The ones you have in your program just tell SAS the width to use when reading and printing the data. There is a lot more to FORMATs than that, and there is a good description in the online manuals for SAS. For 9.2, see
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000309859.htm
Doc Muhlbaier
Duke
Informats are typically used to read or input data from external files called flat files. The informat instructs SAS on how to read data into SAS variables.
If informats are instructions for reading data, then you can view formats as instructions for outputting data.
Thanks
Dhanasekaran R
Just as Art.T said.
You just need to change a little code like:
if gender= 'F' then _gender = 0;
else if gender= 'M' then _gender = 1;
else if gender= 'U' then _gender = -1;
drop gender;
run;
Ksharp
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.