Checking ID Variable

From Q
Jump to navigation Jump to search

This script checks the properties of an ID variable. In particular it:

To run this script

  1. Change the name of the id variable that appears in quotes in the first line of code below.
  2. Save this file to a location somewhere on your computer/network (but not where it is currently located).
  3. Run the QScript.
var id = "URLID";  //Variable names are case sensitive (e.g., URLID is not the same as urlid)

//The code below performs the checks; only edit this code if youw wish to change what this routine is doing.
if (project.dataFiles.length  != 1)
  alert("Warning: multiple data files exist in this project; only the first is being examined.");
var data = project.dataFiles[0];
var results = ""
var v = data.getVariableByName(id);
if (v == null) 
{
  results = "Variable " + id + " does not exist in this data file";
}
else 
{
  var invalid_type = v.variableType != "Categorical";
  if (invalid_type)
     results = "The variable type of " + id + " is  " + v.variableType
            + "; change it to Categorical and then, if necessary, change the name of the id variable at the top of this script to the new variable's name."
  else
  {
    var t = project.report.appendTable();
    t.primary = v.question;
    t.secondary = "SUMMARY";
    var output = t.calculateOutput()
    var id_counts = output.get('n')
    var n = output.numberRows;
    var rowLabels = output.rowLabels;
    for (var r=0; r<n; r++)
        if(id_counts[r][0] > 1)
          if (rowLabels[r] != "NET")
             results += "\r\n"+rowLabels[r]+ " appears " + id_counts[r][0] + "times";
    //identifying missing values
    t.secondary = "RAW DATA";
    var output = t.calculateOutput()
    var ids = output.get('Values')
    var n = output.numberRows;
    results += "\r\n";
    for (var r=0; r<n; r++)
        if(isNaN(ids[r][0]))
          results += "\r\nThere is missing data for the id variable for row " + (r+1);
    t.deleteItem();
  }
}
if (results.length == 0)
  results = "The variable passes all the tests.";
log("Checking the suitability of " + id + " as a unique identifier\r\n\r\n" + results);


See also