Using the Format Statement in Proc Format in a Data Step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Using the Format Statement in Proc Format in a Data Step

I have the following proc format statement:

proc format;

  value Sex (multilabel)

  1 = 'Female'

  2 = 'Male';

  value Race (multilabel)

  1 = 'Asian'

  2 = 'Black'

  3 = 'Caucasian'

  4 = 'Other'

  . = 'Not entered';

What I do not understand is how to create the format statement in a data step.

Data xxx;

What goes in between the data statement and the format statement?

format Sex  Race (How do I format from numeric to character?);

run;

I already have the following format statement at the top of my code as well.

data xxx;

   infile 'xxxx' dsd missover;

   title 'xxxx';

   informat

     Site $1.

     Pt $2.

     Dosedate mmddyy10.;

  input

  Site

  Pt

  Sex

  Race

  Dosedate

  Height

  Weight

  Result1-Result3;

   format Site $1. Pt $2. Dosedate mmddyy8.;


Accepted Solutions
Solution
‎01-26-2013 02:48 PM
Super User
Super User
Posts: 7,076

Re: Using the Format Statement in Proc Format in a Data Step

Look up the syntax of the FORMAT statement.  Basically you list one or more variables followed by a format.

So in your case you have used PROC FORMAT to define two new formats( SEX. and RACE. ).  Then in your data step you can use a format statement to attach that format to the variable.  You could include the new variables in your existing format statement or add new ones.

format sex sex.;

format age age.;

Note that the definition of the format is not stored in the dataset, only the association of the variable with the name of the format.  If you want to use the user defined formats with this data in another session you will need to either redefine them or store them in a permanent format catalog and point to it with the FMTSEARCH option.

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Using the Format Statement in Proc Format in a Data Step

proc format;

  value Sex (multilabel)

  1 = 'Female'

  2 = 'Male';

  value Race (multilabel)

  1 = 'Asian'

  2 = 'Black'

  3 = 'Caucasian'

  4 = 'Other'

  . = 'Not entered';

  data have;

  input sex race;

  newrace=put(race,race.);

  newsex=put(sex,sex.);

  cards;

  1 2

  2 1

  ;

  proc print;run;

                   Obs    sex    race    newrace    newsex

                       1      1       2      Black     Female

                       2      2       1      Asian     Male

Occasional Contributor
Posts: 12

Re: Using the Format Statement in Proc Format in a Data Step

Hello Linlin,

Although your code works, I have to use the format statement and not a put statement. Also, I do not have cards, I have to read in data. So I guess my question is;if I create another format statement, won't it negate my first format statement? If not, how do I write another data statement under the proc format statement?

proc format;

  value Sex (multilabel)

  1 = 'Female'

  2 = 'Male';

  value Race (multilabel)

  1 = 'Asian'

  2 = 'Black'

  3 = 'Caucasian'

  4 = 'Other'

  . = 'Not entered';

What I do not understand is how to create the format statement in a data step.

Data have;

Infile 'c:\xxx.txt\

Do I need to put every variable in here again?

What goes in between the data statement and the format statement?

format Sex  sex. Race race. ;

run;

Super Contributor
Posts: 273

Re: Using the Format Statement in Proc Format in a Data Step

A remark

(multilabel) has no sense here

as you are not recoding twice your race classification

by example  for the second recoding 3 and all the others together in an opposite category.

see inside

http://www2.sas.com/proceedings/sugi31/249-31.pdf

Andre

Solution
‎01-26-2013 02:48 PM
Super User
Super User
Posts: 7,076

Re: Using the Format Statement in Proc Format in a Data Step

Look up the syntax of the FORMAT statement.  Basically you list one or more variables followed by a format.

So in your case you have used PROC FORMAT to define two new formats( SEX. and RACE. ).  Then in your data step you can use a format statement to attach that format to the variable.  You could include the new variables in your existing format statement or add new ones.

format sex sex.;

format age age.;

Note that the definition of the format is not stored in the dataset, only the association of the variable with the name of the format.  If you want to use the user defined formats with this data in another session you will need to either redefine them or store them in a permanent format catalog and point to it with the FMTSEARCH option.

Occasional Contributor
Posts: 12

Re: Using the Format Statement in Proc Format in a Data Step

I swear I tried that a million times and it did not work, but a million and one was a charm. Thanks Tom and LinLin for your help. Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 479 views
  • 0 likes
  • 4 in conversation