- Posts: 23
- Thank you received: 2
Ask the community, share ideas, and connect with other LimeSurvey users!
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
@Joffm: You need to announce your holidays in advance.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
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.
Please Log in to join the conversation.