BookmarkSubscribeRSS Feed
klovewalk
Calcite | Level 5

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

7 REPLIES 7
art297
Opal | Level 21

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.

klovewalk
Calcite | Level 5

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;

art297
Opal | Level 21

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

klovewalk
Calcite | Level 5

What would the format and informat statements do?

Doc_Duke
Rhodochrosite | Level 12

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

dhana
Fluorite | Level 6

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

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 4273 views
  • 0 likes
  • 5 in conversation