1
votes

I'm using SPSS 20. In my dataset is a list of string variables which I want to recoded into numeric. Originally I wanted them to be recoded into themselves. I realize that this is not possible as SPSS runs through the dataset casewise and one variable can only have one type at a time. So I want them to be recoded into new variables but with the suffix _rec.

DO REPEAT var = var_1 var_2 ... var_n.
  RECODE var (CONVERT) INTO var_rec.
END REPEAT. 

But this creates only one new variable var_rec not several new ones.

I also tried to programme a workaround:

COMPUTE Job_2
STRING Job(A20)
    DO REPEAT var = var_1 var_2 ... var_n.
COMPUTE var = Job. 
RECODE Job (CONVERT) INTO Job_2.
DELETE VARIABLES var.
COMPUTE var = Job_2.  
END REPEAT. 

But this doesn't work because DELETE VARIABLES can not be used within a DO REPEAT loop.

So I'm back at my original question.

1

1 Answers

0
votes

You say they (string variables) can't be recoded into themselves but they can using ALTER TYPE

ALTER TYPE var_1 var_2 var_3 (F8.2). 

Where F8.2 is a numeric variable of width 8 and 2 decimal points.

You can use DO REPEAT with multiple stand-in list of variables, like following:

DO REPEAT Old = var_1 var_2 var_3
    /New = var1_rec var2_rec var3_rec.
  RECODE Old (CONVERT) INTO New.
END REPEAT PRINT. 

Which expanded out is the equivalent to:

RECODE var_1 (CONVERT) into var1_rec.
RECODE var_2 (CONVERT) into var2_rec.
RECODE var_3 (CONVERT) into var3_rec.

But if you are going to do that then you can do this within the RECODE command directly, something like this:

RECODE var_1 var_2 var_3 (CONVERT) INTO var1_rec var2_rec var3_rec.

Both methods assume the original variables are all string variables (of any and differing widths).

Take note of what exactly the CONVERT function does, namely, taking a string variable (containing numeric values stored as string) and converting it to a numeric format variable. So the input variable is always a string and output numeric.