在 SPSS 中系统地重命名变量不使用 Python

问题描述

我正在寻找在 SPSS 中重命名变量的解决方案。由于工作场所的软件限制,我无法使用 Python。

目标是将变量重命名为“oldname_new”。 我试过这样“do repeat”,但是不能和rename功能结合使用。

do repeat x= var1 to var100.
rename var (x=concat("x","_new")).
end repeat print. 
exe.

此外,我认为即使没有 do repeat重命名命令也不允许 concat 和类似命令?对吗?

那么,在 SPSS 中是否有任何解决方案?

解决方法

正如您发现的那样,您不能在 rename 循环中使用 do repeat
SPSS 宏可以做到这一点 -

define DoNewnames ()
rename vars 
!do !v=1 !to 100 !concat("var",!v," = var","_new") !doend .
!enddefine.

* now the macro is defined,we can run it.
DoNewnames .

编辑: 上面的代码适用于具有系统名称的一组变量。如果名称不系统,您将需要一个不同的宏:

define DoNewnames (varlist=!cmdend)
rename vars 
!do !v !in(!varlist) !concat(!v," = ","_new") !doend .
!enddefine.

* Now in this case you need to feed the variable list into the macro.
DoNewnames varlist = age sex thisvar thatvar othervar. 

如果您想查看宏生成的语法(就像您对 end repeat print 所做的那样),您可以在运行宏之前运行它:

set mprint on.

编辑 2: 正如OP所说 - 最后一个宏需要命名所有要重命名的变量,如果有很多,这很麻烦。所以接下来的代码将自动获取它们,而无需单独命名它们。该过程 - 如@petit_dejeuner 的评论中所述 - 创建一个新的数据集,其中包含作为观察的每个原始变量,以及作为值的原始变量名称(=关于变量的元信息,如密码本)。这样,您可以将变量名称重新编码为重命名语法。

dataset name orig.
DATASET DECLARE  varnames.
OMS /SELECT TABLES /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information'] 
    /DESTINATION FORMAT=SAV OUTFILE='varnames' VIEWER=NO.
display dictionary.
omsend.
dataset activate varnames.
string cmd (a50).
compute cmd=concat("rename vars ",rtrim(var1),"_new .").
* Before creating the rename syntax in the following line,this is your chance to remove variables from the list which you do not wish to rename (using "select if" etc' on VAR1).    
write out="my rename syntax.sps" /cmd.
dataset activate orig.
insert file="my rename syntax.sps" .

一些注意事项:

  1. 在写入(和插入)“my rename syntax.sps”之前,您可能需要在文件名中添加一个可写路径。
  2. 此代码将重命名数据集中的所有变量。如果您想避免使用某些变量 - 您应该在写出“my rename syntax.sps”之前在变量列表中过滤它们(请参阅我在代码中指出的位置)。