Data Model > Accessing the UNICOM Intelligence Data Model > Working with the Metadata Model > Working with the Metadata Model: Tutorial > Shared category lists
 
Shared category lists
Many questionnaires contain categorical questions that contain the same list of responses. The easiest way to handle this situation is to create a shared list, which you can then use as a template. The following mrScriptBasic code demonstrates how to do this for a shared category list that contains an Other Specify category.
Dim MyDocument, MyVariable
Dim MyElements, MyElement, MyOthervar

Set MyDocument = CreateObject("MDM.Document")

MyDocument.Languages.Add("ENU")
MyDocument.Languages.Base = "ENU"
MyDocument.Languages.Current = "ENU"
MyDocument.Contexts.Base = "Question"
MyDocument.Contexts.Current = "Question"

' Create an Elements collection object
Set MyElements = MyDocument.CreateElements("Teas") ' <== 1st highlighted line

' Create an Element object of type mtCategory and add
' it to the Elements collection
Set MyElement = MyDocument.CreateElement("green", "Green")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)

' Create some more Element objects of type mtCategory and
' add them to the Elements collection
Set MyElement = MyDocument.CreateElement("chinese", "Chinese")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)
Set MyElement = MyDocument.CreateElement("black", "Black")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)
Set MyElement = MyDocument.CreateElement("herbal", "Herbal")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)
Set MyElement = MyDocument.CreateElement("flavored", "Flavored")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)

' Create an Other Specify category
Set MyElement = MyDocument.CreateElement("other", "Other, please specify")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory

' Create a text variable and set the OtherVariable property
' of the Other Specify category to point to this variable
Set MyOthervar = MyDocument.CreateVariable("other", "Other specify")
MyOthervar.DataType = mr.Text
Set MyElement.OtherVariable = MyOthervar

' Add the Other Specify category to the Elements collection
MyElements.Add(MyElement)

' Create a No Answer special response category and add
' it to the Elements Collection
Set MyElement = MyDocument.CreateElement("na", "Not answered")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Flag = MDMLib.CategoryFlagConstants.flNoanswer
MyElements.Add(MyElement)

' Add the Elements collection to the Document's Types
' collection
MyDocument.Types.Add(MyElements) ' <== 2nd highlighted line

' Create a multiple-response categorical question
Set MyVariable = MyDocument.CreateVariable("Which_Teas", _
"Which of these teas do you drink? (Select all that apply)")
MyVariable.DataType = mr.Categorical
MyVariable.MinValue = 1
MyVariable.MaxValue = 5

' The multiple-response categorical question uses the
' shared category list that we have just created
Set MyVariable.Elements.Reference = MyElements ' <== 3rd highlighted line
MyDocument.Fields.Add(MyVariable)

' Create a single-response categorical question
Set MyVariable = MyDocument.CreateVariable("Favorite", _
"Which is your favorite type of tea? (Select one answer only)")
MyVariable.DataType = mr.Categorical
MyVariable.MinValue = 1
MyVariable.MaxValue = 1

' The single-response categorical question uses the
' shared category list that we have just created
Set MyVariable.Elements.Reference = MyElements ' <== 4th highlighted line
MyDocument.Fields.Add(MyVariable)

MyDocument.Save("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\My Shared List.mdd")
MyDocument.Close()
A similar example in VB.NET is:
Private Sub Create_Shared_List()
Dim MyDocument As MDMLib.Document
Dim MyVariable As MDMLib.IVariable2
Dim MyElements As MDMLib.Elements
Dim MyElement As MDMLib.Element
Dim MyOthervar As MDMLib.IVariable2

MyDocument = New MDMLib.Document

MyDocument.Languages.Add("ENU")
MyDocument.Languages.Base = "ENU"
MyDocument.Languages.Current = "ENU"
MyDocument.Contexts.Base = "Question"
MyDocument.Contexts.Current = "Question"

