Help using Base SAS procedures

Proc Format

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Proc Format

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


Accepted Solutions
Solution
‎06-05-2014 11:49 AM
Respected Advisor
Posts: 3,124

Re: Proc Format

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


All Replies
Super User
Super User
Posts: 7,413

Re: Proc Format

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;

Super Contributor
Posts: 1,040

Re: Proc Format

thanks for the reply

I was wondering how PUT function is giving a NUMERIC variable

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

Thanks

Super User
Super User
Posts: 7,413

Re: Proc Format

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;

Respected Advisor
Posts: 3,124

Re: Proc Format

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

Super User
Super User
Posts: 7,413

Re: Proc Format

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?

Respected Advisor
Posts: 3,124

Re: Proc Format

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

Super Contributor
Posts: 1,040

Re: Proc Format

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

Solution
‎06-05-2014 11:49 AM
Respected Advisor
Posts: 3,124

Re: Proc Format

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

Super Contributor
Posts: 1,040

Re: Proc Format

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

Regular Contributor
Posts: 217

Re: Proc Format

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)Smiley SadColumn).
      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

Super Contributor
Posts: 1,040

Re: Proc Format

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;

Regular Contributor
Posts: 217

Re: Proc Format

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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