The script below is example code you can use in a custom QScript that specifies the Values and Order of categories in similar variables. In some cases, your data file will contain a number of variables that share the same code frame, for example a five-point rating. Often you can combine these questions into a Nominal - Multi variable set so that you can show all of the data on the same table, but in some cases you may wish to show each question separately. If the rows of the tables need to be re-ordered then this would require you to drag-and drop the rows of each table according to the desired order. This QScript will do this automatically by recoding the variables. You can have a variable set like this:
and the columns (codes) will reorder to the following per the QScript:
Technical details
The Qscript looks through all variables in the file and will only modify variables that have the exact same category labels as specified in the label_order variable in the QScript code. If you want to just order and not recode all the variables, you can comment out or remove the line: recodeQuestionByLabelValueArray(questions[j], label_order, label_codes);
in the main loop.
Method
This example code uses Q23. Attitudes (categories) from the Phone Tidied.QPack you can download here. You should edit the first two variables label_order and label_codes to what you need.
//// EDIT Specify the ordering of the labels.
// The first label will appear at the top of the table or in the leftmost column.
var label_order = ['Strongly disagree', 'Disagree a little', 'Neither','Agree a little','Strongly agree'];
var label_codes = [100,200,300,400,500];
// Get all of the questions in the project
var questions = project.dataFiles[0].getQuestionsByName('');
// This function checks to see if a question contains all of
// the specified labels, and returns false if one of the
// labels in 'label_array' does not exist in the Values for
// this question.
function questionContainsAllLabels(question,label_array) {
var has_all_labels = true;
for (var j = 0; j < label_array.length; j++) {
if (question.dataReduction.contains(label_array[j]) === null) {
has_all_labels = false;
break;
}
}
return(has_all_labels);
}
// This function sorts the labels of a question according to
// a given order
function sortLabels(question, label_array) {
question.dataReduction.moveAfter(label_array[0],null);
for (var j = 1; j < label_array.length; j++) {
question.dataReduction.moveAfter(label_array[j],label_array[j-1]);
}
}
// Recode the question according to the arrays of input labels and values
function recodeQuestionByLabelValueArray(question, label_array, value_array) {
var current_value_attributes = question.valueAttributes;
var cur_val;
for (var j = 0; j < label_array.length; j++) {
cur_val = current_value_attributes.getSourceValueByLabel(label_array[j]);
if (cur_val != null) {
current_value_attributes.setValue(cur_val, value_array[j]);
}
}
}
// Loop over all of the questions in the project.
// If the question contains the required labels
// then sort the rows/columns for this question.
for (var j = 0; j < questions.length; j++) {
if (questionContainsAllLabels(questions[j], label_order)) {
sortLabels(questions[j],label_order);
recodeQuestionByLabelValueArray(questions[j], label_order, label_codes);
}
}