Desktop User Guides > Professional > Table scripting > Getting started > Weighting tables
Weighting tables
Sample script file: WeightedTables.mrs
Weighting is another term for sample balancing. You use it when you want the figures in your tables to be an accurate reflection of the target population. For example, of the 602 respondents interviewed in the Museum survey, 56.31% were male and 43.69% were female. However, you might want to adjust the figures to reflect an even balance between the genders. You can do this using the genbalance variable, which is a special weighting variable that has been set up to inflate the responses from the female respondents and deflate the responses from the male responses so that when you use it to weight your tables, they will reflect an even balance between the genders.
Table of age by gender, weighted using genbalance variable
To use the genbalance variable to weight a table of age by gender like the one shown in Creating a simple table of Age by Gender, you could use this script:
TableDoc.Tables.AddNew("Table1", "age * gender", "Weighted table of Age by Gender")
TableDoc.Table1.Weight = "GenBalance"
This example refers to the table in the Tables collection of the Table Document using its name. This is possible only in mrScriptBasic because of its dynamic property expansion feature and is equivalent to referring to the table by its index in the collection. For example, the following lines are equivalent:
TableDoc.Table1.Weight = "GenBalance"
TableDoc.Tables.Table1.Weight = "GenBalance"
TableDoc.Tables[0].Weight = "GenBalance"
TableDoc.Tables["Table1"].Weight = "GenBalance"
Here is the table produced by the script:
This graphic is described in the surrounding text.
Notice that in this example the counts are shown with two decimal places by changing the number of decimal places for the counts default cell item:
TableDoc.Default.CellItems[0].Decimals = 2
Table of age by gender, weighted using genbalance variable, with weighted and unweighted counts
If you compare the results with the unweighted table we created in Creating a simple table of Age by Gender, you will see that an Unweighted Base row and column have been added to the table automatically and the base row now shows equal numbers of male and female respondents. If a table is weighted, counts created using the Count keyword are always weighted. However, you can add unweighted counts to the table. Here is the script to create a similar table, but this time showing the unweighted counts as well as the weighted counts:
TableDoc.Tables.AddNew("Table2", "age * gender", "Weighted table showing unweighted counts")
TableDoc.Table2.Weight = "GenBalance"
TableDoc.Table2.CellItems.AddNew(8, 2) ' itUnweightedCount = 8
Here is the table produced by the script:
This graphic is described in the surrounding text.
By default, an unweighted base element is added to all weighted tables. This is because it is good practice to show the unweighted base values in addition to the weighted base values. However, the unweighted base elements are not necessary in this table, because we are showing the unweighted counts. You can stop the unweighted base elements being added to the table by setting the AutoUnweightedBases table property (see Table properties) to False. The unweighted base element is added when you add the weighting to the table, so you need to set the property before you weight the table. For example:
With TableDoc.Tables
.AddNew("Table3", "age * gender", "Weighted table without automatic
       unweighted base elements")
.Table3.Properties["AutoUnweightedBases"] = False
.Table3.Weight = "GenBalance"
.Table3.CellItems.AddNew(8, 2) ' itUnweightedCount = 8
End With
Note that if we were not showing the unweighted counts in the table, we should include the unweighted base elements, in order to comply with good practice guidelines. See Base element for more information.
By default, details of the weighting variable are shown in the right header position. See Annotation positions for more information.
Weighting variables must be numeric variables. However, not all numeric variables are suitable for use as weights. Generally, weighting variables are created specially, typically using the Weight component. See Working with the Weight component for more information.
UNICOM Intelligence Reporter
Working with numeric variables
See also
Getting started