' Create an Elements collection object
MyElements = MyDocument.CreateElements("Teas")

' Create an Element object of type mtCategory and add
' it to the Elements collection
MyElement = MyDocument.CreateElement("green", "Green")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)

' Create some more Element objects of type mtCategory and
' add them to the Elements collection
MyElement = MyDocument.CreateElement("chinese", "Chinese")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)
MyElement = MyDocument.CreateElement("black", "Black")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)
MyElement = MyDocument.CreateElement("herbal", "Herbal")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)
MyElement = MyDocument.CreateElement("flavored", "Flavored")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElements.Add(MyElement)

' Create an Other Specify category
MyElement = MyDocument.CreateElement("other", "Other, please specify")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory

' Create a text variable and set the OtherVariable property
' of the Other Specify category to point to this variable
MyOthervar = MyDocument.CreateVariable("other", "Other specify")
MyOthervar.DataType = MDMLib.DataTypeConstants.mtText
MyElement.OtherVariable = MyOthervar

' Add the Other Specify category to the Elements collection
MyElements.Add(MyElement)

' Create a No Answer special response category and add
' it to the Elements Collection
MyElement = MyDocument.CreateElement("na", "Not answered")
MyElement.Type = MDMLib.ElementTypeConstants.mtCategory
MyElement.Flag = MDMLib.CategoryFlagConstants.flNoanswer
MyElements.Add(MyElement)

' Add the Elements collection to the Document's Types
' collection
MyDocument.Types.Add(MyElements)

' Create a multiple-response categorical question
MyVariable = MyDocument.CreateVariable("Which_Teas", _
"Which of these teas do you drink? (Select all that apply)")
MyVariable.DataType = MDMLib.DataTypeConstants.mtCategorical
MyVariable.MinValue = 1
MyVariable.MaxValue = 5

' The multiple-response categorical question uses the
' shared category list that we have just created
MyVariable.Elements.Reference = MyElements
MyDocument.Fields.Add(MyVariable)

' Create a single-response categorical question
MyVariable = MyDocument.CreateVariable("Favorite", _
"Which is your favorite type of tea? (Select one answer only)")
MyVariable.DataType = MDMLib.DataTypeConstants.mtCategorical
MyVariable.MinValue = 1
MyVariable.MaxValue = 1

' The single-response categorical question uses the
' shared category list that we have just created
MyVariable.Elements.Reference = MyElements
MyDocument.Fields.Add(MyVariable)

MyDocument.Save(" [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Output\My Shared List.mdd")
MyDocument.Close()
End Sub
The first highlighted line creates a new Elements collection and the following lines create the individual Element objects and add them to the collection. The Other Specify category is handled differently than in Creating a categorical question with Other Specify, where the text variable for the open-ended responses is added to the HelperFields collection. In a shared list, you cannot add the variable to the HelperFields collection, because the Elements collection does not belong to a variable. Instead we define the variable in the Element.OtherVariable property. When you set up the reference to the Elements collection from a variable, the text variable is automatically added to the HelperFields collection.
MyElement.Flag = MDMLib.CategoryFlagConstants.flNoanswer defines the Not answered category to be a special "No Answer" category.
The second highlighted line adds the Elements collection to the Document.Types collection. This defines the Elements collection as a template that you can reuse anywhere in the Document.
Because you have already defined the variable as a categorical variable, the line MyVariable.MaxValue = 5 defines the variable as a multiple response variable. The following line, which is highlighted, creates a reference to our Teas Elements collection, so that it is used as a template. A similar line (also highlighted) for the next variable also uses the Teas Elements collection as a template. Notice that for this variable MaxValue is set to 1, which defines it as a single response variable.
Requirements
See Requirements.
Optional: Microsoft Visual Basic .NET 2003.
Next
Sorting Element and VariableInstance objects
See also
Working with the Metadata Model: Tutorial
Creating and saving a variable