简体   繁体   English

同时重新编码多个变量

[英]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. 所以我想循环超过200个变量(它们不是按顺序)并将字符串答案重新编码为代码。 I have a codeframe list in excel with over 2000 different codes for each string. 我在excel中有一个代码帧列表,每个字符串有超过2000个不同的代码。 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. 因为它们不按顺序我想在SPSS中使用python来做到这一点,但由于我是新手,我不知道如何编写实际的重新编码。

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 谢谢M.

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. 正如@ eli-k的回答指出的那样,你不需要在Python中循环或做任何事情,以便将相同的重新编码方案应用于多个变量。 Vanilla SPSS syntax handles that quite well. Vanilla SPSS语法处理得非常好。 Having more than 2000 string-to-code pairings, however, can be problematic. 但是,拥有超过2000个字符串到代码的配对可能会有问题。 (Sometime I have trouble de-bugging RECODE syntax with only 20 pairings.) (有时我只能通过20次配对来解决RECODE语法问题。)

The solution to this is to use the AUTORECODE facility together with its APPLY TEMPLATE option: 解决方案是使用AUTORECODE工具及其APPLY TEMPLATE选项:

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. SPSS用作模板的.sat文件只是具有不同扩展名的.sav文件的特例。 They have exactly two variables: a string called "Source_" and a numeric variable called "Target_" (note the capitalization and trailing underscores). 它们只有两个变量:一个名为“Source_”的字符串和一个名为“Target_”的数字变量(注意大小写和尾随下划线)。 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. 只要您使用这些变量名称,就可以通过将Excel中的字符串到代码映射导入SPSS,然后另存为.sat文件来创建自己的模板。

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. 关于使用AUTORECODE的一个关键注意事项:数据中找不到Source_列中的任何字符串将自动分配新代码。

In SPSS syntax you can use the same recoding pattern for a bunch of variables at a time without looping at all, for example: 在SPSS语法中,您可以一次对一堆变量使用相同的重新编码模式而不进行循环,例如:

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).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM