Desktop User Guides > Professional > Data management scripting > Getting started with Data Management scripting > 7. Learning about weighting
 
7. Learning about weighting
Weighting is another term for sample balancing. You use weighting when you want the figures in your table to reflect your target population more accurately than the actual figures do. For example, suppose your target population consists of 57% women and 43% men, but you interviewed 50% women and 50% men for your survey. By applying weighting, you can make the women's figures count for more than the men's figures, so that they more accurately reflect the gender distribution in the target population.
UNICOM Intelligence Professional includes the Weight component, which enables you to set up weighting in your data. The following example uses the Weighting.dms file. This sets up weighting based on equal numbers of male and female respondents.
1 Open the Weighting.dms file in UNICOM Intelligence Professional. By default, the file is in the [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Scripts\Data Management\DMS folder.
This DMS file contains a Metadata section:
Metadata (enu, Analysis, Label, Input)
  Weight "Weighting based on gender balance"
         Double usagetype("Weight");
End Metadata
This creates in the output data source a numeric variable, called Weight, to hold the weighting information that will be set up using the Weight component in the OnJobEnd Event section.
Here is the OnJobEnd Event section code that calls the Weight component and sets up weighting in the Weight variable that is defined in the Metadata section:
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", _
True)

' 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 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...

WgtEng.Prepare(Wgt)
ReptFile.Write(Wgt.Report)

' 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...

WgtEng.Execute(Wgt)
ReptFile.Write(Wgt.Report)

ReptFile.Close()

' 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
2 Run the Weighting.dms file.
3 If you have the UNICOM Intelligence Professional Tables Option, you can use the DMSWeightedTables.mrs table scripting sample mrScriptBasic file to create two tables of Age by Gender, the first unweighted and the second weighted using the Weight variable just set up. (If you do not have the UNICOM Intelligence Professional Tables Option, you can use DM Query instead: see step 5.) To run the DMSWeightedTables.mrs sample:
Open the DMSWeightedTables.mrs file in UNICOM Intelligence Professional. (The table scripting sample files are typically installed in the [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\Scripts\Tables folder.)
Press Ctrl+F5 or choose Start Without Debugging from the Debug menu.
Here is the unweighted table:
This graphic is described in the surrounding text.
Here is the weighted table:
This graphic is described in the surrounding text.
4 In the unweighted table, there are 339 male and 263 female respondents in the Base row, whereas in the table weighted using the Weight variable, there are equal numbers of male and female respondents in the Base row.
5 If you do not have the UNICOM Intelligence Professional Tables Option, create equivalent tables in DM Query.
To set up DM Query, see How to run the example queries in DM Query using the museum sample.
However, remember to select the output data source files (Weighting.mdd and Weighting.ddf) rather than the installed Museum sample files.
6 To create the unweighted table, enter the following:
SELECT groupby.col[0] AS Age,
  SUM(gender = {male}) AS Male,
  SUM(gender = {female}) AS Female
  FROM vdata
  GROUP BY age ON age.DefinedCategories()
  WITH (BaseSummaryRow)
Here are the results:
Age
Male
Female
{}
339
263
{e116_years}
23
15
{e1720_years}
50
32
{e2124_years}
51
44
{e2534_years}
108
84
{e3544_years}
49
42
{e4554_years}
32
23
{e5564_years}
16
17
{e56_years}
10
6
7 To create the weighted table in DM Query, enter the following:
SELECT groupby.col[0] AS Age,
  SUM((gender = {male}) * Weight) AS Male,
  SUM((gender = {female}) * Weight) AS Female
  FROM vdata
  GROUP BY age ON age.DefinedCategories()
  WITH (BaseSummaryRow)
Here are the results:
Age
Male
Female
{}
301.000000000002
301.000000000002
{e116_years}
20.4218289085546
17.1673003802281
{e1720_years}
44.3952802359881
36.6235741444867
{e2124_years}
45.2831858407079
50.3574144486692
{e2534_years}
95.8938053097344
96.1368821292775
{e3544_years}
43.5073746312684
48.0684410646388
{e4554_years}
28.4129793510324
26.3231939163498
{e5564_years}
14.2064896755162
19.4562737642586
{e56_years}
8.87905604719764
6.86692015209126
The difference in decimal precision is because the UNICOM Intelligence Professional Tables Option post-processes the results. For more information, see Advanced SQL queries.
You could include code in the OnAfterJobEnd Event section in your DMS file to set up the tables. For more information, see OnAfterJobEnd Event section.
The Weight component automatically creates a weighting report. The OnJobEnd Event section contains the following line:
ReptFile.Write(Wgt.Report)
This writes the report to an HTML file called Weighting.htm.
8 Open the weighting report file. To do this, in Windows Explorer go to the location of the weighting report file (typically this is [INSTALL_FOLDER]\IBM\SPSS\DataCollection\7\DDL\\Output) and double-click WeightingReport.htm. This opens the file in your default browser.
For more information, see Weighting report.
9 To find out more about weighting, see Working with the Weight component, which includes a general introduction to weighting, details of the various weighting methods and their formulae, and examples of using each of the weighting methods. Most of the examples are in the form of mrScriptBasic (.mrs) files. For information about how to incorporate the examples into a DMS file, see Setting up weighting in a DMS file.
Next
8. Mastering mrScriptMetadata.
See
Getting started with Data Management scripting