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 tool 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.
Truncation is identified by looking for patterns within sets of variables that have been grouped together. In Q, when the variables are combined as a [question].
This tool should only be applied to data where the labels look truncated, as in the example below. Applying it to data with labels that are already correct may have the effect of removing text unnecessarily.
Usage
To use this tool in Q:
- Select Automate > Browse Online Library > Preliminary Project Setup > Remove Truncated Text from Variable Labels
- Choose which changes you wish to apply from the list shown.
- Under Data Sets, select a variable from the data set that you wish to search for truncated labels.
- Choose which changes you wish to apply from the list shown.
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 tool 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
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript Selection Functions");
includeWeb("QScript Utility Functions");
includeWeb("QScript Functions for Fixing Truncated Labels");
removeTruncatedTextFromLabels()
function removeTruncatedTextFromLabels() {
const web_mode = inDisplayr();
const allowed_types = ["Nominal - Multi", "Ordinal - Multi",
"Numeric - Multi", "Numeric - Grid", "Binary - Multi", "Binary - Grid"];
const data_file = requestOneDataFileFromProject(false);
const fail_message = "No opportunities to tidy truncated label were found in " + data_file.name + ".";
let candidate_questions = getAllQuestionsByStructures([data_file], allowed_types);
if (!candidate_questions)
{
log(fail_message);
return false;
}
// Loop through the questions and find possible ways to tidy the truncated labels
let change_information = [];
candidate_questions.forEach(function (q) {
let labels = q.variables.map(function (v) { return v.label; });
let truncation_string = labelsAreTruncated(labels);
if (truncation_string != null) {
change_information.push({ question: q, string: truncation_string });
}
});
// Prompt the user to confirm the changes
let changes_text = [];
change_information.forEach(function (obj) {
let 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)
{
let changes_to_make = selectMany("Select the label changes to make:", changes_text);
// Apply changes
if (changes_to_make.length > 0)
{
let selected_changes = changes_to_make.map( function(j) {
let 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)
{
let 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]; }));
let new_group = project.report.appendGroup();
new_group.name = "Tidied Questions";
let text_item = new_group.appendText();
change_information.forEach(function (obj) {
var t = new_group.appendTable();
t.primary = obj.question;
});
let title_builder = Q.htmlBuilder();
let 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(fail_message);
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.
Preliminary Project Setup
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Data Cleaning QScripts
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library
R Online Library