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|

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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