Strip suffix from all variable names in SPSS

人盡茶涼 提交于 2019-12-07 10:25:17

问题


I have a dataset in which every variable name has the suffix "_1" (this was done to indicate the first interview timepoint). I want to remove this suffix from all variables, but there are hundreds of them, so I am looking for a way to do it without using the RENAME statement hundreds of times.

The closest to relevant info I found was from the link below, "A few SPSS loops for renaming variables dynamically." However, these examples show how to add a suffix or change a prefix, but not remove a suffix.

http://www.ats.ucla.edu/stat/spss/code/renaming_variables_dynamically.htm

I have the Python essentials package installed with SPSS, though I am not familiar with Python.


回答1:


I have no knowledge about SPSS. A quick search located an example of "Renaming variables using Python" with python (http://www.ats.ucla.edu/stat/spss/faq/renaming_vars_Python.htm)

It would fit in your case by just a little change:

begin program.
import spss, spssaux
spssaux.OpenDataFile('d:\data\elemapi2.sav')
vdict=spssaux.VariableDict()
mylist=vdict.range(start="grad_sch", end="enroll")
nvars = len(mylist)

for i in range(nvars):
    myvar = mylist[i]
    mynewvar = myvar.strip("_1")
    spss.Submit(r"""
        rename variables ( %s = %s) .
                        """ %(myvar, mynewvar))
end program.



回答2:


Here's a slightly tighter version of Jignesh's Python program. It's functionally the same.

begin program.  
import spss, spssaux  
filteredvarlist=[v.VariableName for v in spssaux.VariableDict(pattern="^.*_1$")]  
spss.Submit( "rename variables (%s=%s)." %  
    ("\n".join(filteredvarlist), "\n".join([v[:-2] for v in filteredvarlist]))  
)  
end program.



回答3:


Although the solution given on the UCLA website works for that particular example/dataset, how the python program is coded it may not work as intended in all circumstances.

For example "V01_1".strip("_1") would result incorrectly in "V0" and something like "V_1_1".strip("_1") would result in simply "V", again incorrectly and not as desired, if wanting to strip just the suffix.

And to be super critical, it also generates redundant RENAME VARIABLES commands even if the variable didn't have a "_1" suffix (although I know in your particular example you say all variables shall).

Also, it generates separate individual RENAME VARIABLE commands for each variable, not sure if this has a speed performance disadvantage than if generating a single RENAME VARIABLES command? Nonetheless, perhaps I demonstrate a different way of coding if you so required to code it in such a way:

DATA LIST FREE / ID V01_1 V02_1 V03_1 W_1_1 W_2_1 W_3_1.
BEGIN DATA
0 11 12 13 21 22 23
END DATA.

begin program.
spss.Submit(r"set mprint on.")
import spss, spssaux
allvarlist=[str(v) for v in spssaux.VariableDict()]
filteredvarlist=[v for v in allvarlist if v.endswith("_1")]
spss.Submit( "rename variables (\n" \
     + "\n".join(filteredvarlist) \
     + "\n=\n" \
     + "\n".join([v[:-2] for v in filteredvarlist]) \
     + ").")
spss.Submit(r"set mprint off.")
end program.


来源:https://stackoverflow.com/questions/34798827/strip-suffix-from-all-variable-names-in-spss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!