Developer Documentation Library > Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Derived variables > Special elements and helper elements in metadata > Creating special elements in metadata
 
Creating special elements in metadata
This topic contains examples of creating variables with special elements used by UNICOM Intelligence Reporter and UNICOM Intelligence Reporter - Survey Tabulation. The examples are in mrScriptBasic.
The first example shows a categorical variable with a mean element based on factors.
Dim MyDocument, MyCategoricalVariable, MyElement

'Create a new MDM document object
Set MyDocument = CreateObject("MDM.Document")
MyDocument.Contexts.Current = "Analysis"

'Create a categorical variable "Occupants"
Set MyCategoricalVariable = MyDocument.CreateVariable("Occupants", _
"On your most recent car journey, how many people were in the car?")
MyCategoricalVariable.DataType = mr.Categorical
MyCategoricalVariable.Minvalue = 1
MyCategoricalVariable.MaxValue = 1

'now add the variable to the document fields
MyDocument.Fields.Add(MyCategoricalVariable)

' Add categories
Set MyElement = MyDocument.CreateElement("Occupants_1", "One")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 1
' add the element to the fields
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Occupants_2", "Two")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 2
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Occupants_3", "Three")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 3
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Occupants_4", "Four")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 4
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Occupants_5", "Five")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 5
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Occupants_6", "Six")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 6
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Occupants_7_plus", "Seven or more")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Factor = 7
MyCategoricalVariable.Elements.Add(MyElement)

'add a mean element based on factors
Set MyElement = MyDocument.CreateElement("Mean", "Average Occupancy")
MyElement.Type = MDMLib.ElementTypeConstants.mtAnalysisMean
MyElement.Properties.Item["CalculationType"] = "Mean"
MyElement.Properties.Item["HasNoData"] = "True"
MyElement.Properties.Item["ExcludedFromSummaries"] = "True"
MyCategoricalVariable.Elements.Add(MyElement)

'Save the Document
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\CreatingCategorical_With_Mean_From_Factors.mdd")
MyDocument.Close()
The mean element is calculated using the factors attached to the categorical elements in the variable. Notice that the element type for the mean element is mtAnalysisMean, and not the mtCategory type used for categorical elements. When you set up a mean element, you must also specify CalculationType, HasNoData and ExcludedFromSummaries custom properties. For a list of properties and custom properties to use when creating special elements, and information on when to use them, see Special elements and helper elements in metadata.
The second example creates a numeric variable called Spend (see Creating and saving a variable), and also creates a derived categorical variable based on the numeric variable, including categories formed by banding the values in the Spend variable, and mean and standard deviation special elements based on the values in Spend variable. The categorical variable also sets up the helper variables (SumXSquared, SumX and SumN) associated with the special elements.
Dim MyDocument, MyNumericVariable
Dim MyCategoricalVariable, MyElement

'Create a new MDM document object
Set MyDocument = CreateObject("MDM.Document")
MyDocument.Contexts.Current = "Analysis"

' Create a new MDM Variable object with the name "Spend"
Set MyNumericVariable = MyDocument.CreateVariable("Spend", _
"How much do you spend on our products per year?")
' Specify the data type of this variable (numeric)
MyNumericVariable.DataType = mr.Long
'specify the range as 0 - 1000
MyNumericVariable.MinValue = 0
MyNumericVariable.MaxValue = 1000
' Add the newly created numeric variable to the MDM Document
MyDocument.Fields.Add(MyNumericVariable)

'Create a categorical variable "Spend_Range" derived from the
'existing Spend variable, including mean and standard deviation
'analysis elements and the associated helper elements
Set MyCategoricalVariable = MyDocument.CreateVariable("Spend_Range", "Spending Range")
MyCategoricalVariable.DataType = mr.Categorical
MyCategoricalVariable.MinValue = 1
MyCategoricalVariable.MaxValue = 1
'now add the variable to the document fields
MyDocument.Fields.Add(MyCategoricalVariable)

' Now add the categories based on the Spend variable
Set MyElement = MyDocument.CreateElement("Low")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Expression = ("spend > 0 and spend <= 100")
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("Medium")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Expression = ("spend > 100 and spend <= 500")
MyCategoricalVariable.Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("High")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Expression = ("spend > 500 and spend <= 1000")
MyCategoricalVariable.Elements.Add(MyElement)

'Add mean and standard deviation elements with helper elements
'first the helper elements
'sumxsquared required for standard deviation
Set MyElement = MyDocument.CreateElement("SumXSquared")
MyElement.Type = MDMLib.ElementTypeConstants.mtAnalysisSummaryData
Set MyElement.MultiplierReference = MyNumericVariable
MyElement.Properties.Item["CalculationType"] = "SumXSquared"
MyElement.Properties.Item["DataElement"] = ""
MyElement.Properties.Item["Hidden"] = True
MyElement.Properties.Item["ExcludedFromSummaries"] = True
MyCategoricalVariable.Elements.Add(MyElement)

'sumx required for mean and standard deviation
Set MyElement = MyDocument.CreateElement("SumX")
MyElement.Type = MDMLib.ElementTypeConstants.mtAnalysisSummaryData
Set MyElement.MultiplierReference = MyNumericVariable
MyElement.Properties.Item["CalculationType"] = "SumX"
MyElement.Properties.Item["DataElement"] = ""
MyElement.Properties.Item["Hidden"] = True
MyElement.Properties.Item["ExcludedFromSummaries"] = True
MyCategoricalVariable.Elements.Add(MyElement)

'sumn required for mean and standard deviation
Set MyElement = MyDocument.CreateElement("SumN")
MyElement.Type = MDMLib.ElementTypeConstants.mtAnalysisSummaryData
MyElement.Properties.Item["CalculationType"] = "SumN"
MyElement.Properties.Item["DataElement"] = ""
MyElement.Properties.Item["Hidden"] = True
MyElement.Properties.Item["ExcludedFromSummaries"] = True
MyCategoricalVariable.Elements.Add(MyElement)

'mean element
Set MyElement = MyDocument.CreateElement("Mean")
MyElement.Type = MDMLib.ElementTypeConstants.mtAnalysisMean
MyElement.Properties.Item["CalculationType"] = "Mean"
MyElement.Properties.Item["HasNoData"] = True
MyElement.Properties.Item["ExcludedFromSummaries"] = True
MyCategoricalVariable.Elements.Add(MyElement)

'standard deviation element
Set MyElement = MyDocument.CreateElement("Stddev")
MyElement.Type = MDMLib.ElementTypeConstants.mtAnalysisStdDev
MyElement.Properties.Item["CalculationType"] = "Stddev"
MyElement.Properties.Item["HasNoData"] = True
MyElement.Properties.Item["ExcludedFromSummaries"] = True
MyCategoricalVariable.Elements.Add(MyElement)

'Save the Document
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\CreatingCategorical_With_Mean_From_Numeric.mdd")
MyDocument.Close()
Requirements
To run the examples in this topic, you need UNICOM Intelligence Data Model.
See also
Special elements and helper elements in metadata