Professional > Data management scripting > Working with the Weight component > Weight component examples > Simple target weighting example
 
Simple target weighting example
This simple mrScriptBasic example sets up target weighting (see Target weighting) in a variable called Weight that already exists in both the metadata and the case data. You will get an error if you attempt to run the example when this variable does not exist. (For an example that creates this variable, see Creating a new variable to hold the weighting information.) The weighting is based on equal numbers of male and female respondents. Here is the weighting matrix:
Gender
Target
Male
301
Female
301
Here is the mrScriptBasic code:
' This script weights the files created by Weight1a.mrs '
' and Weight1b.mrs

Dim MDM2, WgtEng, Wgt, fso, ReptFile

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

' Open the .mdd file created by Weight1a.mrs
MDM2.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\
   museum_weight.mdd")

' Create an instance of the Weight Engine object
Set WgtEng = CreateObject("mrWeight.WeightEngine")

' Initialize the Weight Engine
WgtEng.Initialize (MDM2)

' Create an instance of the Weight object with the
' following parameters:
' "Weight" is a variable of type Double that already exists
' in both the metadata and the case data
' "gender" is the variable that stores the characteristics
on which we wish to base the weighting
' 2 defines the weighting method as target weighting
Set Wgt = WgtEng.CreateWeight("Weight", "gender", 2)

' Create a text file for the weighting report
Set fso = CreateObject("Scripting.FileSystemObject")
Set ReptFile = fso.CreateTextFile("C:\Program Files\IBM\SPSS\
   DataCollection\7\DDL\Output\Weight2.htm", True)

' Define the targets for the 2 cells of the weighting matrix Wgt.CellRows.Targets = "301; 301"
' Call the WeightEngine.Prepare method
WgtEng.Prepare(Wgt)
' Write the weighting report to the text file ReptFile.Write(Wgt.Report)
' Call the WeightEngine.Execute method
WgtEng.Execute(Wgt)
ReptFile.Write(Wgt.Report)
ReptFile.Close()
' Close the database connection and flush pending data updates
Set WgtEng = Null MDM2.Close()
This example is in the UNICOM Intelligence Developer Documentation Library as a sample script called Weight2.mrs. For more information, see Sample mrScriptBasic files.
Here is a table of age by gender run on the Museum example data weighted using the Weight variable:
In the unweighted table, shown in Weight component examples, there were 339 male and 263 female respondents in the Base row; now there are equal numbers of male and female respondents in the Base row.
The Wgt.CellRows.Targets = "301; 301" line in the example script defines targets of 301 respondents for each of the two cells of the weighting matrix. The weighted total is the sum of the targets (301 + 301 = 602) because this is the default behavior and we have not specified a different option in the Weight.TotalType property.
If you want the weighted total to be 1000, with an even distribution between the male and female respondents, you could simply change the Wgt.CellRows.Targets = "301; 301" line to:
Wgt.CellRows.Targets = "500; 500"
If you want the targets to represent proportions, you must specify the weighted total. One way of doing this is to specify that the weighted total should equal the unweighted total. This is useful when you want to specify your targets as proportions of the unweighted total but you do not know what the unweighted total is, perhaps because the data is still being collected. You do this by setting the Weight.TotalType property to 2. For example, replace the Wgt.CellRows.Targets = "301; 301" line with the following:
Wgt.CellRows.Targets = "50; 50"
Wgt.TotalType = 2
If you now run a table of age by gender on the weighted data, the results will be the same as in the table shown above. If, however, you want your weighted total to be 5000, replace these lines with the following:
Wgt.CellRows.Targets = "50; 50"
Wgt.TotalType = 3
Wgt.WeightedTotal = 5000
In the next topic, More on target weighting, you will learn how to specify target weights using more than one variable in the weighting matrix and how to specify targets using the names of the categories.
Requirements
UNICOM Intelligence Professional
See also
Weight component examples