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.



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.


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.


  • 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.


// Fix Variable Label Truncation

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

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

function main() {
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    var allowed_types = ["Nominal - Multi", "Ordinal - Multi", 
        "Numeric - Multi", "Numeric - Grid", "Binary - Multi", "Binary - Grid"];
    var selected_questions = selectInputQuestions(allowed_types);
    if (!selected_questions)
        return false;
    // Loop through the questions and tidy the truncated labels
    var change_information = [];
    selected_questions.forEach(function (q) {
        var labels = (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 table_info = [["Question", "Removed text starting with"], ["",""]]
                 .concat( (obj) { 
                                                    return [, obj.string.length > 60 ? obj.string.substr(0,59) : obj.string]; 
        if (!web_mode) {
            var new_group =;
   = "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 });

            text_builder.appendTable(table_info, [30, 60], null, { font: 'Lucida Console', size: 9 });
            text_item.title = title_builder;
            text_item.content = text_builder;
        }else {
            var tpage ="TitleOnly");
            var ttext = tpage.subItems[0];
            ttext.text = "Tidied Questions";        
   = ttext.text
            change_information.forEach(function (obj) {
                var page = tpage.appendPage("Blank");
                var t = page.appendTable();
                t.primary = obj.question;
            var text_item = tpage.appendText();
            var tbuilder = Q.htmlBuilder();
            tbuilder.appendParagraph("Tidied Questions",  { font: 'Tahoma', size: 20 });
            tbuilder.appendTable(table_info, [30, 60], null, { font: 'Lucida Console', size: 9 });
            text_item.content = tbuilder;
    }else if (web_mode)
        log("None of the selected variable sets required tidying.");
    return true;

See also