BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

robertrao
Quartz | Level 8

thanks for the reply

I was wondering how PUT function is giving a NUMERIC variable

is it not supposed to give character variable????

Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Haikuo
Onyx | Level 15

data final;

length sex 3;

set location;

SEX=input(put(gender_name,$sex.),3.);

run;

  "Numeric variables always have a length of 8" , Wrong. 8 is default. Range is 3-8.

, why you are doing this? Are you from stone age where you only have 256K memory? :smileylaugh:

Haikuo

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

Haikuo
Onyx | Level 15

In ancient days, people use <8 numeric variables to accommodate very limited computer memories and storage.Smiley Wink 

robertrao
Quartz | Level 8

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

Haikuo
Onyx | Level 15

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

robertrao
Quartz | Level 8

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

jwillis
Quartz | Level 8

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

robertrao
Quartz | Level 8

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;

jwillis
Quartz | Level 8

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 2716 views
  • 9 likes
  • 4 in conversation