Help using Base SAS procedures

Proc Format to create user defined formats

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Proc Format to create user defined formats

I am receiving a syntax error with the following Race format. Can you please provide some guidance?

Race is numeric and must remain numeric.


proc format;

value Race

Caucasian = 'White'

Black,Asian,not entered,other ='Other';

proc freq data=combine;

tables Doselot Wt_Cat Race*Weight / nocum;

format Race Race.;


Accepted Solutions
Solution
‎02-21-2013 06:36 PM
Occasional Contributor
Posts: 13

Re: Proc Format to create user defined formats

That is 100% correct.  Formats, even formats applied to a variable in a DATA step and part of the dataset attributes, do not change the underlying values of the variable.  So even if it prints as "Caucasian" the unformmatted value should still be 1.  This unformmatted value is what you need to use in your new format statement.

View solution in original post


All Replies
PROC Star
Posts: 7,364

Re: Proc Format to create user defined formats

It is a character field, thus you have to precede the format name with a $  e.g.,

proc format;

value $Race

Caucasian = 'White'

Black,Asian,not entered,other ='Other';

run;

proc freq data=combine;

tables Doselot Wt_Cat Race*Weight / nocum;

format Race $Race.;

run;

Super User
Posts: 5,092

Re: Proc Format to create user defined formats

In addition, consider ...

You'll need quotes around values that get translated:

value $Race

'Caucasian' = 'White'

'Black', 'Asian', 'not entered', other='Other';

What does "not entered" mean?  Does it mean that RACE actually contains the characters "not entered" or does it mean RACE is blank?

What does "other" mean?  Does it mean that RACE actually contains the characters "other" or does it mean that RACE contains something else such as Hispanic?

Capitalization is important.  The format will look for an exact match between the format definition and the data values for RACE.

All of these issues can affect how you code the VALUE statement.

Super User
Posts: 10,538

Re: Proc Format to create user defined formats

I thought the quotes on the source were required but not so when I tested with SAS 9.2

I'm familiar with several data sources that will export a text field of 'not entered' or similar when an option button or other widget in a data base is not activated or no choice made from a group. Ugly but true.

I usually use an infomat to make these a numeric so I can assign things like 'not entered' to missing as 'Other', at least with my data sources race codes means either multi-racial or a respondant insisted on something other than the planned single racial codes.

Super User
Posts: 5,092

Re: Proc Format to create user defined formats

Interesting.  I tried it under 9.3 and got the same results ... quotes are not needed.

Just to explore, I tried defining the format in two ways:

other='Other'

'other'='Other'

Without the quotes around other, it was treated as all other values not defined by the format.  Adding the quotes around other limited that category to just the letters "other".

FWIW

Occasional Contributor
Posts: 12

Re: Proc Format to create user defined formats

In a previous formatting statement I created the numeric value of Race
proc format;

     value Race

          1     =     'Caucasian'

          2     =     'Black'

etc. so it is a numeric variable. I even double checked in the variables properties.

And, yes 'Not Entered' and 'Other' are actual characters in the Race value.

PROC Star
Posts: 7,364

Re: Proc Format to create user defined formats

Maybe you can teach me/us something: How does one enter 'Not Entered' into a numeric field?

Valued Guide
Posts: 2,175

Re: Proc Format to create user defined formats

if the field is controlled by a SAS application (I recall them using informats), these can tolerate values that appear to be non-numeric. These non-numeric values  wou;ld be treated and assigned special numeric values, or even special missing values - all with no fuss.

I do not know if such fuctionality has been extended to html or java applications


PROC Star
Posts: 7,364

Re: Proc Format to create user defined formats

,

You have caught my attention and may be teaching me something today.  Are you saying that character fields, that contain some kind of special numeric value, wouldn't require a character format?

If so, please point me in the direction of what to read.

Thanks,

Art

p.s.  Are you going to be at SGF this year?

Valued Guide
Posts: 2,175

Re: Proc Format to create user defined formats

Art

an informat can convert a string into a number

Which informat is appropriate for these?

1.2

1:2

01-02-03

?????????

Well, all could be read with either 8. or  $8. or anydttme. - not always with a useful result Smiley Wink

But they are all typed into a box as characters

another example

age-not-admitted

used to be a special string for date-of-birth, when a lady's modesty was "tolerated" by insurance companies

And in SAS we would use a special informat accepting that string as .N and other strings were informatted by the DATE informat

value anadate   'age-not-admitted' = .N other =[date9.] ;

See you in SASGF

(not long now!)

(for completing 2 papers and presentations Smiley Happy

peterC

PROC Star
Posts: 7,364

Re: Proc Format to create user defined formats

: Speaking of SGF, I'll either be presenting 0, 1 or 2 papers, depending upon decisions that have yet to be made.  I didn't have anything to submit before the deadline, but KSharp suggested something that led to two papers, one of which has already been accepted as a feature presentation at the next MWSUG meeting.  I've submitted both to a couple of the section chairs and, hopefully, will still get a chance to present them in SF.

Minimally, I'll be presenting the SAS-L stats at the combined SAS-L/SAS Discussion Forums/sasCommunity.org/sasCanadaCommunity/(and maybe)SASProfessionals meetup Tuesday night at 6:30pm.

Last year, SASProfessionals could only present via a pre-recorded video.  Are you interested in co-ordinating and presenting their stats this year?  Don Henderson will be presenting the stats for sasCommunity.org, Art Carpenter will be presenting the stats for the Discussion Forums, and Matt Malczewski will be presenting the stats for the sasCanadaCommunity.

Art

Occasional Contributor
Posts: 12

Re: Proc Format to create user defined formats

I seriously doubt I could teach you anything...relating to SAS. Smiley Happy I am sure this is my error.

PROC Star
Posts: 7,364

Re: Proc Format to create user defined formats

You would be surprised.  NONE of us know everything about SAS!  And I would include the developers, as well, in that comment.

Even after 40 years of using SAS, I am still learning new things every day.

Occasional Contributor
Posts: 12

Re: Proc Format to create user defined formats

As a newbie, that scares me. Smiley Happy

But, I think I get it. Even though I previously applied and activated a format, the original numeric values for Race are still there and I should use those again?

1 = 'White'

2, 3, 4 = 'other'?

Solution
‎02-21-2013 06:36 PM
Occasional Contributor
Posts: 13

Re: Proc Format to create user defined formats

That is 100% correct.  Formats, even formats applied to a variable in a DATA step and part of the dataset attributes, do not change the underlying values of the variable.  So even if it prints as "Caucasian" the unformmatted value should still be 1.  This unformmatted value is what you need to use in your new format statement.

☑ This topic is solved.

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

Discussion stats
  • 20 replies
  • 700 views
  • 4 likes
  • 6 in conversation