BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
proc format ;
value  $gen  'F'=1 'M'=2;
run;

data ds1;
set dsn;
format  sex $gen. ;
run;

how to format for character sex variable to numberic using proc format

output M=1     and    F=0 

9 REPLIES 9
Tom
Super User Tom
Super User

FORMATS control how values are DISPLAYED.  So formats always convert values into strings.  So your code should have been written this way to make that clearer to the programmer that had to read the code after you created it.

proc format ;
  value  $gen  'F'='1' 'M'='2';
run;

data ds1;
  set dsn;
  format  sex $gen. ;
run;

INFORMATS convert strings into values.   So a CHARACTER informat creates character values and a NUMERIC informat creates numeric values.

 

So to convert the string 'M' into the number 1 you need to use a numeric INFORMAT, not a character FORMAT.

proc format ;
  invalue  gen  'F'=1 'M'=2 other=.;
run;

data ds1;
  set dsn;
  num_sex = input(sex,gen.);
run;
BrahmanandaRao
Lapis Lazuli | Level 10
data ds;
set sashelp.class;
informat sex$ gen.;
run;

proc format ;
  invalue  gen  'F'=1 'M'=2 other=.;
run;

i want to use proc fomat only not input or put functions to convert variable formats 

where i did wrong in the above syntax

Tom
Super User Tom
Super User

@BrahmanandaRao wrote:
data ds;
set sashelp.class;
informat sex$ gen.;
run;

proc format ;
  invalue  gen  'F'=1 'M'=2 other=.;
run;

i want to use proc fomat only not input or put functions to convert variable formats 

where i did wrong in the above syntax


That data step makes no sense. 

Changing the INFORMAT attached to a variable that already has values in it does nothing.  You are not reading any text and converting it into a value to store into SEX.  For that to happen you need to use either an INPUT statement or an INPUT() function call.

BrahmanandaRao
Lapis Lazuli | Level 10
/* how to define user formats char to numeric  */

proc format ;
value $gen 'F' =1 'M'=0;
run;


data ds;
set sashelp.class;
format sex $gen.;
run;



data new;
input sex $ ;
datalines;
F
F
F
M
M
M
;
run;


proc format ;
value $gender 'F'=111 'M'=222;
run;


data new1;
set new;
format sex $gender.;
run;

*How to remove user define formats for particular datasets using proc datasets*
proc datasets lib=work memtype=data;
attrib gender format= ;
run;
quit;


hi Tom i got my required output above code

but I can not remove format for new1 dataset using proc datasets attrib statment

ballardw
Super User

You need a MODIFY statement to tell proc datasets which data set in the library that you want to modify;

 

proc datasets lib=work memtype=data;
modify new1;
attrib gender sex format= ;
run;
quit;

 

BrahmanandaRao
Lapis Lazuli | Level 10

Hi Ballardw

 

but it cannot remove format it shows numeric only not actual F and M 

data new;
input sex $ ;
datalines;
F
F
F
M
M
M
;
run;


proc format ;
value $gender 'F'=111 'M'=222;
run;


data new1;
set new;
format sex $gender.;
run;



proc datasets lib=work memtype=data;
modify new1;
attrib gender format= ;
run;
quit;


ballardw
Super User

@BrahmanandaRao wrote:

Hi Ballardw

 

but it cannot remove format it shows numeric only not actual F and M 

 Wrong variable, your variable is SEX. I used gender as a test on a different data set.

If you read your LOG you will see something similar to this stating so:

646  proc datasets lib=work memtype=data;
647  modify new1;
WARNING: Variable GENDER not found in data set WORK.NEW1.
648  attrib gender format= ;
649  run;

NOTE: MODIFY was successful for WORK.NEW1.DATA.
650  quit;

 

Tom
Super User Tom
Super User

The error message tells you want is wrong.  You tried to remove the format from a variable named GENDER, but the dataset does not have any variable with that name.  The variable you used in the other code you posted was named SEX.  GENDER as the name of the FORMAT you were trying to attach to the variable, and now appear to want to remove.  

 

Also please add the quotes around the label/decode values when writing your VALUE statements in PROC FORMAT.  I know that SAS will accept the statement without them (to maintain backwards compatibility with code written in the 1970's) but it just confuses humans to not have them.

Kurt_Bremser
Super User

If you want to convert a variable from character to numeric, you MUST use the INPUT function, period.

If you want to keep the variable as is, but only change how it is displayed, assigning a format with PROC DATASETS will suffice.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 9 replies
  • 694 views
  • 0 likes
  • 4 in conversation