BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BruceBrad
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

3 REPLIES 3
Ksharp
Super User

How about:

proc format;

  invalue    testf

   'A' ,'B' = 1

   'C' ,'D' = 0;

run;

data temp;

chr='A';

num = input(chr,testf.);

run;

Ksharp

BruceBrad
Lapis Lazuli | Level 10

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)

data_null__
Jade | Level 19

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|

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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