Since the search/replace option was a bit too fiddly and prone to typos for me, I wrote an SPSS/Python script to automatically convert standard Limesurvey scales to a numeric format in SPSS. It will look for any string variable with attached value labels, automatically recode all unique values containing a number to said number (while also recoding the "other" answer to a number) and then change labels accordingly. This will also adjust value labels that don't have an occurence of their value in the dataset.
In case someone wants to reuse this please keep in mind this is for unaltered standard settings of limesurvey batteries. If you have some weird keys/values like "A1Lime45" corresponding to your value labels/questions this script won't work. It also won't work properly if you have values containing the same number in your variable. like "A1" (Car - Mercedes) and "B1"(Bike - Mercedes). If you have customised your variables that way this will not work.
For some reason it doesn't want to show the code in the proper code blocks for me, so I've added it in spoilers below. I hope that works...
Code:
BEGIN PROGRAM PYTHON3.
import spss, spssaux, re, spssdata
print("Warning: This program is meant for recoding limesurvey default item batteries to a numeric format.\
Eventual customisations may not be covered by this code. Please make sure to check the results for errors.\
By default variables remain in string format and need to be manually converted")
sDict = spssaux.VariableDict() #Get a copy of the Variable Dictionary
infotext = "Variables processed: " # begin infotext
#Iterate through all variables (in the dictionary - all variables in this case)
for var in sDict:
#Only Adress variables which are of type String and have value labels.
if spss.GetVariableType(var.index) > 0 and var.ValueLabels != {} :
varname = spss.GetVariableName(var.index) #Get variable Name
infotext += varname + ', ' #Add processed variable to infotext
#Begin Value Labels and Recode commands for active variable
commandlab = 'Value Labels ' + varname + ' '
commandrecode = 'Recode ' + varname + ' '
commandalter = 'Alter Type ' + varname + '(f2).'
#Get unique values of Variable
varvalues = set(spssdata.Spssdata(varname, names=False).fetchall())
varvalues = set([item[0] for item in varvalues])
#Write the recode code for the variable
for item in varvalues:
if any(char.isdigit() for char in item): #make sure only values containing digits are processed
newkey = re.findall(r'\d+', item)[0] #Get first number in key (value of label)
newkey = re.sub(r'0+(.+)', r'\1', newkey) #Cut leading zeros from number
#Add entry to recode command
commandrecode = commandrecode + '("' + item + '"="' + newkey + '") '
if "-" in item: #in case "other" answer
newkey = "-9" #set value for "other" answer
#Add entry for Value labels and recode commands
commandrecode = commandrecode + '("' + item + '"="' + newkey + '") '
commandlab = commandlab + newkey + ' ' + '"' + 'Other answer' + '"' + ' '
#Open entry for each value of valuelabels
for key,val in var.ValueLabels.items():
newkey = re.findall(r'\d+', key)[0] #Get first number in key (value of label)
newkey = re.sub(r'0+(.+)', r'\1', newkey) #Cut leading zeros from number
#Add entry Value labels command:
commandlab = commandlab + newkey + ' ' + '"' + val + '"' + ' '
#Complete commands for SPSS:
commandlab = commandlab + '.'
commandrecode = commandrecode + '.\n Execute.'
#Check codes
#print(commandlab) #Syntax for labeling
#print(commandrecode) #Syntax for recoding
#print(varvalues) #Unique values for variable
#Execute recode and relabel (and alter type).
spss.Submit(commandlab) #relabel
spss.Submit(commandrecode) #recode
#spss.Submit(commandalter) #alter type to numeric
#Show frequencies of altered variables to look for errors
spss.Submit("frequencies " + varname)
#Print Infos
print(infotext)
print("Warning: Only Values containing numbers have been recoded.\
Please check your dataset to make sure all entries have been affected.")
end program.
As a standard setting this script will use -9 as a value for the option "Other". This can be manually changed in the script by simply changing the number. I would not recommend to pick a number with more figures than one since the strings are usually only size-2.
I have disabled automatic conversion of the string variables to numeric. This is so you can use the frequencies to check the data for anomalies and conversion errors. This should not occur, but I have only tested this script on my own data so it might not work properly in your specific case. You can reactivate the conversion by removing the first "#" from the line "#spss.Submit(commandalter) #alter type to numeric".
Anyways, I hope this will help people who have to deal with the same issue in the future.