BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kristiepauly
Obsidian | Level 7

Hi All, 

I have a numeric sex variable with values of 0 or 1 (0=female, 1=male).  I want to convert this to character variable so I can add the aforementioned format in a proc format statement.  

The below code does not make a new variable "sex_c", and you can see in the proc contents output, sex is still showing as numeric. 

Here's my code:

Data datasets.redcap_pde1;
	set datasets.redcap_pde0;
	Sex_c=PUT(sex,8.);
	Drop sex_c;
run;

Results:

Obs	Sex
1	0
2	1
3	1
4	1
5	1
6	0
7	0

Proc contents:
                     Variables in Creation Order
#	Variable	Type	Len	Format	Informat	Label
6	  Sex	      Num	  8	   15.	 	          Sex
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Leave the variable alone a create a custom format with the current values.

 

data example;
   input sex;
datalines;
0
1
;

proc format;
value sex
0='Female'
1='Male'
;

proc print data=example noobs;
   var sex;
   format sex sex. ;
run;

If the variable may have missing values I would add appropriate format such as

proc format;
value sex
0='Femaie'
1='Male'
.= 'Missing'
;

View solution in original post

4 REPLIES 4
Reeza
Super User

Why do you want is as character?

I think the format should be $8.

 

Quick rules of thumb, for PUT use the format you want to see in the output.

For input, use the informat the variable currently has. 

 

You're showing the format for Sex, not SEX_C which should be the character variable which you've dropped.....

 

 

kristiepauly
Obsidian | Level 7

I didn't really want to make it a character variable. I thought I had to, to get the format to work correctly.  

 

It was my understanding PUT is used when going from numeric to character and INPUT is used when going from character to numeric?  

 

Thanks for your help! 

ballardw
Super User

Leave the variable alone a create a custom format with the current values.

 

data example;
   input sex;
datalines;
0
1
;

proc format;
value sex
0='Female'
1='Male'
;

proc print data=example noobs;
   var sex;
   format sex sex. ;
run;

If the variable may have missing values I would add appropriate format such as

proc format;
value sex
0='Femaie'
1='Male'
.= 'Missing'
;
kristiepauly
Obsidian | Level 7

@ballardw Thank you again.  

This is frustrating. I've been messing with this for hours. I originally had what you suggested, except for the  . = "missing" and it didn't work, hence why I thought I needed to change to character to see it.  It doesn't make sense to me why adding the above line of code would all of a sudden make everything work? I didn't really care about labeling the missing data as such and figured it would just output a <period> if there wasn't a 0 or 1 in the column and I would therefore know that . = missing. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1920 views
  • 1 like
  • 3 in conversation