Help using Base SAS procedures

Converting numeric to character via format

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Converting numeric to character via format

Hi,

I have numeric data representing provinces that I formatted as characters using a proc format

proc format

     value provFmt 10='NL' 11='NS'  ...

I can use the character value in a model statement for example but in an if statement

if province='NL' or province='NS' then ...

then I get

NOTE: Character values have been converted to numeric values at the places given by:

      (Line)Smiley SadColumn).

and the statement fails.

Any ideas on how to get around this?

Thanks!

RR


Accepted Solutions
Solution
‎04-05-2013 05:37 PM
Super User
Posts: 10,516

Re: Converting numeric to character via format

Many place SAS will use the formatted value if the variable was permanently associated with the format.

For use in a datastep IF you'll basically need to create a new variable with the text value.

Either a straight forward:

ProvinceText = put(province,provFmt.);

or if the variable just has to be named Province

data want;

     set have (rename=(province=provnum));

    Province = put(provnum,provFmt.);

    /* and optionally */

   drop provnum;

run;

View solution in original post


All Replies
Super User
Posts: 10,516

Re: Converting numeric to character via format

if put(province,provFmt.) in ('NL', 'NS') then ...

Super User
Posts: 5,085

Re: Converting numeric to character via format

By creating a format, you haven't changed the values of PROVINCE.  You have only changed how they print.  So the IF statement should refer to the actual values:

if province=10 or province=11 then ...

if province in (10, 11) then ...

Good luck.

New Contributor
Posts: 3

Re: Converting numeric to character via format

Thanks, Astounding. I'm confused, though. I can use

class province(ref='NL')

and that works fine...i.e. it's behaving as if 'NL' is the value (or an alias).

Anyway, is there a way to really change the values of province?

Thanks,

RB

Solution
‎04-05-2013 05:37 PM
Super User
Posts: 10,516

Re: Converting numeric to character via format

Many place SAS will use the formatted value if the variable was permanently associated with the format.

For use in a datastep IF you'll basically need to create a new variable with the text value.

Either a straight forward:

ProvinceText = put(province,provFmt.);

or if the variable just has to be named Province

data want;

     set have (rename=(province=provnum));

    Province = put(provnum,provFmt.);

    /* and optionally */

   drop provnum;

run;

New Contributor
Posts: 3

Re: Converting numeric to character via format

Thanks, ballardw.

Super User
Super User
Posts: 6,502

Re: Converting numeric to character via format

Why you have to use ref='NL' instead of ref=10 is explained in this note: 

37108 - Setting the reference levels for the CLASS predictor variables

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 201 views
  • 8 likes
  • 4 in conversation