Question
"I'd like to completely remove the value label from a value for many variables at once. Is there an easy way to accomplish that?"
SPSS Python Syntax Example
begin program.
variables = 'v1 to v5' # Specify variables here.
value = 3 # Specify value to unlabel here.
import spss,spssaux
vDict = spssaux.VariableDict(caseless = True)
varList = vDict.expand(variables)
for var in varList:
valLabs = vDict[vDict.VariableIndex(var)].ValueLabels
if str(value) in valLabs:
del valLabs[str(value)]
vDict[vDict.VariableIndex(var)].ValueLabels = valLabs
end program.
variables = 'v1 to v5' # Specify variables here.
value = 3 # Specify value to unlabel here.
import spss,spssaux
vDict = spssaux.VariableDict(caseless = True)
varList = vDict.expand(variables)
for var in varList:
valLabs = vDict[vDict.VariableIndex(var)].ValueLabels
if str(value) in valLabs:
del valLabs[str(value)]
vDict[vDict.VariableIndex(var)].ValueLabels = valLabs
end program.
Description
- Since this syntax uses Python, make sure you have the SPSS Python Essentials installed.
- The two things you'll want to modify for using the example on other data are the variables and the value from which the label should be removed. Both are boldfaced in the syntax example.
- Note that the variables can be specified using the TO and ALL keywords.
- One could use supermarket.sav for testing purposes.
THIS TUTORIAL HAS 8 COMMENTS:
By anon on June 11th, 2014
vDict is undefined.
varList should be named vDict
By Ruben Geert van den Berg on June 13th, 2014
You are totally right, I've no idea how that could happen since we always test all syntax multiple times before posting it. Anyway, the syntax in this tutorial has now been corrected.
Thanks a million for letting me know!
By Megan on June 21st, 2017
Would you rewrite the syntax and email me please? I'm a little confused about what needs to change. Thanks
By Mario Giesel on June 20th, 2018
Excellent solution! Thanks a lot!
By Peiyi Lin on May 25th, 2022
Hello, thanks very much for the syntax.
They were very helpful. I have another question: I have a long list of variables (to remove value labels) and they span across multiple command lines. I have tried the block quotes (opening and closing """) and the line break method (\n) suggested in the Python reference guide, but neither worked.
Is this possible at all? (The list of variables I am including is all over the place in a data file.)
Thank you!