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;
How about:
proc format;
invalue testf
'A' ,'B' = 1
'C' ,'D' = 0;
run;
data temp;
chr='A';
num = input(chr,testf.);
run;
Ksharp
How about:
proc format;
invalue testf
'A' ,'B' = 1
'C' ,'D' = 0;
run;
data temp;
chr='A';
num = input(chr,testf.);
run;
Ksharp
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)
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|
----------------------------------------------------------------------------
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.