Hello all.
i successfully recoded my data which we initially o and 1 into white and black, and male and female under the sex and race column but the proc content shows that it is numeric. i am trying to convert to character, but when i run the syntax, it comes out blank as shown in the second picture. what could be wrong please?
here is my syntax ($SEX_NEW and $RACE_NEW are the formats i created in my library)after i converted race and sex to character, i got new variables but in the original format of 0 and 1
nothing is appearing . i am expecting the black and white, male and female
DATA BIOSTAT.KEEP10;
SET BIOSTAT.KEEP5;
OPTIONS FMTSEARCH= (BIOSTAT.FORMATS);
FORMAT SEX_CHARACTER $SEX_NEW. RACE_CHARACTER $RACE_NEW.;
RUN;
Why are you converting sex and race to character?
HI SASKIWI,
i want to convert because of when i want to do inferential statistics.
Can you provide an example of those? There shouldn't be a problem just leaving them as numeric and just applying a numeric format to them if you want them to show with text descriptions.
thanks. I want to use chi square to find association between BMI and sex, race and age at the univariate level. Because race and sex are qualitative in nature, i thought i should convert them tom categorical variables. for my descriptive stats, will SAS run a bar chart with a numerical value?
@Banke wrote:
HI SASKIWI,
i want to convert because of when i want to do inferential statistics.
Why would it matter whether the variable has 0/1 or "male"/"female" for statistics?
What PROC are you using?
Doesn't it support the CLASS statement?
You need to show how you convert the values from numeric to character and the definitions of the formats.
Some possibilities involve creating character values with leading spaces and a format that doesn't display those converted values as expected.
I have a hard time seeing what you need the character value for as pretty much anything that will work with a character value will work with a numeric value and format to display desired text.
thanks. here is what i did
i had initially converted recoded race and sex which had values of 0 and 1 in the original file into 'white and black' and male and female'/ i then noticed they showed as numeric variable when i ran the proc content.
i then wanted to convert them into character variables using
DATA BIOSTAT.KEEP7;
SET BIOSTAT.KEEP2;
SEX = PUT (SEX, BEST5.);
RACE = PUT (RACE, BEST5.);
RUN;
it worked but showed up as rnew variables (race_character and sex_character in my file biostat.keep7) and the coding was the original 0 and 1 as shown in the first picture (blakc and white, male and female).
i then wanted to convert back to the formatted style of black and white, male and female using the following syntax
DATA BIOSTAT.KEEP10;
SET BIOSTAT.KEEP7;
OPTIONS FMTSEARCH= (BIOSTAT.FORMATS);
FORMAT $SEX_CHARACTER $SEX_A. $RACE_CHARACTER $RACE_A.;
RUN;
$SEX_CHARACTER and $RACE_CHARACTER . are the new variable creater after conversion to character
$SEX_A and $RACE_A are the formats i created (white and black, male and female)
@Banke wrote:
thanks. here is what i did
i had initially converted recoded race and sex which had values of 0 and 1 in the original file into 'white and black' and male and female'/ i then noticed they showed as numeric variable when i ran the proc content.
i then wanted to convert them into character variables using
DATA BIOSTAT.KEEP7;
SET BIOSTAT.KEEP2;
SEX = PUT (SEX, BEST5.);
RACE = PUT (RACE, BEST5.);
RUN;it worked but showed up as rnew variables (race_character and sex_character in my file biostat.keep7) and the coding was the original 0 and 1 as shown in the first picture (blakc and white, male and female).
i then wanted to convert back to the formatted style of black and white, male and female using the following syntax
DATA BIOSTAT.KEEP10;
SET BIOSTAT.KEEP7;
OPTIONS FMTSEARCH= (BIOSTAT.FORMATS);
FORMAT $SEX_CHARACTER $SEX_A. $RACE_CHARACTER $RACE_A.;
RUN;
$SEX_CHARACTER and $RACE_CHARACTER . are the new variable creater after conversion to character
$SEX_A and $RACE_A are the formats i created (white and black, male and female)
First. You cannot change the variable type with code like : sex = put(sex,best5.) . If sex is numeric you will get a bit in the log (you do read your logs don't you ?) that reads like this:
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column)
because Sex is numeric and you are attempting to place a character value into it.
Second, if you had created a new variable, and likely your variables, would have leading spaces. If the original value was 1, then the character value is going to have 4 leading spaces.
You do not show the code you used to create the format but if you did not have the corresponding leading spaces it likely doesn't work as expected.
The line
FORMAT $SEX_CHARACTER $SEX_A. $RACE_CHARACTER $RACE_A.;
did not associate your format because $SEX_CHARACTER and $RACE_CHARACTER are not a valid variable names and your log should have notes to that effect
Hint: Rerun the code with the assignments, the format creation and the assignment. The go to the log, copy the text from the first data step through the last with all of the notes, warnings and errors. Then on the forum open a text box by clicking on the </> icon above the message window and pasting all the copied text.
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.
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.