BookmarkSubscribeRSS Feed
PunkinSAS08
Fluorite | Level 6

I am trying to change the gender(renamed sex) variable from "female" and "male" to "2" and "1". I am trying to achieve this using proc format but keep receiving the error " 1474 'male' = 1;
NOTE: Format $GENDER is already on the library WORK.FORMATS.
NOTE: Format $GENDER has been output.
1475 'female' = 2;
--------
180
ERROR 180-322: Statement is not valid or it is used out of proper order."

 

Here is the sample of my code:

proc format;
value $gender
'male' = 1;
'female' = 2;
run;

data PUF16;
set PUF2016;
keep inc_key age gender race1;
rename inc_key = patientID;
rename age = ageyears;
rename gender = sex;
rename race1 = race;
format sex $sex.;
run;

 

Is there a "more permanent way to change the variable or is proc format the only way?

4 REPLIES 4
Tom
Super User Tom
Super User

You cannot change a variable's type.  You can make a NEW variable if you want.  You could then change the names of the variables so the new variable uses the same name as the old variable.

 

PROC FORMAT has nothing to do with changing a variable's type.  In fact the actions of PROC FORMAT have nothing to do with variables at all.  They are for defining FORMATs and INFORMATs.

 

A FORMAT is special instructions on how to convert values into text.  An INFORMAT is special instructions for how to convert text into values.

 

A character FORMAT converts text values to text.  A numeric FORMAT converts numeric values to text.  A numeric INFORMAT converts text to a number.  A character INFORMAT converts text to other text.

 

The only way to use either of those to help with converting strings like 'male' into numbers like 1 would be to make a numeric INFORMAT.   Your code is making a character FORMAT.

 

And then you don't even TRY to use what you made to CONVERT the variable.  All you appear to do is try to ATTACH the format to a variable so that it will be DISPLAYED in a different way.

 

To use an INFORMAT() to make a numeric value from a character string you need to use the INPUT() function or the INPUT statement.

 

So if your existing variable is named GENDER and has values of 'male' or 'female' you could do something like this to make a new numeric variable named SEX that has values of 1 or 2.

proc format;
invalue gender
  'male' = 1;
  'female' = 2
;
run;

data want;
  set have;
  sex = input(gender,gender.);
run;

 

Ksharp
Super User

As Tom point out , you should use INFORMAT , not FORMAT to change character into numeric .

And If you do not care about upper or lower case, you could add one more option UPCASE.

 

proc format;
invalue gender(upcase)
  'MALE'   = 1
  'FEMALE' = 2
;
run;

data want;
  set have;
  sex = input(gender,gender.);
run;
mkeintz
PROC Star

No need for a special format.  A sample array of "lookup" values in an array works fine:

 

data want;
  set have;
  array gender_list {2} $6  _temporary_ ('Male','Female');
  sex=whichc(gender,of gender_list{*});
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

The reason for your ERROR:

proc format;
value $gender
'male' = 1; /* this semicolon terminates the VALUE statement */
'female' = 2; /* therefore this turns into invalid syntax */
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 876 views
  • 3 likes
  • 5 in conversation