Preliminary Project Setup - Remove Truncated Text from Variable Labels

From Q
Jump to: navigation, search

This QScript removes untidy text from the ends of variable labels when the labels have been cut off in the data file. Such truncation of data labels can occur in older data-processing software (e.g. Quantum) when there is a limit to the lengths of labels.

To tidy the labels for a multiple response question in your project you should first ensure that the variables for the question have been combined together and use Set Question if they are not already combined. When the QScript runs you will be prompted to select the questions to tidy, and a report will be generated describing which changes have been made.

You should only apply this QScript for questions where the labels look truncated, as in the example below. Applying the QScript to questions with labels that are already correct may have the effect of removing text unnecessarily.

Example

LabelTruncationBefore2.PNG

In the example above, the variable labels from the question Q10. Why drinks more than one cola have been truncated in the data file, and this results in a messy table presentation.

The example below shows the effect of tidying these labels with this QScript.

LabelTruncationAfter2.PNG

Technical details

If possible you should try to avoid having to use this script by asking your data provider to prepare the data file according to the specifications linked at: SPSS Data File Specifications, and to ensure that there is no truncation of the Variable Labels in the SPSS data file.

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

  • Copy the JavaScript shown on this page.
  • 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

// Fix Variable Label Truncation

includeWeb("QScript Selection Functions");
includeWeb("QScript Functions for Fixing Truncated Labels");

if (!main())
    log("QScript Cancelled.");
else
    log("QScript Finished.");

function main() {
    var data_file = requestOneDataFileFromProject(false, true);
    // Get an array of all non-hidden, multiple-variable questions
    var candidate_questions = data_file.questions.filter(function (q) { return !q.isHidden && !q.isBanner && q.variables.length > 3; });
    if (candidate_questions.length == 0) {
        log("No appropriate questions found.");
        return false;
    }

    // Prompt the user
    var selected_questions = selectManyQuestions("Select questions to tidy:", candidate_questions, true).questions;

    if (selected_questions.length == 0) {
        log("No questions selected.");
        return false;
    }

    // Loop through the questions and tidy the truncated labels
    var change_information = [];
    selected_questions.forEach(function (q) {
        var labels = q.variables.map(function (v) { return v.label; });
        var truncation_string = labelsAreTruncated(labels);
        if (truncation_string != null) {
            fixLabelTruncation(q, truncation_string); 
            change_information.push({ question: q, string: truncation_string });
        }
    });

    // Build a report of what has been changed
    if (change_information.length > 0) {
        var new_group = project.report.appendGroup();
        new_group.name = "Tidied Questions";
        var text_item = new_group.appendText();
        change_information.forEach(function (obj) {
            var t = new_group.appendTable();
            t.primary = obj.question;
        });
        var title_builder = Q.htmlBuilder();
        var text_builder = Q.htmlBuilder();
        title_builder.appendParagraph("Tidied Questions",  { font: 'Tahoma', size: 20 });
        var table_info = [["Question", "Removed text starting with"], ["",""]]
                         .concat(change_information.map(function (obj) { 
                                                            return [obj.question.name, obj.string.length > 60 ? obj.string.substr(0,59) : obj.string]; 
                                                        }))
        text_builder.appendTable(table_info, [30, 60], null, { font: 'Lucida Console', size: 9 });
        text_item.title = title_builder;
        text_item.content = text_builder;
    }
    return true;
}


See also