Reputation: 8494
I'm quite new to SAS and I use the proc format
to attribute value to codes :
proc format;
value code_to_value
-1 = .
1 = 0.5
2 = 0.25
3 - high = 0;
run;
I then convert it to a numeric column in my dataset.
DATA foo;
SET bar;
my_var = put(my_var ,code_to_value.);
RUN;
The problem is that this code set all -1
code to .
, but as a character, not as a missing value.
How can I set it to missing ?
Upvotes: 1
Views: 1068
Reputation: 27508
Allan's input(put
is a very common and legitimate construct.
Here is more information for a deeper understanding.
The value
statement of Proc FORMAT
creates a format
. Formats always performs a 'to-text' mapping.
PUT
, PUTC
and PUTN
functionsA invalue
statement creates an informat
.
INPUT
, INPUTC
and INPUTN
functionsThere is no format/function combination that directly maps a numeric value to a numeric value.
Using INPUT()
with a numeric will perform an implicit conversion to character (not a bad thing per se) prior to using the specified numeric informat. This example shows INVALUE
and INPUT
.
proc format;
invalue code_to_value
-1 = .
1 = 0.5
2 = 0.25
3 - high = 0;
run;
data have;
do my_var = -2 to 4; output; end;
run;
DATA want;
SET have;
my_varx = input(my_var, code_to_value.);
RUN;
----- LOG -----
NOTE: Numeric values have been converted to character values at the places given by:
Upvotes: 1
Reputation: 12691
The put()
function creates a character value. Combine with input()
if you want to convert back to numeric, eg:
DATA foo;
SET bar;
my_var = input(put(my_var ,code_to_value.),best.);
RUN;
Upvotes: 1