Create New Variables - Create Categorical Variable
Jump to navigation
Jump to search
Automatically constructs a categorical variable from selected variables (the selected variables should generally be binary variables with values of only 0 and 1).
For example, if you have three binary variables: Aged 18 to 24 (Yes/No), Aged 25 to 34 (Yes/No), Aged 35 or more (Yes/No), they can be converted into a single categorical Age variable (with three categories corresponding to the original binary variables).
This newly constructed question consists of a constructed Categorical Variable and can be edited and manipulated in the same ways as any other categorical variable.
includeWeb("QScript Selection Functions");
includeWeb("QScript Utility Functions");
function createCategoricalVariable(selected_variables) {
if (selected_variables.length === 0) {
alert('Please select at least one non-text variable.');
return;
}
const data_file = selected_variables[0].question.dataFile;
const { exhaustive, exclusive } = data_file.checkExhaustiveAndExclusive(selected_variables);
if (!exclusive && !confirm('The variables you have selected are not mutually exclusive. If you wish to continue, it is advisable to analyze the new variable you have created by the variables used to create it.')) {
return;
}
if (!exhaustive && !confirm('The variables you have selected are not exhaustive (i.e., some respondents have been assigned a missing value).')) {
return;
}
const new_var_name = randomVariableName(16, 'cat');
const new_var_label = 'Constructed categorical variable';
const new_variable_set = data_file.createCategoricalVariable(new_var_name, new_var_label, selected_variables);
if (!new_variable_set) {
return;
}
return new_variable_set;
}
if (requireDataFile()) {
const selected_variables = getAllUserSelections().selected_variables.filter(v => v.variableType !== 'Text');
createCategoricalVariable(selected_variables);
}