BookmarkSubscribeRSS Feed
Banke
Pyrite | Level 9

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 1after i converted race and sex to character, i got new variables but in the original format of 0 and 1nothing is appearing . i am expecting the black and white, male and femalenothing 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;

8 REPLIES 8
SASKiwi
PROC Star

Why are you converting sex and race to character?

Banke
Pyrite | Level 9

HI SASKIWI,

i want to convert because of when i want to do inferential statistics. 

SASKiwi
PROC Star

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.

Banke
Pyrite | Level 9

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?

Tom
Super User Tom
Super User

@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?

ballardw
Super User

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.

Banke
Pyrite | Level 9

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)

ballardw
Super User

@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.

 

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
  • 8 replies
  • 1775 views
  • 1 like
  • 4 in conversation