Suffix All Variable Names

Question

I have a data file in which all variables were measured in 2012. I’d like to suffix their names with “_2012”. What’s the easiest way to do this?

SPSS Python Syntax Example

begin program.
variables = ‘v5 to v10‘ #Specify variables to be suffixed.
suffix =’_2012‘ # Specify suffix.
import spss,spssaux
oldnames = spssaux.VariableDict().expand(variables)
newnames = [varnam + suffix for varnam in oldnames]
spss.Submit(‘rename variables (%s=%s).’%(‘\n’.join(oldnames),’\n’.join(newnames)))
end program.

Description

  • This syntax will add a suffix to one, many or all variables in your data.
  • Variable names can be specified right behind variables = .
  • Following SPSS Syntax conventions, variable names should be separated by spaces. The TO and ALL keywords may be used and the entire specification should be enclosed in quotes (”). The desired suffix should be enclosed in quotes as well.
  • Next, a suffix can be specified right behind suffix = .
  • As a test file for this solution, you could use supermarket.sav.

Post Author: Zahid Farid