Desktop User Guides > Professional > Data management scripting > Table scripting in a data management script > Exporting to RDB and creating multiple weighted tables
 
Exporting to RDB and creating multiple weighted tables
In a data management script file, you can use the Weight component to set up weighting, which you can then use to weight your tables. The TablesWeightingAllFieldsHdata.dms sample provides an example of doing this. It includes a Metadata section that creates a weighting variable called Weight and an OnJobEnd Event section that uses the Weight component to define target weighting similar to that described in More on target weighting.
The OnAfterJobEnd Event section then creates multiple tables that are all weighted using the newly-created Weight variable. This is achieved by defining the weighting variable as the default weight for the tables like this:
' Set the default weight, so that all new tables are weighted by default TableDoc.Default.Weight = "Weight"
For an example of setting the weighting on individual tables, see Weighting tables.
The sample also demonstrates setting up a reusable axis and then creating a table for each suitable variable in the data set with the reusable axis forming the banner. Here is the script:
' Create an axis that can be reused
Dim Axis
Set Axis = TableDoc.Axes.AddNew("MyBanner")
Axis.Specification = "Before{.., ^Not_answered}"

' Create tables for all suitable variables
MakeHdataTables(TableDoc, TableDoc.DataSet.MDMDocument.Fields)

Sub MakeHdataTables(TableDoc, Fields)
Dim Field

' Loop through all fields and create tables for all the
' Categorical and Grid variables and any other simple
' variables that have an axis specification stored in the
' metadata
For Each Field In Fields
Select Case Field.ObjectTypeValue
Case ObjectTypesConstants.mtVariable ' Simple variable
If Field.DataType = mr.Categorical Then
' It's a categorical variable - create a table
TableDoc.Tables.AddNew("Table" + CText(TableDoc.Tables.Count + 1), _
Field.FullName + " * axis(MyBanner)", Field.Label)
ElseIf Field.AxisExpression.IsEmpty() = False Then
' It's not a categorical variable but it has an axis expression,
' so you can create a table
TableDoc.Tables.AddNew("Table" + CText(TableDoc.Tables.Count + 1), _
Field.FullName + " * axis(MyBanner)", Field.Label)
End If
Case ObjectTypesConstants.mtGrid
' It's a grid, so use the AddNewGrid method to create
' a grid table
TableDoc.Tables.AddNewGrid("Table" + CText(TableDoc.Tables.Count + 1), _
Field.FullName, , "Grid: " + Field.Name)
Case ObjectTypesConstants.mtClass, _
ObjectTypesConstants.mtCompound, _
ObjectTypesConstants.mtArray
' Another form of container, process the contents
MakeHdataTables(TableDoc, Field.Fields)
End Select
Next
End Sub
The MakeHdataTables Sub procedure uses a For Each loop to iterate through all of the objects in the Fields collection that is passed as a parameter. In the first iteration, this is the Fields collection of the metadata document and subsequently it is the Fields collection of any nested Array, Compound, or Class objects. The For Each loop tests each object to determine the appropriate action:
If the object is a simple categorical variable, it is crosstabulated against the reusable banner.
If the object is a simple variable of any other type and has an axis expression, the variable is crosstabulated against the reusable banner. For an example of setting up an axis expression in a data management script file, see Creating axis expressions and exporting to IBM SPSS Statistics.
If the object is a grid variable, a grid table is created.
If the object is one of the other types of container object, the MakeHdataTables Sub procedure is called again to process the variables nested inside the container.
To run this sample, you need to have the UNICOM Intelligence Professional Tables Option and access to a SQL Server installation and appropriate user access rights. Before running the sample, you need to set up a SQL Server database called TablesWeightingAllFieldsHdata. If you run this script more than once, you must delete the records in the database created by the previous run, otherwise the serial numbers will be duplicated and errors will result.
You can modify the TablesWeightingAllFieldsHdata.dms sample to write to any other output data format, provided that the CDSC you are using is write-enabled (Can Add is True for the CDSC) and supports a hierarchical HDATA view of the data.
For information on sample data management scripts with table scripting sections, see Table scripting sample Data Management scripts.
See
Table scripting in a data management script