在 SPSS 中系统地重命名变量(没有 Python)
Systematically renaming variables in SPSS (without Python)
我正在寻找在 SPSS 中重命名变量的解决方案。由于工作场所的软件限制,我无法使用 Python。
目标是将变量重命名为“oldname_new”。
我试过像这样“做重复”,但它不能与重命名功能结合使用。
do repeat x= var1 to var100.
rename var (x=concat("x","_new")).
end repeat print.
exe.
此外,我认为即使没有 do repeat
,重命名命令也不允许使用 concat 和类似命令?对吗?
那么,在 SPSS 中有解决这个问题的方法吗?
如您所见,您不能在 do repeat
循环中使用 rename
。
SPSS宏可以做到这一点-
define DoNewnames ()
rename vars
!do !v=1 !to 100 !concat("var", !v, " = var", !v, "_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, " = ", !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), " = ", 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" .
几个注意事项:
- 在写入(和插入)“my rename syntax.sps”之前,您可能需要在文件名中添加可写路径。
- 此代码将重命名数据集中的所有变量。如果你想避免一些变量 - 你应该在写入“我的重命名 syntax.sps”之前在变量列表中过滤它们(请参阅我在代码中指出的位置)。
我正在寻找在 SPSS 中重命名变量的解决方案。由于工作场所的软件限制,我无法使用 Python。
目标是将变量重命名为“oldname_new”。 我试过像这样“做重复”,但它不能与重命名功能结合使用。
do repeat x= var1 to var100.
rename var (x=concat("x","_new")).
end repeat print.
exe.
此外,我认为即使没有 do repeat
,重命名命令也不允许使用 concat 和类似命令?对吗?
那么,在 SPSS 中有解决这个问题的方法吗?
如您所见,您不能在 do repeat
循环中使用 rename
。
SPSS宏可以做到这一点-
define DoNewnames ()
rename vars
!do !v=1 !to 100 !concat("var", !v, " = var", !v, "_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, " = ", !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), " = ", 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" .
几个注意事项:
- 在写入(和插入)“my rename syntax.sps”之前,您可能需要在文件名中添加可写路径。
- 此代码将重命名数据集中的所有变量。如果你想避免一些变量 - 你应该在写入“我的重命名 syntax.sps”之前在变量列表中过滤它们(请参阅我在代码中指出的位置)。