Dan Chaltiel
Dan Chaltiel

Reputation: 8494

set missing value with proc format

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

Answers (2)

Richard
Richard

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.

  • A numeric format maps a numeric value to a character value
  • A character format maps a character value to a character value
  • formats can be applied programmatically using the PUT, PUTC and PUTN functions

A invalue statement creates an informat.

  • A numeric informat maps a character value to a numeric value
  • A character informat maps a character value to a character value
  • informats can be applied programmatically using the INPUT, INPUTC and INPUTN functions

There 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

Allan Bowe
Allan Bowe

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

Related Questions