Create New Variables - Case-Level Shares

Create a new variable which computes shares from numeric data at the case (e.g., respondent) level This tool computes shares from numeric data at the case (e.g., respondent) level. A simpler and often better method for computing shares is to use the share statistics available from Statistics - CellsSTATISTICS > Cells on a table.

Technical details

• Data with missing values will have shares of NaN for all variables.
• Where data contains negative values, negative shares will be computed.
• Generally the shares computed when showing the Average of the variables created using this QScript will differ to those obtained if selecting one of the share Statistics from Statistics - Cells, as the Average will show the average of the shares computed at the respondent (case level), whereas the shares computed using Statistics - Cells are created by summing up the data for each case and thus give greater weight to cases with larger values.

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.

OR

• 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.

• 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

• 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.

JavaScript

includeWeb("QScript Selection Functions");
includeWeb("QScript Utility Functions");// for areQuestionsValidAndNonEmpty
includeWeb("QScript Functions to Generate Outputs"); // for reportNewRQuestion

caseLevelShares()

function caseLevelShares() {
let question = selectInputQuestions(["Numeric - Multi"], single = true);
if (!question)
return false;
if (!areQuestionsValidAndNonEmpty(question))
return false;

let data_file = question.dataFile;
if (question != null){
let variables = question.variables;

// Computing the total
let total_var = preventDuplicateVariableName(data_file, "total");

let total = "var " + total_var + " = " + variables[0].name;
for (var j = 1; j < variables.length; j++)
total += " + " + variables[j].name;
total += ";\r\n" + total_var + " /= 100;\r\n";

let new_variables = [];
let last_var = null;
let prefix = makeid();
for (let j = 0; j < variables.length; j++) {
try {
let variable = variables[j];
let new_name = preventDuplicateVariableName(data_file, "shares" + prefix + "_" + (j + 1));
let expression = total + variable.name + " / " + total_var;
var new_var = data_file.newJavaScriptVariable(expression, false, new_name, variable.label, last_var, false);
} catch(e)
{
log("Could not create variable: " + e);
return false;
}
new_variables.push(new_var);
last_var = new_var;
}

// Set question and create table
let new_q_name = preventDuplicateQuestionName(data_file, question.name + " -  Shares (%)");
let new_q = data_file.setQuestion(new_q_name, "Number - Multi", new_variables);
data_file.moveAfter(new_q.variables, question.variables[question.variables.length -1]);
insertAtHoverButtonIfShown(new_q);
reportNewRQuestion(new_q, "Shares");
return true;
}
}