Sometimes you may have an option in a questionnaire which none of the respondents select. If you are using an Excel-style file or an SPSS file with incomplete code frames, the category will not appear when you create a table for the question. You may wish to have a table which includes the missing category, and which shows 0%. You can add this missing category a couple of ways:
Both will take you from a variable set with incomplete categories (shown in a table):
to one with the missing category included (Prefer not to answer below):
Requirements
- A Ordinal or Nominal variable set without negative category Values. This example uses the Gender variable from the cola example data set.
- For Method 2, you must have an Enterprise Displayr license and be familiar with How to Use QScripts in Displayr.
Method 1 - Using back coding
If wanting to add a category into a variable set with a - Multi structure, see Additional details below. This method uses a fake text variable to back code into the categorical variable and use the categorization tool to add any missing categories.
- First, we'll create a fake text variable to back code. In the Data Sets tree, hover and click + > Custom Code > JavaScript - Text.
- Give it a Label like dummy text.
- In the JAVASCRIPT CODE box simply type
NaN
to create all missing values for the dummy text variable. - With the dummy text variable selected, hover over the tree and select + > Text Categorization > Manual > Mutually Exclusive Categories > New.
- The categorization tool will open. Right click on New Category and Delete it.
- Click the Inputs and Back Coding button at the bottom.
- In the Corresponding back coding variables: field select the variable you'd like to add the category to in the dropdown, such as Gender in our example:
- Click OK and you will see the current categories added to the list on the right.
- Right click and Add Category for the category you are missing:
- Repeat step 9 for as many categories as you are missing (note the number next to them in the list will be the underlying Value for that category in the new code frame).
- Click Save Categories.
- You will now have a new variable with the missing categories added:
Method 2 - Using a QScript
This method uses a QScript to add the value specified.
- Open the document with your data.
- Open the QScript editor through Anything > your company name > Open QScript Editor.
- Go to Automate > Open QScript (Macro) Editor.
- Paste the below into the editor and look for ////EDIT for where to change the question name and add your new values:
includeWeb("QScript Utility Functions");
includeWeb("QScript Value Attributes Functions");
var data_file = project.dataFiles[0];
////EDIT Gender with your variable set label
var question = data_file.getQuestionByName("Gender");
var variable = question.variables[0];
// Create new copy of variable using JavaScript
var new_variable = data_file.newJavaScriptVariable(variable.name, false, preventDuplicateVariableName(data_file, variable.name), variable.label + " - Complete");
new_variable.question.questionType = "Pick One";
data_file.moveAfter([new_variable], variable);
// Copy existing values labels into new variable
copyValueAttributesForVariable(variable, new_variable);
// Add a new category by setting a label for a value that does not exist yet.
var value_attributes = new_variable.valueAttributes;
////EDIT change 3 to a new value for the new category
////change Prefer not to answer to your new category label
////to add more categories add additional copies of the line below
value_attributes.setLabel(3, "Prefer not to answer");
//OPTIONAL Show the results in a table
var new_table = project.report.appendTable();
new_table.primary = new_variable.question;
project.report.setSelectedRaw([new_table]);
4. Press the Play button.
Your new "Gender - Complete" variable will now be found in the Data Sets tree.
Additional details
Regarding adding a category a -Multi variable set in Method 1, you cannot use a - Multi structure as the variable set to back code into so some additional steps are required. Before the instructions above, you'll need to Split off one of the variables from the -Multi set to use in the back coding. Then afterwards, Combine the -Complete variable created back into the -Multi set.
Next
How to Modify Variables and Value Attributes via QScript
How to Add a Category for the Value Attributes Using the Displayr API