Preliminary Project Setup - Remove Truncated Text from Variable Labels
Remove untidy text from the ends of variable labels when the labels have been cut off in the data file
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
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.
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
// Fix Variable Label Truncation
includeWeb("QScript Selection Functions");
includeWeb("QScript Utility Functions");
includeWeb("QScript Functions for Fixing Truncated Labels");
if (!main())
log("QScript Cancelled.");
else
log("QScript Finished.");
function main() {
var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
var structure_name = web_mode ? "variable sets" : "questions";
var allowed_types = ["Nominal - Multi", "Ordinal - Multi",
"Numeric - Multi", "Numeric - Grid", "Binary - Multi", "Binary - Grid"];
var data_file = requestOneDataFileFromProject();
var candidate_questions = getAllQuestionsByStructures([data_file], allowed_types);
if (!candidate_questions)
{
log("No " + structure_name + " required tidying.");
return false;
}
// Loop through the questions and find possible ways to tidy the truncated labels
var change_information = [];
candidate_questions.forEach(function (q) {
var labels = q.variables.map(function (v) { return v.label; });
var truncation_string = labelsAreTruncated(labels);
if (truncation_string != null) {
change_information.push({ question: q, string: truncation_string });
}
});
// Prompt the user to confirm the changes
var changes_text = [];
change_information.forEach(function (obj) {
var q = obj.question;
changes_text.push(q.name + ":" + q.variables.map(
function(v) { return(" " + v.label + " -> " + v.label.split(obj.string)[0]); }));
});
if (changes_text.length > 0)
{
var changes_to_make = selectMany("Select the label changes to make:", changes_text);
// Apply changes
if (changes_to_make.length > 0)
{
var selected_changes = changes_to_make.map( function(j) { var ch = change_information[j];
fixLabelTruncation(ch.question, ch.string); return ch; });
// Build a report of what has been changed
if (selected_changes.length > 0)
{
if (!web_mode)
{
var table_info = [["Question", "Removed text starting with"], ["",""]]
.concat(selected_changes.map(function (obj) {
return [obj.question.name, obj.string.length > 60 ?
obj.string.substr(0,59) : obj.string]; }));
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 });
text_builder.appendTable(table_info, [30, 60], null, { font: 'Lucida Console', size: 9 });
text_item.title = title_builder;
text_item.content = text_builder;
} else
project.report.setSelectedRaw(selected_changes.map(function(obj) { return obj.question; }));
}
}
} else
log("No " + structure_name + " required tidying.");
return true;
}
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.