Desktop User Guides > Professional > Data management scripting > Working with the Weight component > Weight component examples > Creating a new variable to hold the weighting information
 
Creating a new variable to hold the weighting information
The Weight component does not create a new variable to hold the weighting information. It needs a suitable numeric variable (of type Double) to already exist in both the metadata and the case data.
This topic shows you how to create the new variable, first in the MDM Document and then in the case data. You can easily incorporate similar code in the script that you use to set up the weighting. However, example scripts for creating the variables in the metadata and the case data are shown here separately for clarity.
Creating the new variable in the MDM document
The following mrScriptBasic example creates a variable called Weight in a copy of the museum.mdd file.
' The output metadata document (.mdd) file
#define OUTPUTMDM "C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\museum_weight.mdd"

' The output case data (.ddf) file
#define OUTPUTDDF "C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\museum_weight.ddf"

' Note: After running this script, run Weight1b.mrs to add
' the Weight variable to the case data file (museum_weight.ddf).

' Copy the museum.mdd and museum.ddf sample files so that
' the weight examples will not update the original files...
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Data Collection File\museum.mdd", _
OUTPUTMDM, True)
fso.CopyFile("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Data Collection File\museum.ddf", _
OUTPUTDDF, True)

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

Dim MDM2, WgtVar, MyDataSource

' Create the MDM object
Set MDM2 = CreateObject("MDM.Document")

' Open the Museum.mdd file in read-write mode
MDM2.Open(OUTPUTMDM)

' Create the new variable
Set WgtVar = MDM2.CreateVariable("Weight", "Weighting variable")

' Specify the DataType
WgtVar.DataType = mr.Double

' Specify the usage type as VariableUsageConstants.vtWeight, so
' that it is recognized as a weight variable in Survey Tabulation, etc.
WgtVar.UsageType = BitOr(WgtVar.UsageType, _
MDMLib.VariableUsageConstants.vtWeight)

' Add the new variable to the Fields collection
MDM2.Fields.Add(WgtVar)

' Add a new data source object to the MDM document
' to refer to museum_weight.ddf
MDM2.DataSources.Remove(0)
MDM2.DataSources.AddNew("mrDataFileDsc", "mrDataFileDsc", "museum_weight.ddf", 0)

' Save the metadata file
MDM2.Save()
MDM2.Close()
This example is in the UNICOM Intelligence Developer Documentation Library as a sample script called Weight1a.mrs. For more information, see Sample mrScriptBasic files.
The function is used to set the variable’s usage type to vtWeight. This means that the variable will be recognized as a weighting variable in UNICOM Intelligence Reporter - Survey Tabulation and InterviewReporter. BitOr is used instead of just setting WgtVar.UsageType = MDMLib.VariableUsageConstants.vtWeight. The reason is to preserve any existing flags that are set in WgtVar.UsageType.
The script copies the Museum sample case data file (museum.ddf) to another file called museum_weight.ddf, which will be used by the weight example scripts in the following topics. This ensures that the original museum.ddf file is not updated.
Creating the new variable in the case data
The following mrScriptBasic example displays the Data Link Properties dialog, and then runs the xp_syncdb extended stored procedure to synchronize the case data with the metadata. If you run this script after running Weight1a.mrs, and select the appropriate metadata (museum_weight.mdd) and case data (museum_weight.ddf) in the Data Link Properties dialog, the corresponding variable is created in the case data.
Dim Wizard, ConnectionString

' Use the Data Link Properties dialog to get the connection string
Set Wizard = CreateObject("mrOleDB.DataLinkHelper")
ConnectionString = Wizard.DisplayWizard()

If Len(ConnectionString) > 0 Then
' Create a connection and open a recordset against VDATA
Dim adoConnection
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Open(ConnectionString)
adoConnection.Execute("exec xp_syncdb")
End if
A similar example is in the UNICOM Intelligence Developer Documentation Library as a sample script called Weight1b.mrs. For more information, see Sample mrScriptBasic files.
Running the xp_syncdb extended stored procedure on an existing data set requires that the CDSC that you are using is write-enabled and supports changing the layout of the data in existing records (Can Update and DDL Column are True for the CDSC). For more information, see Supported features of the CDSCs.
Requirements
UNICOM Intelligence Professional
See
Weight component examples