Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Creating a categorical question with Other Specify
 
Creating a categorical question with Other Specify
The MDM allows you to associate (or reference) one variable from another variable by using helper fields. The reason for helper fields is that sometimes variables are very closely linked and the information about the linkage needs to be stated. An example of this is an Other Specify category in a categorical question. An Other Specify category requires a text variable to hold the text of the respondent's answer. This text variable may also be coded, so an additional categorical question is needed to hold the codes created from the text variable. If the questionnaire is to be scanned, the text may be held as an image file and the filename of this image file is stored in another variable (called the source file variable).
The following mrScriptBasic example creates a categorical question called Products where one of the categories is an Other Specify. Then it creates and sets up the additional variables required to support the Other Specify category, including a text variable (for holding the open-ended response), a categorical variable (for coding the text variable), and the source file variable, which is a text variable for holding the filename of the file that contains the scanned image of the text.
Dim MyDocument, MyCategoricalVar, MyElement

' Helper fields for MyCategoricalVar
Dim MyOtherVar, MyCodesVar, MySourceFileVar

Dim MyRoutingItem, MyField

Set MyDocument = CreateObject("MDM.Document")
MyDocument.Contexts.Base = "Question"
MyDocument.Contexts.Current = "Question"

'Create a multiple response categorical variable called "Products"
Set MyCategoricalVar = MyDocument.CreateVariable("Products", _
"Which of our products have you purchased?")
MyCategoricalVar.DataType = mr.Categorical
MyCategoricalVar.MinValue = 0
MyCategoricalVar.MaxValue = 4
' A max value greater than 1 and a data type of mtCategorical
' indicates multiple response
MyDocument.Fields.Add(MyCategoricalVar)

'Create the categories
Set MyElement = MyDocument.CreateElement("A", "Product A")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
' Now add the element to the question
MyDocument.Fields["Products"].Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("B", "Product B")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyDocument.Fields["Products"].Elements.Add(MyElement)

Set MyElement = MyDocument.CreateElement("C", "Product C")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyDocument.Fields["Products"].Elements.Add(MyElement)

' Include an Other Specify category
Set MyElement = MyDocument.CreateElement("OtherElement", _
"Other (please specify)")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyDocument.Fields["Products"].Elements.Add(MyElement)

' Create a text variable to hold the open-ended answer to
' the Other specify category
Set MyOtherVar = MyDocument.CreateVariable("Other")
' A label is not required because this question will
' never be asked
MyOtherVar.DataType = mr.Text
MyOtherVar.UsageType = MDMLib.VariableUsageConstants.vtOtherSpecify

' Add the Other helper variable to the HelperFields
' collection of the question
MyDocument.Fields["Products"].HelperFields.Add(MyOtherVar)

' Set the Other Specify element's OtherReference
' to point at its helper variable
Set MyElement.OtherReference = MyOtherVar

' Create a categorical variable that will be used when
' coding the above text variable "MyOtherVar"
Set MyCodesVar = MyDocument.CreateVariable("Codes")
' A label is not required because this question will
' never be asked
MyCodesVar.DataType = mr.Categorical
MyCodesVar.UsageType = MDMLib.VariableUsageConstants.vtCoding

MyOtherVar.HelperFields.Add(MyCodesVar)

' Create a text variable to hold the filename of the
' file that holds the image of the other specify text
' (for use when scanning using Paper/Scan)
Set MySourceFileVar = MyDocument.CreateVariable("SourceFile")
' A label is not required because this question will
' never be asked
MySourceFileVar.DataType = mr.Text
MySourceFileVar.UsageType = MDMLib.VariableUsageConstants.vtSourceFile

MyOtherVar.HelperFields.Add(MySourceFileVar)

'Add all of the questions to the Paper routing collection
For Each MyField In MyDocument.Fields
Set MyRoutingItem = CreateObject("MDM.RoutingItem")
Set MyRoutingItem.Item = MyField
MyDocument.Routing.Add(MyRoutingItem, "Paper")
Next

