Desktop User Guides > Professional > Data management scripting > Working with the Weight component > Weight component examples > Setting up weighting in a DMS file
Setting up weighting in a DMS file
This topic shows a DMS file that sets up the Weight variable and the target weighting shown in Simple target weighting example. The Weight metadata and case data variable is defined in the Metadata Section using one line of mrScriptMetadata code. You do not need to explicitly define the case data variable because UNICOM Intelligence Professional automatically synchronizes the output case data with the output metadata.
You must not call the Initialize method when you set up the weighting in a DMS file because UNICOM Intelligence Professional initializes the weight engine with the output metadata. You get an error if you call the Initialize method in a DMS file.
To close the database connection and flush any pending data updates at the end of the Event section, you must set Wgt to null. This can either be done explicitly by saying WgtEng = null, or implicitly by making it go out of scope. Also, there is a known issue that Wgt.Report must be accessed in some way once Wgt has been created with CreateWeight(); otherwise the database connection is still not closed even is you have correctly set WgtEng = null.
' In the Metadata section, create a numeric variable called "Weight"
' to hold the weighting information...

Metadata (enu, Analysis, Label, Input)
Weight "Weighting based on gender balance"
Double usagetype("Weight");
End Metadata

' In the OnJobEnd event, use the Weight component to set up weighting
' and write a weighting report...

Event(OnJobEnd, "Weight the data")
Dim WgtEng, Wgt, fso, ReptFile
Set WgtEng = dmgrJob.WeightEngine

' Create an html file to contain the weighting report...

Set fso = CreateObject("Scripting.FileSystemObject")
Set ReptFile = _
fso.CreateTextFile("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\WeightingReport.htm", _

' Create an instance of the Weight object with the following parameters:
' "Weight" is the variable of type Double that was created in the
' metadata section.
' "gender" is the variable that stores the characteristics on
' which we wish to base the weighting.
' wtMethod.wtTargets defines the weighting method as target weighting.

Set Wgt = WgtEng.CreateWeight("Weight", "gender", wtMethod.wtTargets)

' Define a two cell weighting matrix with a target of 301 for
' each value of the gender variable...

Wgt.CellRows.Targets = "301; 301"

' Call the WeightEngine.Prepare method and then write the
' weighting report to the html file...


' Call the WeightEngine.Execute method, which will insert the
' calculated weight values in the Weight variable. Then
' write the weighting report to the html file...



' Setting WgtEng to Null ensures that the connection to the
' output data source is closed and that any pending data
' updates are flushed...

set WgtEng = Null
End Event

' The input data source is the museum.ddf sample
' data set...

ConnectionString = "Provider=mrOleDB.Provider.2; _
Data Source=mrDataFileDsc; _
Location=C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Data Collection File\museum.ddf; _
Initial Catalog=C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Data\Data Collection File\museum.mdd"
End InputDataSource

' The output data source...

ConnectionString = "Provider=mrOleDB.Provider.2; _
Data Source=mrDataFileDsc; _
Location=C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Weighting.ddf"
MetaDataOutputName = "C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Weighting.mdd"
End OutputDataSource
This example is provided as a sample DMS file (called Weighting.dms) that is installed with the UNICOM Intelligence Developer Documentation Library. For more information, see Sample DMS files.
UNICOM Intelligence Professional
Weight component examples