Computing A Variable Measuring Time Difference in Calendar Months Since Last Interview
Jump to navigation
Jump to search
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Tracking Studies (Longitudinal Data Analysis)
Q Technical Reference > Updating and Automation > JavaScript
User Interface > JavaScript Variables > JavaScript Variables Examples Library
This example uses the Access All Data Rows feature to work out the most recent month in the data, and then computes the difference, in months, of all other observations from this most recent point. For example, observations in the same calendar month are assigned a score of 0, those from the previous calendar month are assigned a value of 1, and so on. To use this code:
- Ensure that you have Access all data rows (advanced) selected.
- In the first line of the Expression, Replace CreatedOn_date with the name of the Date variable in your data file.
var date_var = CreatedOn_date;
// finding highest date
var highest_date = 0;
for (var i = 0; i < N; i++) {
var cur_date = date_var[i];
if(cur_date > highest_date)
highest_date = cur_date;
}
// Computing month difference
var result = new Array(N); //declaring an array to hold the results
for (var i = 0; i < N; i++)
result[i] = Q.MonthDif(date_var[i], highest_date);
result
The resulting question should be set to a Question Type of Pick One.
See also
Computing A Variable Measuring Time Difference Since Last Interview
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Tracking Studies (Longitudinal Data Analysis)
Q Technical Reference > Updating and Automation > JavaScript
User Interface > JavaScript Variables > JavaScript Variables Examples Library