"I'd like to calculate the mean over all variables that have the "How would " in their variable labels. What's a good way to do so?
The reason is that I have data in which some variables are ratings for different quality aspects. The variable labels for all of them contain the string "How would ". None of the other variables in my data have a variable label containing this string. How can I compute the mean over all ratings and none of the other variables? Can I also label the new variable automatically?"
targetPattern = 'How would ' #Specify target pattern.
varList = [spss.GetVariableName(v) for v in range(spss.GetVariableCount()) if targetPattern in spss.GetVariableLabel(v)]
*If the selection of variables is correct, compute their mean as a new variable.
new_variable = 'mean_rating' #Specify a name for the new variable.
variable label %s 'Mean over (%s).'.
- This syntax will compute the mean over all variables with "How would " in their variable labels.
- First and foremost, make sure you have the SPSS Python Essentials installed.
- The first program block collects the names of all variables having the target pattern "How would " anywhere in their variable labels. These variable names are first just printed for inspection. Note that this list is not affected by closing the program block.
- If this selection is correct, the second program block will calculate the mean over these variables. A name for this new variable ("mean_rating") must be specfied since it's not obvious what it should be.
- As a test file for this solution, you could use supermarket.sav.