Creating Variables Across Data Files Using JavaScript

From Q
Jump to: navigation, search

JavaScript Variables can be created for one data file using data from the other data file using Q.GetValue. The way that JavaScript variables work when being used in a data file with a One to many relationship with another data file is different to how JavaScript variables work elsewhere in Q. This is because the nature of a One to many relationship is that the each observation in the individual-level database is related to multiple observations in the other database (e.g., one household may have made multiple purchases). Consequently, in these situations, JavaScript expressions that refer to variables in the situational database return an array (i.e., a list of values).

The following Expression computes penetration in c:\Program Files\Q\Examples\Panel.Q:

var brand = Q.GetValue('brand', 'Transactions.sav');
brand.length != 0

This is interpreted as follows:

  • Q.GetValue('brand', 'Transactions.sav') gets, for each observation in the individual-level database, the data in the varable called brand in the situational database (i.e., Transactions.sav). As mentioned, this is an Array. For example, if the household had made four purchases, of brands 3, 2, 1 and 1, then [3,2,1,1] would be returned.
  • var brand = assigns the array to a variable called brand. (A variable in this context is temporary and does not create a variable called brand in the Variables and Questions tab.)
  • brand.length computes the length of the array stored in the variable called brand (i.e., the number of values separate by commas).
  • brand.length != 0 returns a 1 if the length of the array contains at least one entry and a 0 otherwise (i.e., it is a logical expression and true is shown as 1 and false as 0).