The right way for changing variable names in SPSS is using RENAME VARIABLES
. Changing variable names manually in variable view is a bad idea because you can't keep track of what you did. This tutorial shows a better alternative.
Changing Variable Names in SPSS
- Changing variable names with syntax is straightforward with the
RENAME VARIABLES
command. - The main thing to keep in mind is that you should use parentheses when you rename more than one variable at once.
- Second, make sure that renaming variables does not result in duplicate variable names.
- Existing variable names may be used as new variable names if they're renamed themselves within the same command. So for instance,
RENAME (v1 v2 = v2 v1).
will work.
SPSS Rename Variables Syntax Examples
(The test data used by the syntax below are found here.)
*1. Basic variable renaming.
rename variables name = first_name.
rename variables married= marital_status.
*2. Rename both variables back to their original names (undo previous 2 commands).
rename variables (first_name marital_status = name married).
rename variables name = first_name.
rename variables married= marital_status.
*2. Rename both variables back to their original names (undo previous 2 commands).
rename variables (first_name marital_status = name married).
THIS TUTORIAL HAS 5 COMMENTS:
By caleb terrel on September 24th, 2014
The best contribution detailed about syntax in SPSS Statistics!
By Yaser Pourdavar on February 17th, 2016
Hi
Which file do you work on?
When I run the above syntax, it sends error message.
Thanks
By Ruben Geert van den Berg on February 17th, 2016
Hi Yaser! Right above the syntax, it says "The test data used by the syntax below are found here." If you click "here" (preferably with your mouse wheel) it'll open the test data syntax in a new tab. After running it, the syntax that throws and error should run properly.
I'd like to add that these dictionary tutorials had better be rewritten and all use a single downloadable .sav file. I hope we'll find the time for doing so in the second half of 2016 as we're currently very full.
By Nick Frye on March 18th, 2021
I have a longitudinal dataset in which all variables have prefixes and suffixes that need to be renamed. For instance, time1.sex123, time1.age123, time1.race123, time2.sex123, etc. would be renamed t1.sex, t1.age, t1.race, t2.sex. Making things a little more complex, the variables are in no specific order. How would I go about renaming both the prefixes and suffixes? Thanks for your help!
By Ruben Geert van den Berg on March 18th, 2021
Hi Nick!
First off, periods in variable names are a bad idea in SPSS.
Renaming variables according to some pattern in the old names can be done with Python. The syntax below (requiring SPSS 24+) gives a very basic example.
Hope it helps!
data list free/time1.sex123, time1.age123, time1.race123, time2.sex123.
begin data
end data.
begin program python3.
import spss
oldNams,newNams = [],[]
for ind in range(spss.GetVariableCount()):
oldNam = spss.GetVariableName(ind)
newNam = oldNam.replace('time','t').replace('123','').replace('.','_')
oldNams.append(oldNam)
newNams.append(newNam)
spssSyn = 'RENAME VARIABLES (%s = %s).'%(' '.join(oldNams),' '.join(newNams))
#print(spssSyn)
spss.Submit(spssSyn)
end program.