DATA Step, Macro, Functions and more

Recoding from character to numeric with formats

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 134
Accepted Solution

Recoding from character to numeric with formats

I'm trying to recode some character values into numeric values. Here is some code that doesn't work. What have I done wrong?

proc format;

  invalue testf  'A'  'B' = 1  'C'  'D' = 0;

run;

data _null_;

chr='A';

num = input(chr,testf.);

run;


Accepted Solutions
Solution
‎10-10-2011 04:00 AM
Super User
Posts: 10,046

Recoding from character to numeric with formats

Posted in reply to BruceBrad

How about:

proc format;

  invalue    testf

   'A' ,'B' = 1

   'C' ,'D' = 0;

run;

data temp;

chr='A';

num = input(chr,testf.);

run;

Ksharp

View solution in original post


All Replies
Solution
‎10-10-2011 04:00 AM
Super User
Posts: 10,046

Recoding from character to numeric with formats

Posted in reply to BruceBrad

How about:

proc format;

  invalue    testf

   'A' ,'B' = 1

   'C' ,'D' = 0;

run;

data temp;

chr='A';

num = input(chr,testf.);

run;

Ksharp

Frequent Contributor
Posts: 134

Recoding from character to numeric with formats

Posted in reply to BruceBrad

Thanks. The curious thing about this is that proc format without the required commas does not give an error message. The error only occurs when I use the incorrect format in the data step. And a very unhelpful message too:

NOTE: Invalid argument to function INPUT at line 38 column 7.

chr=A num=. _ERROR_=1 _N_=1

NOTE: Mathematical operations could not be performed at the following places. The results of

      the operations have been set to missing values.

(I'm using version 9.3)

Respected Advisor
Posts: 3,799

Re: Recoding from character to numeric with formats

Posted in reply to BruceBrad

You don't get an error because the code is correct.  The value/invalue statements have implied concatenation, which is most useful if you need to use two different types of constants.  For example a character constant followed by a HEX constant.  "Hello' 'F2'x 

You can see how PROC FORMAT processed your statements by adding the FMTLIB option.

----------------------------------------------------------------------------

|     INFORMAT NAME: @TESTF   LENGTH:    2   NUMBER OF VALUES:    2        |

|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH   2  FUZZ:        0   |

|--------------------------------------------------------------------------|

|START           |END             |INVALUE(VER. V7|V8   10OCT2011:06:40:11)|

|----------------+----------------+----------------------------------------|

|AB              |AB              |                                       1|

|CD              |CD              |                                       0|

----------------------------------------------------------------------------

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 214 views
  • 0 likes
  • 3 in conversation