Conditional Table Creation

From Q
Jump to navigation Jump to search

Reads through all the questions in a data file and identifies any containing a response option of Don’t know or DON’T KNOW and creates a frequency table of each. A useful data file to test this on is C:\Program Files\Q\Examples\Phone.sav (this may be located on a different place on your computer depending upon how Q was installed).

When reviewing this example, please keep in mind that some tables may not show a Don’t know response because it has already been set as missing in the raw data.

log("Creating tables of all questions that contain DK");
var data_file = project.dataFiles[0];
var report = project.report;
var dks = report.appendGroup();
dks.name = "Tables of questions containing Don't Know responses";
var n_quest = data_file.questions.length;
for (var i=0; i<n_quest; i++){//looping through the project
    //getting each question
    var q = data_file.questions[i];
    if (q.questionType != "Text" && q.questionType != "Text - Multi") {
        var unique_vals = q.uniqueValues
        var val_attr = q.valueAttributes;
        for (var counter=0; counter < unique_vals.length; counter++) {
            var val = unique_vals[counter];
            var lab = val_attr.getLabel(val);
            if (lab == "Don't know"  || lab == "DON'T KNOW") {
                //Add a summary table.
                var table = dks.appendTable();
                table.primary = q;
                table.secondary = "SUMMARY";
                table.cellStatistics = ["%", "n"];
            }
        }
    }
}


See also