eM_Sk
eM_Sk

Reputation: 59

Recoding multiple variables at the same time

So I want to loop over 200 variables (they are not in order) and recode string answers into codes. I have a codeframe list in excel with over 2000 different codes for each string. So as they are not in order I would like to use python in SPSS to do that, but as i'm new to that i don't know how to write actual recode code.

begin program.
import spss 
for v in ['a','b','c']: #list of variables I want to loop over
 # MISSING RECODE part ("string1"=1) ("string2"=2) ("string3"=3) etc.... up to whatever number of codes I want   
end program.

Could you please help with with missing part of code, I mean how the syntax should look like?

Thanks M

Upvotes: 1

Views: 2634

Answers (2)

Kevin Troy
Kevin Troy

Reputation: 432

As @eli-k's answer points out, you don't need to loop or do anything in Python in order to apply the same recoding scheme to multiple variables. Vanilla SPSS syntax handles that quite well. Having more than 2000 string-to-code pairings, however, can be problematic. (Sometime I have trouble de-bugging RECODE syntax with only 20 pairings.)

The solution to this is to use the AUTORECODE facility together with its APPLY TEMPLATE option:

AUTORECODE var1 var2 var3 
  /INTO nvar1 nvar2 nvar3 
  /APPLY TEMPLATE = 'my_template.sat'

The .sat files used by SPSS as templates are simply a special case of .sav files with a different extension. They have exactly two variables: a string called "Source_" and a numeric variable called "Target_" (note the capitalization and trailing underscores). So long as you use those variable names, you can create your own template by importing your string-to-code mappings from Excel into SPSS, then saving as a .sat file.

One key thing to note about using AUTORECODE: any strings found in the data that aren't in the Source_ column will be automatically assigned new codes.

Upvotes: 2

eli-k
eli-k

Reputation: 11310

In SPSS syntax you can use the same recoding pattern for a bunch of variables at a time without looping at all, for example:

recode var1 var2 var3 ("apple"=1)("orange"=2)("banana"=3) into Nvar1 Nvar2 Nvar3.

Recoding into new variables is necessary if you want to recode from strings to numbers. Alternatively, if you do not want new variables, you could do this:

recode var1 var2 var3 ("apple"="1")("orange"="2")("banana"="3").
alter type var1 var2 var3 (f6.2).

Upvotes: 1

Related Questions