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 Ruben Geert van den Berg on May 26th, 2022
Yes, of course that's possible.
Try to specify the vars as a single string and .split() it as shown below.
myvars = '''
Naomi
Esmee
Philip
Rogier
Raghmin
Glenn
Sophie
Sofie
Rutger
Anne-May
DANIEL
Nynke
Friedeman
AN-SOFIE
Coco
Naomi
Esmee
Philip
'''
for var in myvars.split('\n'):
if var:
print(var)
end program.
Hope that helps!
SPSS tutorials
By Peiyi Lin on May 31st, 2022
Thank you very much for your reply. I combined the two sections (first the variable then the label removing) into one set of syntax (please see below):
begin program.
myvars = '''
Naomi
Esmee
Philip
Rogier
Raghmin
Glenn
Sophie
'''
for var in myvars.split('\n'):
if var:
print(var)
variables = 'var'
value = 5 # 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.
But there was an error, and the message says the following:
"Traceback (most recent call last):
File "", line 63, in
File "C:\PROGRA~1\IBM\SPSS\STATIS~1\23\Python\Lib\site-packages\spssaux\spssaux.py", line 1242, in expand
raise ValueError, _msg19 + v
ValueError: Invalid variable or TO usage: var"
I replaced the "var" (in variables = ' ' ) with 'myvars' but it still gave me the same error. Did I place the wrong object here? Thank you!
By Ruben Geert van den Berg on June 1st, 2022
For your syntax, myvars should be a Python string holding a valid variable specification in SPSS format such as
v1 v3 v8
or
v1
v3
v8
or
v1 to v8.
If something like DESCRIPTIVES V1 TO V5. runs ok, then your variable specification is correct.
The error you're getting tells you that this is not the case (yet).