See Crunch Automation basics for more information.
The ALTER ARRAY command allows you to add or remove subvariables from a derived array—this includes both multiple response variables and categorical arrays.
A common use case for this command is in tracking studies where you want to add or remove subvariables (e.g., brands or statements) from arrays.
The command has some special requirements (described in the following sections) of the array and of the incoming subvariable that need to be adhered to, which are:
- This command only works on derived arrays.
- The incoming subvariable must have equivalent categories.
- You can only remove a subvariable as part of the upfront schema block.
ALTER ARRAY alias ADD alias, ..., alias (BEFORE|AFTER) alias; ALTER ARRAY alias ADD alias, ..., alias POSITION integer; ALTER ARRAY alias ADD alias, ..., alias POSITION FIRST|LAST; ALTER ARRAY alias REMOVE alias, ..., alias;
Whereas the command is fairly straightforward, the conditions for the command are strict.
The command only works on derived arrays
Derived arrays are arrays that you have created in Crunch. A derived array is one in which you have used the CREATE command such as CREATE MULTIPLE DICHOTOMY, CREATE CATEGORICAL ARRAY, or CREATE MULTIPLE SELECTION. In other words, they were not present in the original datafile you uploaded or imported.
Arrays that are defined in the data on import are not derived. 'Not being derived' means they are not mutable and 'not being mutable' means the ALTER ARRAY command cannot alter it (as an error message will appear). If you are working with a ‘real’ array, you must first derive an array (e.g., using the CREATE command such as CREATE MULTIPLE DICHOTOMY or CREATE CATEGORICAL ARRAY commands).
In the context of appending in tracking studies, Crunch will include additional subvariables automatically in the append process if the array is a real array defined in the datafile you’re importing. In that case, if you’re looking to add or remove subvariables, you may not need to use the ALTER ARRAY command because the subvariables will already be added as per the incoming datafile.
- For example, you have a dataset where Question 1 (alias = q1) has 10 subvariables (brands).
- You append a new dataset where Question 1 has 12 subvariables (2 additional brands).
- The append process will automatically add the new subvariables to Question 1.
The incoming subvariable must have equivalent categories
You must align the subvariables before they can be added using the ALTER ARRAY commmand. This is true for both multiple response and categorical array variables.
If the categories are not the same, you can use the CREATE CATEGORICAL RECODE command to align the categories in a new variable, which you can subsequently use in the ALTER ARRAY command.
A common mistake with altering multiple response variables is that the categories of the variable being added do not match. You should always inspect the categories of the subvariables before attempting to add them to an array to make sure they match.
You can only remove a subvariable as part of the upfront schema block
The REMOVE syntax must be included as part of the script's upfront schema block, while the ADD syntax can be used at any point in the scripts. In order words, there are restrictions on when you can remove a subvariable, but there are no restrictions on adding subvariables.
You must specify where you want to ADD a subvariable, either at the top, at the bottom, or somewhere in-between.
- POSITION — either by integer or whether it’s FIRST or LAST in the list
- BEFORE or AFTER another specific subvariable (which you declare by alias)
In the following, we look at a multiple response variable example from the 2019 Core Trends Mobiles and Broadband case study.
Consider the multiple response variable Social Media Sites Use. We can expand it in the variable tree to see all the subvariables:
Suppose we wish to create a net of Twitter and Instagram usage and then add that to the multiple response variable. We could first use the CREATE LOGICAL command to derive a net variable and then add that to the array:
CREATE LOGICAL web1a = 1 OR web1b = 1 AS web1a_net DESCRIPTION "Net: Twitter and Instagram";
In this case study, we need to refer to the original source variables, which are in the HIDDEN folder. But will this be compatible with the array so that the ALTER ARRAY command can work?
We can inspect the subvariables of the multiple response variable, by selecting one of the subvariables in the variable tree, inspecting it in Tables and Graphs mode, and then opening its Properties:
We can see that it has the categories Selected and Other. These match the output categories of the CREATE LOGICAL variable, so they will be compatible for the ALTER ARRAY command.
We can then put the subvariable to the array, positioning it at the top:
ALTER ARRAY web1_mr ADD web1a_net POSITION FIRST;
Make some further housekeeping adjustments on the variable, which is prudent: rename the subvariable that has been created within the multiple response variable and organize the logical variable into hidden:
ORGANIZE web1a_net INTO HIDDEN; CHANGE LABELS web1_mr APPLY REGEX_REPLACE("web1a_net", "NET: Twitter/Instagram");