Search code examples
importsasformatspssrecode

How to create a new character variable in SAS from the formats applied to a numeric variable OR How to import SPSS value labels only


I have a series of formatted numeric variables and I would like to convert them all into character variables assigned the corresponding values found in the format labels. Here is an example of the format:

proc format;
    value Group
        1= 'Experimental 1'
        2= 'Experimental 2'
        3= 'Treatment as usual';
run;

My variable Group_num has values 1-3 and has this format applied. I want to create a new character variable called Group_char which has the values "Experimental 1", "Experimental 2", and "Treatment as usual".

The (long) way I would do this would be:

data out;
set in;
format Group_char $30.;
if Group_num=1 then Group_char="Experimental 1";
if Group_num=2 then Group_char="Experimental 2";
if Group_num=3 then Group_char="Treatment as usual";
run;

However, I need to do this to 13 different variables and I don't know what their variable values, format names, and format labels are without looking at the data more. Preferably, I would want to use whatever format is already applied to the variable to automatically translate it into a new character variable, without needing to know the format name/labels or original variable values. However, if I need to find out the format name to create a new character variable just by using the format name, that would be better than needing to also know the original variables values and format labels as well.

Alternatively, another way to solve my problem would be if you could tell me if there is a way of importing SPSS datasets using variable value labels only, and leaving the values themselves out of the picture entirely, such that numeric variables with value labels are imported as character variables.

Thank you


Solution

  • First off, it's usually best not to do this - most of the time that you need the character bits, you can get them off of the formats.

    But, that said... you need to look at the vvalue function.

    data want;
      set have;
      var_char = vvalue(var_num);
    run;
    

    vvalue returns the formatted value of the argument.