Hi
gender_name variable is character and I want to change it to numeric using formats
attributes for that variable are
charecter
length 16
format $16.
informat $16.
I do the below:
the SEX variable is numeric BUT has the following attributes even after I used a 3.???
length 8,
format best12.
informat 12.
proc format;
value $sex
'Male' = 1
'Female' =2
;
run;
data final;
set location;
SEX=input(put(gender_name,$sex.),3.);
run;
How can I get the resulting numeric variable to a length of 3???
Thanks
data final;
/*length sex 3;*/ /*you don't really want this*/
set location;
SEX=input(put(gender_name,$sex.),3.);
format sex 3.; /*this will give you the wanted format*/
informat sex; /*this will remove whatever informat attached*/
run;
Good Luck,
Haikuo
Numeric variables always have a length of 8. You can apply a format to the variable:
data final;
attrib sex format=3.;
set location;
sex=put(gender_name,$sex.);
run;
thanks for the reply
I was wondering how PUT function is giving a NUMERIC variable
is it not supposed to give character variable????
Thanks
Sorry, in a hurry there (note the length will still be 8):
proc format;
value $sex
'Male' = 1
'Female' =2
;
run;
data location;
attrib gender_name format=$10.;
gender_name="Male"; output;
gender_name="Male"; output;
gender_name="Female"; output;
run;
data final;
attrib sex length=3 format=3.; /* adds the format and length*/
set location;
SEX=input(put(gender_name,$sex.),3.);
run;
Yep, your right, it is 3-8, where on earth did I get the length always = 8 from - previous version of SAS or from other standards?
In ancient days, people use <8 numeric variables to accommodate very limited computer memories and storage.
Hi,
The software needs it in this format to do some analysis.
after using the below I still have format BEST12.
and INFORMAT 12. and of course what I wanted a length of 3
HOW DO I REMOVE THE FORMATS AND INFORMATS AS WELL
data final;
length sex 3;
set location;
SEX=input(put(gender_name,$sex.),3.);
run;
Thanks
data final;
/*length sex 3;*/ /*you don't really want this*/
set location;
SEX=input(put(gender_name,$sex.),3.);
format sex 3.; /*this will give you the wanted format*/
informat sex; /*this will remove whatever informat attached*/
run;
Good Luck,
Haikuo
Hi,
I used format and informats and that removed the formats
If I don't use length statement the length is set to default 8 . for SEX variable???
Thnx
This worked for me and gives a format and informat of 3.
proc sql; drop table work.have; quit;
data have;
input name $16. gender_name $16.;
datalines;
John Male
Joe Male
Jack Male
Johnathan Male
Johnson Male
Deborah Female
Deb Female
Denise Female
Debbie Female
;
run;
proc format;
value $sex (min=3 max=8)
'Male' = 1
'Female' = 2
other = 99
;
run;
data want;
length sex1 sex2 3;
set have;
sex1 = put(gender_name,$sex.);
sex2 = input(put(gender_name,$sex.),3.);
run;
212
213 data want;
214 length sex1 sex2 3;
215 set have;
216 sex1 = put(gender_name,$sex.);
217 sex2 = input(put(gender_name,$sex.),3.);
218 run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
216:10
NOTE: There were 9 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 9 observations and 4 variables.
proc contents data=want;
run;
# Variable Type Len
4 gender_name Char 16
3 name Char 16
1 sex1 Num 3
2 sex2 Num 3
Also for the below
will both ASIAN and NATIVE HAWAIIAN be given a value of 4???
I only have Asian in my data so I am not sure if putting a comma and writing the other will be picked also if it is available in the data????
thanks
value $race
'White'=1
'American'=2
'Hisp'=3
'Asian', 'Native Hawaiian =4
' Indian '=5
other=6
;
run;
It should. If your format is coded with the missing single quote after Hawaiian; and the words in your file are spelled exactly as you have them spelled in your format..
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.