BookmarkSubscribeRSS Feed
bncoxuk
Obsidian | Level 7
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.
10 REPLIES 10
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
bncoxuk
Obsidian | Level 7
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.
DBailey
Lapis Lazuli | Level 10
DATA work.file02(drop=sex);
SET work.file01;
format gender $8;
if sex='M' then gender='Male';
else gender='Female';
RUN;
bncoxuk
Obsidian | Level 7
Thanks, DBailey. Another way to work this out. Worked!
stateworker
Fluorite | Level 6
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;
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
bncoxuk
Obsidian | Level 7
Hi SPR,

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

bncoxuk
bncoxuk
Obsidian | Level 7
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.
ballardw
Super User
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.
Ksharp
Super User
How about:
[pre]
format sex $sexf8.;[/pre]



Ksharp

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
  • 10 replies
  • 2469 views
  • 0 likes
  • 6 in conversation