# Checking the Number of Selections in Questions

This script checks the number of responses provided for specified questions in a study. By default, it counts the number of non-missing values and compares this to the number of variables it can also check that the number of specified values, or, range of values, is in accordance with expectations.

To use this script

1. Amend the list of variables shown in the rows commencing with question.push([. Please note that with multiple response questions you should only list the name of the first variable in the question). Currently, this has been setup to check C:\Program Files\Q\Examples\Phone Basic Setup.Q (this may be located on a different place on your computer depending upon how Q was installed).
2. Save this file to a location somewhere on your computer/network (but not where it is currently located).
3. Run the QScript.
var questions = [];
questions.push(['q1',,,1,2]); //Counts up the number of values in q1 in the range of 1 to 2non-missing values in the question that contains q1 and reports an error if there are any missing values
questions.push(['q2',,[1,2,3,4,5,6,7],,]); //Counts up the number of responses in q2 in that are of the specified values and reports an error if there are any missing values
questions.push(['q3',,,1,8]); //Counts up the number of responses in the question the question in the range of 1 to 8 and reports an error if there are any out of range values
questions.push(['Q5_1',,,,]); //Counts up the number of respondents with one or more missing value in Q5
questions.push(['Q6_1',,,,]); //Counts up the number of respondents with one or more missing values in Q6
questions.push(['q23a',25,,,]); //Identifies responses with more than or less than 25 responses to question 23

//checks for multiple data files
if (project.dataFiles.length  != 1)
alert("Warning: multiple data files exist in this project; only the first is being examined.");
var data = project.dataFiles[0];

//general setup
var n_questions = questions.length;
var invalid_number_responses_prefix = "Required number of responses: ";
var report = "Number of responses report (NB: this report gives information for use in data cleaning but does not perform any cleaning)\r\n";
var t = project.report.appendTable();
for (var q_counter=0; q_counter<n_questions; q_counter++){
//extracts the details to be checked in each question
var details = questions[q_counter];
var variable = data.getVariableByName(details[0]);
if (variable == null)
alert(details[0] + " does not exist in the data file; you need to either select a different data file or edit this script.");
var question = variable.question;
var number_values_required = details[1];
var acceptable_values = details[2];
var min = details[3];
var max = details[4];
var name = question.name;
var results = "";
//working out which rule to use when checking for valid values
if (acceptable_values != null && (min != null || max != null))
alert(name + " contains both a list of acceptable values and a range; it should only specify one or the other (or neither)");
if (min == null && max != null || min != null && max == null )
alert(name + " needs to have both a minimum and maximum value (or neither)");
var checking_specified_values = acceptable_values instanceof Array;
var range_of_values = max != null;
var check_missing = !checking_specified_values  && !range_of_values;
//extracts the raw data
t.primary = question;
t.secondary = "RAW DATA";
var output = t.calculateOutput()
var values = output.get('Values')
var n = values.length;
var k = values[0].length;
//checking for out of range value
var too_few_counter = 0;
var too_many_counter = 0;
if (number_values_required == null)
number_values_required = k;
for (var i=0; i<n; i++){
var observation = values[i];
var valid_responses_counter = 0;
for (var c=0; c<k; c++){
if (k > 1)
v = observation[c];
else
v = observation;
if (check_missing)
{
if (!isNaN(v))
valid_responses_counter++;
}
else if (range_of_values)
{
if (v >= min && v <= max)
valid_responses_counter++;
}
else
{
if (acceptable_values.indexOf(v) == -1)
valid_responses_counter++;
}
}
if (valid_responses_counter < number_values_required)
too_few_counter++;
else if (valid_responses_counter > number_values_required)
too_many_counter++;
}
report += "\r\nRequired # responses: " + number_values_required
+ "   # too many: " + too_many_counter
+ "   # too few: " + too_few_counter
+ "   " + name;
}
t.deleteItem();
log(report + '\r\n\r\nFinished!');