Scripting > mrScriptBasic overview > mrScriptBasic examples > 4: Creating a derived variable
 
4: Creating a derived variable
This example creates a derived categorical variable called YoungPeople in the Museum sample data set. For a Visual Basic version of this example and general information about creating derived variables, see Dynamically derived variables.
For information on the MDM objects, see the UNICOM Intelligence Developer Documentation Library.
Code
This example is included with the UNICOM Intelligence Developer Documentation Library as a sample script called DerivedVariable.mrs. See Sample mrScriptBasic files for more information.
' The input metadata document (.mdd) file
#define INPUTMDM "C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Data Collection File\museum.mdd"

' The output metadata document (.mdd) file
#define OUTPUTMDM "C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\museum_DerivedVariable.mdd"

' Copy the museum.mdd sample file so that
' we do not update the original file...
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile(INPUTMDM, OUTPUTMDM, True)

' Make sure that the read-only attribute is not set
Set f = fso.GetFile(OUTPUTMDM)
If f.Attributes.BitAnd(1) Then
f.Attributes = f.Attributes - 1
End If

Dim Expression, strError
Dim DerivedVariable, MDM2

' Create the MDM object
Set MDM2 = CreateObject("MDM.Document")
MDM2.Open (OUTPUTMDM) ' Line 24

' Add the derived variable to the MDM Document
Set DerivedVariable = MDM2.CreateVariable("YoungPeople", "Young People") ' Line 27
MDM2.Fields.Add (DerivedVariable)
DerivedVariable.DataType = MDMLib.DataTypeConstants.mtCategorical

' For categorical variables there's usually more than
' one category, so use sExpressions option
DerivedVariable.SourceType = MDMLib.SourceTypeConstants.sExpressions

' Set up the derived categories ' Line 35
Expression = "(age = {E1116_years} AND gender = {Male}) OR (age = {E1720_years} AND gender = {Male})"
CreateDerivedCategory (MDM2, DerivedVariable, "YoungMen", "Young Men", Expression)

Expression = "(age = {E1116_years} AND gender = {Female}) OR (age = {E1720_years} AND gender = {Female})"
CreateDerivedCategory (MDM2, DerivedVariable, "YoungWomen", "Young Women", Expression)

' Save the MDM Document
MDM2.Save () ' Line 43
MDM2.Close()

Sub CreateDerivedCategory (MDM2, DerivedVariable, CategoryName, CategoryLabel, Expression)
Dim Category
' Create a new category
Set Category = MDM2.CreateElement (CategoryName, CategoryLabel)
DerivedVariable.Elements.Add (Category)

' Add the expression for the category
DerivedVariable.Expressions.Add (CategoryName, Expression)
End Sub
Line 24
Calls the MDM Document.Open method to open the copy of the Museum example .mdd file in read-write mode. (We do not need to specify the mode explicitly because read-write is the default.)
Line 27
Calls the MDM Document.CreateVariable method to create the variable. The next line adds the variable to the Document's Fields collection and the line after that sets the variable's data type to categorical using the MDM mtCategorical constant. Notice that in mrScriptBasic, when you use a constant that is defined in a type library that is registered in the script, you need to include the name of the type definition. For example, if this example had excluded the “MDMLib.DataTypeConstants” name and specified only “mtCategorical”, mrScriptBasic would give an “Unidentified variable” error. (The MDM Type Library is automatically registered in the script when you use the mrScript Command Line Runner /m: option to specify an .mdd file.)
Line 35
Defines an expression for the first category in the derived variable and the next line calls the CreateDerivedCategory Sub procedure, which creates the category. Similar code is used to define and create the second category. Notice that the parameters are enclosed in () (parentheses). Unlike VBScript, parentheses are required in mrScriptBasic for all call statements.
Line 43
Calls the Document.Save method to save the MDM Document.
Line 46
The CreateDerivedCategory Sub procedure.
See also
mrScriptBasic examples