Tables - Tables Showing Don't Knows
This QScript looks for Don't Know responses in the selected questions, and generates a new question and table for each which shows the respondents that selected a Don't Know option in that question.
Example
Technical details
Phrases that are identified as Don't Know type responses are ones that contain one or more of:
- dk
- dont know
- don't know
- don t know
- unsure
- not sure
- do not know
- no idea
- not applicable
The matching of these is not case sensitive.
Additionally, where a label is, in its entirety, NA or na, it is treated as a Don't Know.
If one of the selected questions appears to have more than one Don't Know category then it will be added in a separate folder. These should be checked carefully.
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.
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
JavaScript
includeWeb('QScript Utility Functions');
includeWeb('QScript Questionnaire Functions');
includeWeb('QScript Selection Functions');
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Value Attributes Functions');
includeWeb('JavaScript Text Analysis Functions');
if (!main())
log("Qscript Cancelled.");
else
conditionallyEmptyLog("QScript finished.");
function main() {
// Prompt the user to enter additional labels for DK options
var extra_dk_strings = promptForDKLabels();
// Get the user to select from questions containing DK options
var relevant_questions = getDKQuestionsWithUserEnteredLabels(["Pick One", "Pick One - Multi", "Number", "Number - Multi"], extra_dk_strings);
if (relevant_questions.length == 0) {
log("No appropriate questions found.");
return false;
}
var selected_questions = selectManyQuestions("Select the questions to display Don't Know responses for:", relevant_questions).questions;
if (selected_questions.length == 0) {
log("No questions selected.");
return false;
}
// Modify the selected questions
var questions_to_check = []; // Questions with more than 1 don't-know should be inspected by the user
var easy_questions = [];
selected_questions.forEach(function (q) {
var value_attributes = q.valueAttributes;
var change_counter = 0;
var dk_values = [];
var new_q = q.duplicate(preventDuplicateQuestionName(q.dataFile, q.name + " - Don't Know"));
new_q.questionType = "Pick Any";
q.uniqueValues.forEach(function (v) {
var cur_label = value_attributes.getLabel(v);
if (isDontKnow(cur_label) || containsSubstring(cur_label, extra_dk_strings) ) {
dk_values.push(v);
change_counter++;
}
});
var new_value_attributes = new_q.valueAttributes;
new_q.uniqueValues.forEach(function (v) {
// If the DK value was already set as missing then set it as not missing
if (dk_values.indexOf(v) > -1 && new_value_attributes.getIsMissingData(v)) {
setIsMissingDataForVariablesInQuestion(new_q, v, false);
}
setCountThisValueForVariablesInQuestion(new_q, v, dk_values.indexOf(v) > -1);
});
if (change_counter > 1)
questions_to_check.push(new_q);
else
easy_questions.push(new_q);
});
var new_group_name = "Don't Know Responses";
var new_group = generateGroupOfSummaryTables(new_group_name, easy_questions);
if (questions_to_check.length > 0) {
var check_group_name = "Questions to check";
var report_text = "Some questions appear to have more than one Don't Know category and should be checked. These have been placed in the group: " + check_group_name;
simpleHTMLReport([report_text], "Don't Know Responses", new_group, true, false);
var check_group = generateSubgroupOfSummaryTables(check_group_name, new_group, questions_to_check);
}
var tables = new_group.subItems.concat(questions_to_check.length > 0 ? check_group.subItems : []);
tables.forEach(function (table) {
if (table.type == "Table")
table.cellStatistics = ['%', 'n', 'Base n', 'Missing n'];
});
// More recent Q versions can point the user to the new items.
if (fileFormatVersion() > 8.65)
project.report.setSelectedRaw([new_group.subItems[0]]);
return true;
}
// Keep prompting the use for more entries until they enter a blank string.
// The message should tell them that they can stop if they enter a blank.
function promptUntilBlank(message) {
var answers = [];
while (true) {
var new_answer = prompt(message);
if (new_answer == "")
break;
else
answers.push(new_answer);
}
return answers;
}
Prior to the 15th of December, 2015, this page was known as Create New Variables - Creating Tables Showing Don't Knows
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Data Cleaning QScripts
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library