Help using Base SAS procedures

PROC FORMAT

Reply
Frequent Contributor
Posts: 131

PROC FORMAT

I used the PROC FORMAT to create a new format called sexf:

PROC FORMAT;
VALUE $sexf
'F' = 'Female'
'M' = 'Male'
;
RUN;

I then tried to use this format to format the exsting variable (sex) in a file called file01. The variable has two values: 'M', 'F'. However, this variable still retails the values as 'M' and 'F' in the new dataset file02, but the new variable sex1 works OK with values as 'Female' and 'Male'.

DATA work.file02;
SET work.file01;
LENGTH sex $8; /*to change the length from 1 to 8*/
sex=PUT(sex,$sexf.);
sex1=PUT(sex,$sexf.);
RUN;

Can you please tell what is the problem? Thanks in advance.
Super Contributor
Super Contributor
Posts: 365

Re: PROC FORMAT

Hello Bncoxuk,

The problem is that your sex variable in the file02 dataset still has a length of 1 (the length is inherited from file01). To get what you need it is necessary to place LENGTH before SET.

Sincerely,
SPR
Frequent Contributor
Posts: 131

Re: PROC FORMAT

I tried but still got the same problem. The LENGTH statement moved before the SET statement, but the variable Sex still keeps the original value, instead of using the format defined by PROC FORMAT.
Super Contributor
Posts: 578

Re: PROC FORMAT

DATA work.file02(drop=sex);
SET work.file01;
format gender $8;
if sex='M' then gender='Male';
else gender='Female';
RUN;
Frequent Contributor
Posts: 131

Re: PROC FORMAT

Thanks, DBailey. Another way to work this out. Worked!
Contributor
Posts: 33

Re: PROC FORMAT

Are you wanting to store Sex as "male" "female" or just have it so it displays as "male" "female" in SEX variable?

See if this helps for whichever way you're wanting to do it.

*****************
PROC FORMAT;
VALUE $sexf
'F' = 'Female'
'M' = 'Male'
;
RUN;
****************
DATA TEST;
INPUT NAME $8. SEX $1. ;
DATALINES;
ADAMS M
LINCOLN F
ROGERS M
HILTON F
SMITH X
;
RUN; *** use X to show one that doesn't fit the format ***;

PROC PRINT DATA=TEST; RUN; *** sex stored as M/F ***;
PROC PRINT DATA=TEST; FORMAT SEX $SEXF.; RUN; *** sex still stored as M/F ***;
PROC CONTENTS DATA=TEST; RUN; *** see no permanent format applied ***;

DATA file02;
SET TEST;
format sex3 $sexf.;
sex1=PUT(sex,$sexf.); *** stores as Male/Female ***;
sex2=sex; *** stores as M/F ***;
sex3=sex; *** stores as M/F, displays as Male/Female ***;
RUN;
PROC PRINT DATA=FILE02; RUN;
PROC CONTENTS DATA=FILE02; RUN; *** permanent format applied to sex3 only ***;

data file02;
LENGTH sex $6; /*to change the length from 1 to 8 */
set file02;
sex=put(sex, $sexf.); *** stores as Male/Female ***;
run;
PROC PRINT DATA=FILE02; RUN;
PROC CONTENTS DATA=FILE02; RUN;
Super Contributor
Super Contributor
Posts: 365

Re: PROC FORMAT

Hello Bncoxuk,

I use EG 4.3 and tested the suggestion I provided. If LENGTH is before SET then everything works as expected. This is a program that I tested:
[pre]
PROC FORMAT;
VALUE $sexf
'F' = 'Female'
'M' = 'Male'
;
RUN;
DATA class;
LENGTH sex $8; /*to change the length from 1 to 8*/
SET sashelp.class;
sex=PUT(sex,$sexf.);
sex1=PUT(sex,$sexf.);
RUN;
[/pre]
Sincerely,
SPR Message was edited by: SPR
Frequent Contributor
Posts: 131

Re: PROC FORMAT

Hi SPR,

Using your code, mine also worked now. Thanks a lot!

bncoxuk
Frequent Contributor
Posts: 131

Re: PROC FORMAT

In my case, one tricky thing is that the variable won't be formatted using the new format style. Very tricky!

Right click the variable sex and see the Column Attributes. The format is still shown as $1. To resolve this problem, one more statement can be added immediately after the LENGTH statement:

FORMAT sex $8.;

Now the problem disappeared. I don't know why my data is different from the sashelp.class data. In fact, the format for sex in the class data is still shown as $1.
Super User
Posts: 11,343

Re: PROC FORMAT

If you want the Female/Male values displayed in PROC output just associate the format with the variable instead of increasing the space needed in the data set.

PROC PRINT DATA = WORK.FILE01;
VAR SEX;
FORMAT SEX $SEXF.;
RUN;

or add the FORMAT SEX $SEXF.; statement to DATA work.file02 to permanently associate the format and make it the default appearance.
Super User
Posts: 10,041

Re: PROC FORMAT

How about:
[pre]
format sex $sexf8.;[/pre]



Ksharp
Ask a Question
Discussion stats
  • 10 replies
  • 502 views
  • 0 likes
  • 6 in conversation