Desktop User Guides > Professional > Table scripting > Working with metadata > Working with versions > Changing the base to reflect the version
 
Changing the base to reflect the version
Sample script file: PercentageOnElementsAsked.mrs
This example script is based on the Short Drinks sample data set. To run examples using the Short drinks sample data set, you need access to a SQL Server installation on which the Short Drinks sample database has been restored, and appropriate user access rights. For more information, see Restoring the Short Drinks sample database. For information on running the example scripts, Running the sample table scripts.
In UNICOM Intelligence Reporter, you can use the IsElementInVersions function to determine whether a particular element exists in a particular version of the metadata. This can be used to take into account the fact that some categories or all variables might not be available in all versions of a questionnaire. For example, in the Short Drinks data set the Socioeconomic Class variable (sclass) occurs in versions 1-4 of the questionnaire and the Fulltime Parent category of that variable exists only in versions 3 and 4.
One way to use this function is in an expression that defines a base element, for example:
base('<variable_name>.IsElementInVersions
({category_name}, DataCollection.MetadataVersionNumber)
AND <variable_name> IS NOT NULL)')
In these examples, the expression is presented on multiple lines for clarity. In practice you should specify the expression without line breaks.
For each record in the case data, the IsElementInVersions function checks to see if the specified category was available in the version of the questionnaire that the respondent was asked (this is determined by the value of the DataCollection.MetadataVersionNumber variable). If the category was available and the question itself was asked, the expression returns True, otherwise it returns False. The base element counts the number of respondents for which the expression is True.
The following expression shows the test being carried out on the Fulltime Parent category of the Socioeconomic class variable.
base('sclass.IsElementInVersions
({PARENT},DataCollection.MetaDataVersionNumber)
AND (sclass IS NOT NULL)')
An example of the output of this expression for the Fulltime Parent category for selected respondents is shown in the following table.
Respondent
Response
Metadata version
Result of Expression
15
{w_collar}
2
False
61
{not_work}
3
True
62
{parent}
3
True
68
null
3
False
87
{shopkeeper}
4
True
98
null
5
False
For respondents 15 and 98 (who were asked metadata versions 2 and 5) the expression returns False because the parent category did not exist in those versions. For respondent 68 the expression returns False because the respondent was not asked the sclass question (the response is null). For respondents 61, 62, and 87, the expression returns True because the metadata version asked contains the parent category (this is independent of the actual answer given).
Example
The PercentageOnElementsAsked.mrs sample script is based on the Short Drinks data set. It uses the IsElementInVersions function to define a unique base for each category in the sclass variable, reflecting the number of times the category was available as a possible answer to respondents who were asked the question about socioeconomic class. The script then uses this unique base to calculate percentages for the number of respondents whose answers fell into each category, compared to the number of respondents who were offered the category as a response.
The script creates three tables, all with Socioeconomic Class on the side axis and Metadata Version on the top axis. In the first table, the percentages for each category are calculated using the number of respondents who were asked the question about socioeconomic class:
TableDoc.Tables.AddNew("Table01", _
"sclass * DataCollection.MetadataVersion", _
"The variable sclass tabulated as normal")
Table showing Socioeconomic class by Metadata version. Percentages are calculated based on number of respondents who were asked the question
This graphic is described in the surrounding text.
The overall percentage across for the Fulltime Parent category is 5%. This figure represents the percentage for this category across all versions, including those in which it was not available.
In the second table, an axis expression is used to add unique base elements associated with each category element. These bases show the number of times each sclass category was asked. The percentages for each category are then calculated using the number of respondents who were asked the particular category, rather than the entire question:
TableDoc.Tables.AddNew("Table02", _
CreateAxisSpec(TableDoc, "sclass", True, "") + " * DataCollection.MetadataVersion", _
"The variable sclass with individual element bases inserted and percentaged on")
Table showing Socioeconomic class by Metadata version. Percentages are calculated based on number of respondents who were offered the category as a response to the question
This graphic is described in the surrounding text.
The overall percentage for the Fulltime Parent category is now 12%, representing the number of times the category was answered compared to the number of times it was available to be answered.
The third table uses a similar axis expression, but this time it prevents the new base elements from being displayed in the table, while still using them to calculate the percentages for each category:
Set Table = TableDoc.Tables.AddNew("Table03", _
  CreateAxisSpec(TableDoc, "sclass", False, " *") + _
" * DataCollection.MetadataVersion", _
  "The variable sclass with the individual element bases hidden")
An asterisk (*) is added to the label for any element that was not included in all the versions of the metadata that appear in the table. An annotation provides a key for this symbol in the table:
Table.Annotations[annTitleFooter].Specification = "* - Category does not exist in all versions"
Table showing Socioeconomic class by Metadata version. Percentages are calculated based on number of respondents who were offered the category as a response to the question. Bases are hidden.
This graphic is described in the surrounding text.
The preceding tables show the results of running the script on versions 1-4 of the data. You can see the effect of running this script on different versions of the questionnaire by changing the MDD_VERSION constant at the start of the script.
' Const MDD_VERSION = "{..}" ' All versions of the metadata
Const MDD_VERSION = "{1..4}" ' sclass variable exists in versions 1 to 4
' Const MDD_VERSION = "{3,4}" ' "Fulltime Parent" category only exists in versions 3 and 4
See also
Working with versions