Professional > Data management scripting > Creating new variables > Example 3: Creating filter variables
 
Example 3: Creating filter variables
You use filters during analysis to select a subset of the case data. For example, if you want to create a table that includes only female respondents, you would filter on the Female category of the Gender variable. You can use the DMS file to set up filters as Boolean variables, which can then be reused as filters. This is particularly useful when the same filters will be required again and again and when the analysis requires the use of filters that are based on complex expressions.
Typically, you set up Boolean variables for use as filters as dynamically derived variables. You do this in the Metadata section using the expression keyword. For example, the following Metadata section creates a Boolean variable that could be used to select female respondents between the ages of 11 and 34:
Metadata(ENU, Question, Label, myInputDataSource)
   YoungWomen1 "Young women filter - dynamically derived" boolean
      expression ("Gender = {Female} And Age * {E1116_years,
      E1720_years, E2124_years, E2534_years}"); End Metadata
You can also create the Boolean variable without the expression keyword in the Metadata section and set up the case data in the OnNextCase Event section. However, setting up the case data in the OnNextCase Event section is generally slower than using an expression in the Metadata section, because the OnNextCase Event section code is executed separately for each individual case data record.
Metadata(ENU, Question, Label, myInputDataSource)
   YoungWomen2 "Young women filter - persisted derived" boolean;
End Metadata

Event(OnNextCase, "Set up the case data")
   If gender = {Female} Then
      If Age * {E1116_years, E1720_years, E2124_years,             E2534_years} Then
         YoungWomen2 = True
      Else
         YoungWomen2 = False
      End If
   Else
      YoungWomen2 = False
   End If
End Event
Working with null values
When evaluating a dynamically derived Boolean variable whose expression is based on a comparison operator, the UNICOM Intelligence Data Model assigns a value of False to any cases for which the expression does not evaluate to True. This happens even when the value being compared in the case data is Null, because the Data Model uses the SQL-92 ANSI standard when a comparison operator is used with a Null value. For more information, see Null values.
A Null value in the case data typically means that the respondent was not asked the question. This is unlikely to be an issue in the YoungWomen1 example, because generally all respondents are asked the demographic questions on which the expression is based.
However, sometimes you may want to base a Boolean variable on a question that some respondents were not asked. For example, in the Museum survey, the remember question was asked only if the respondent was interviewed as he or she left the museum. Suppose we want to base a Boolean variable on this variable to select respondents who remember seeing the Fossils gallery. If we simply define the expression as remember >= {fossils}, the Boolean variable will have a False value for all respondents regardless of whether they were asked the question or not. This could lead to misleading results if, for example, we were to use the Boolean variable to calculate the percentage of people who remembered the Fossils gallery.
You can avoid this problem, by using the IIf function to set the value of the Boolean variable to Null for cases for which the remember variable has a Null value. For example:
Metadata(ENU, Question, Label, myInputDataSource)
   RememberFossils "People who remember seeing fossils" boolean
      expression ("IIf(remember IS NULL, NULL, remember >=
         {Fossils})");
End Metadata
The examples in this topic are in the NewVariables.dms sample DMS file that is in the UNICOM Intelligence Developer Documentation Library. For more information, see Sample DMS files.
Requirements
UNICOM Intelligence Professional
See also
Creating new variables