Weighting - Make Merged Categories Available for Weighting

From Q
Jump to: navigation, search

This QScript creates a new copy of a Pick One question where any merged categories are hard-coded so that they can be used to set targets when creating a weight. Often, the categories shown for a demographic question in a questionnaire are not the same categories that need to be used for weighting. When the correct categories can be obtained by merging, this QScript enables those merged categories to be available when creating a weight. This is particularly helpful when a very large number of categories need to be merged.


The first image below shows a SUMMARY table of the Age question from a study next to the Value Attributes window for the question. While the age groups have been merged on the table the underlying data still contains distinct values for all of the original categories. These underlying values will be used if the Age question is used to create a weight, but we would really prefer to use the two merged categories for creating the weight.


The second image shows a SUMMARY table of a new version of the Age question that has been generated by this QScript. While the table appears the same as the previous one, the Value Attributes window now contains only two distinct values, with one for each of the merged categories. This new question, Age - Merged can now be used to construct a weight based on targets for the Under 40 and 40 and over groups.


Technical details

This QScript uses a JavaScript formula to construct the merged categories. If any of the original categories from the question are not shown on the table and not included in any of the merged questions (which can happen when rows are hidden on the table), then respondents from those categories will be assigned to a category called Missing Data and given a value of -99. This will be apparent on the table after the script runs.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.


  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.


// Create a new Pick One question from Consolidated categories of other pick one question
includeWeb("QScript Selection Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript Utility Functions");
includeWeb("QScript Data Reduction Functions");

if (!main())
    log("QScript Cancelled.");
    conditionallyEmptyLog("QScript Finished.");

function main() {

    var data_files = dataFileSelection(); 
    var available_questions = [];
    data_files.forEach(function (data_file) {
        available_questions = available_questions.concat(data_file.questions);
    available_questions = available_questions.filter(function (q) { return q.questionType == "Pick One"});

    if (available_questions.length == 0)
        log("No Pick One questions available.");

    var selected_question = selectOneQuestion("Selected the question you want to use:", available_questions);
    var merged_categories = getMergedCategoriesFromPickOne(selected_question);
    var expression = consolidatedPickOneExpression(selected_question, merged_categories);
    var q_var = selected_question.variables[0];
    var data_file = selected_question.dataFile;
    var new_var = data_file.newJavaScriptVariable(expression, 
                                                  preventDuplicateVariableName(data_file, q_var.name + "_merged"),
                                                  q_var.label + " - Merged", 
    new_var.variableType = "Categorical";

    // Set the value labels
    var value_attributes = new_var.valueAttributes;
    var unique_values = new_var.uniqueValues;
    merged_categories.forEach(function (obj, ind) {
        if (unique_values.indexOf(ind+1) > -1)
            value_attributes.setLabel(ind+1, obj.name);
    if (unique_values.indexOf(-99) > -1)
        value_attributes.setLabel(-99, "Missing Data");

    // Show the user what's been done
    var new_group_name = new_var.question.name;
    var new_group = project.report.appendGroup();
    new_group.name = new_group_name;
    var t = new_group.appendTable();
    t.primary = new_var.question;
    if (fileFormatVersion() > 8.65)

    return true;

function consolidatedPickOneExpression(question, merge_object) {
    var q_name = question.variables[0].name;
    var expression = "var _res = -99;\r\n"
    merge_object.forEach(function (obj, ind) {
        var sub_expression = "if (";
        if (ind > 0)
            sub_expression = "else " + sub_expression;
        var value_elements = obj.values.map(function (v) {
            return "Q.Source(" + q_name + ") == " + v;
        sub_expression += value_elements.join(" || ") + ")";
        sub_expression += "_res = " + (ind+1) + ";\r\n";
        expression += sub_expression;
    expression += "_res;";
    return expression;

See also