Professional > Table scripting > Hierarchical data > Derived grid tables
 
Derived grid tables
Sample Script File: DerivedGridTables.mrs
This example script is based on the Museum sample data set. See Running the sample table scripts for information on running the example scripts.
Where a survey contains a number of different questions that share the same response list (for example, separate satisfaction questions across a range of different services), the results are often reported as a single grid. In this scenario, data is collected as separate questions instead of in a loop or grid, but the data still needs to be tabulated as if it is a grid. This issue is especially relevant when the data to be tabulated comes from outside applications where loop constructs are not available (for example, IBM SPSS Statistics.sav files).
When tabulating, it is convenient to combine identical shared lists as a single grid question. First we will use the TableDoc.DataSet.Variables.AddNewVariable method to create a derived grid for variables whose categories are “Yes”, “No”, and “Not_answered”:
TableDoc.DataSet.Variables.AddNewGrid("before, biology, _
  education, resident", _
  "DerivedGrid", _
  "Derived grid", _
  "Column")
Next we create the derived variables that references and combines the derived grid slice variables:
' Create a derived variable to reference the order grid's first slice
TableDoc.DataSet.Variables.AddNewVariable("!
orderFirst "grid order first slice"
categorical[0..16]
expression("order[{first}].column");
!")

' Create a derived variable to reference the order grid's second slice
TableDoc.DataSet.Variables.AddNewVariable("!
orderSecond "grid order second slice"
categorical[0..16]
expression("order[{second}].column");
!")

' Create a derived grid that combines the derived grid slice variables
TableDoc.DataSet.Variables.AddNewGrid("orderFirst, orderSecond", _
"NewOrder", _
"New order", _
"New order column")
Table 1 demonstrates the grouping of various columns using a derived grid.
.AddNew("Table1", "DerivedGrid * DerivedGrid[].Column", _
"Grid table that groups before, biology, education, and resident together using a derived grid")
Columns grouped via a derived grid
Table 2 provides an example of grid table with a modified column axis expression.
.AddNew("Table2", "DerivedGrid * DerivedGrid[].Column{base(), Yes, No, Not_answered [IsHidden=true]}", _
"Grid table with a modified column axis expression")
Grid table with a modified column axis expression
Table 3 provides an example of grid table with a modified axis expression.
.AddNew("Table3", "!
DerivedGrid{
before,
before_male 'before - male' expression('levelid = {before} and ^.gender.ContainsAny({male})'),
before_female 'before - female' expression('levelid = {before} and ^.gender.ContainsAny({female})'),
biology,
biology_male 'biology - male' expression('levelid = {biology} and ^.gender.ContainsAny({male})'),
biology_female 'biology - female' expression('levelid = {biology} and ^.gender.ContainsAny({female})')
}
* DerivedGrid[].Column{
base(), Yes, No, Not_answered [IsHidden=True]
}
!", _
"Grid table with a modified axis expression")
Grid table with a modified axis expression
Table 4 provides an example of grid table whose elements are derived from another grid.
.AddNew("Table4", "!

NewOrder[].column
*
NewOrder{
orderFirst,
orderSecond,
all 'all' combine({orderFirst, orderSecond})
}
!", _
"Grid table whose elements are derived from another grid")
Grid table whose elements are derived from another grid
Rules and restrictions
The following rules and restrictions apply when selecting variables to create derived grids:
The selected variables must be of the same type.
If categorical variables are selected, the variables must share the same or similar category lists.
The selected variables must all be at the same level. The new grid variable will be created at the level of the selected variables.
The maximum MaxValue from the selected variables will be used as the MaxValue for the nested question in the grid.
The minimum MinValue from the selected variables will be used as the MinValue for the nested question in the grid.
Axis expressions on individual questions will be ignored in the creation of the grid.
Given that the iteration category name will be based on the name of the question, grid slices or variables in classes or compounds cannot be used in derived grids.
See also
Hierarchical data