Recode - Turn Off Missing Data Selection for Specific Values

From Q
Jump to navigation Jump to search

Turn off the missing data setting for a particular value (or values) in every variable in the project This QScript turns off the Missing Data setting for a particular value (or values) in every variable in the project. You can specify which values to modify at the time the script runs, and multiple values can be entered separated by commas.

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

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


function main() {

	// Prompt the user to enter values to untick and make sure they
	// give a sensible input.
	var entered_values;
	var got_valid_values = false;
	while (!got_valid_values) {	
		var input_string = prompt("Enter value(s) to un-tick as Missing Data (separate multiple values with commas):");
		entered_values = input_string.split(",");
		entered_values = entered_values.map(function (s) { return s.trim(); });
		if (!entered_values.every(function (s) { return s == "NaN" || parseFloat(s) == s; })) {
			alert("Enter numeric values or NaN only.");
		} else {
			got_valid_values = true;
		}
	}

	// Convert to numeric
	entered_values = entered_values.map(parseFloat);

	project.dataFiles.forEach(function (data_file) {
		data_file.variables.forEach(function (v) {
		    if (v.variableType != "Text") {
		        var uniques = v.uniqueValues;
		        var value_attributes = v.valueAttributes;
				entered_values.forEach(function (value) {
			        // Check NaN values separately
			        if (isNaN(value) && uniques.some(isNaN)
			        	|| uniques.indexOf(value) != -1) {
			            value_attributes.setIsMissingData(value, false);
			    	}
			    });
		    }
		});
	});

	return true;
}

See also