'Save the Document
MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Other Specify.mdd")
MyDocument.Close()
A similar example in VB.NET is:
Private Sub Create_OtherSpecify()
Dim MyDocument As MDMLib.Document
Dim MyCategoricalVar As MDMLib.IVariable2
Dim MyOtherVar As MDMLib.IVariable2 ' Helper field for MyCategoricalVar
Dim MyCodesVar As MDMLib.IVariable2 ' Helper field for MyCategoricalVar
Dim MySourceFileVar As MDMLib.IVariable2 ' Helper field for MyCategoricalVar
Dim MyElement As MDMLib.Element
Dim MyRoutingItem As MDMLib.RoutingItem
Dim MyField As MDMLib.MDMObject

MyDocument = New MDMLib.Document
MyDocument.Contexts.Base = "Question"
MyDocument.Contexts.Current = "Question"

'Create a multiple response categorical variable called "Products"
MyCategoricalVar = MyDocument.CreateVariable("Products", _
"Which of our products have you purchased?")
MyCategoricalVar.DataType = MDMLib.DataTypeConstants.mtCategorical
MyCategoricalVar.MinValue = 0
MyCategoricalVar.MaxValue = 4
' A max value greater than 1 and a data type of mtCategorical
' indicates multiple response
MyDocument.Fields.Add(MyCategoricalVar)

'Create the categories
MyElement = MyDocument.CreateElement("A", "Product A")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
' Now add the element to the question
MyDocument.Fields("Products").Elements.Add(MyElement)

MyElement = MyDocument.CreateElement("B", "Product B")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyDocument.Fields("Products").Elements.Add(MyElement)

MyElement = MyDocument.CreateElement("C", "Product C")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyDocument.Fields("Products").Elements.Add(MyElement)

' Include an Other Specify category
MyElement = MyDocument.CreateElement("OtherElement", _
"Other (please specify)")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyDocument.Fields("Products").Elements.Add(MyElement)

' Create a text variable to hold the open-ended answer to
' the Other specify category
MyOtherVar = MyDocument.CreateVariable("Other")
' A label is not required because this question will
' never be asked
MyOtherVar.DataType = MDMLib.DataTypeConstants.mtText
MyOtherVar.UsageType = MDMLib.VariableUsageConstants.vtOtherSpecify

' Add the Other helper variable to the HelperFields
' collection of the question
MyDocument.Fields("Products").HelperFields.Add(MyOtherVar)

' Set the Other Specify element's OtherReference
' to point at its helper variable
MyElement.OtherReference = MyOtherVar

' Create a categorical variable that will be used when
' coding the above text variable "MyOtherVar"
MyCodesVar = MyDocument.CreateVariable("Codes")
' A label is not required because this question will
' never be asked
MyCodesVar.DataType = MDMLib.DataTypeConstants.mtCategorical
MyCodesVar.UsageType = MDMLib.VariableUsageConstants.vtCoding

MyOtherVar.HelperFields.Add(MyCodesVar)

' Create a text variable to hold the filename of the
' file that holds the image of the other specify text
' (for use when scanning using mrPaper/mrScan)
MySourceFileVar = MyDocument.CreateVariable("SourceFile")
' A label is not required because this question will
' never be asked
MySourceFileVar.DataType = MDMLib.DataTypeConstants.mtText
MySourceFileVar.UsageType = MDMLib.VariableUsageConstants.vtSourceFile

MyOtherVar.HelperFields.Add(MySourceFileVar)

'Add all of the questions to the Paper routing collection
For Each MyField In MyDocument.Fields
MyRoutingItem = New MDMLib.RoutingItem
MyRoutingItem.Item = MyField
MyDocument.Routing.Add(MyRoutingItem, "Paper")
Next

'Save the Document
MyDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\Other Specify.mdd")
MyDocument.Close()
End Sub
Requirements
See Requirements.
Next
Shared category lists
See also
Working with the Metadata Model: Tutorial
Creating and saving a variable