Construct Logic Variable

From Q
(Redirected from Logic Variable)
Jump to navigation Jump to search

Logic Variables are used when creating custom categories in Banners, such as segments, lifestage segments, or other categories that are constructed using one or more inputs. They are only available in Q4.11 and more recent versions.

How to create a logic variable

Either:

  • When you have created a banner question, right-click on the banner question, and select Edit > BANNER > Add Category - Logic.
  • Right-click on a variable in the Variables and Questions tab, and select Insert Variable(s) > Logic Formula.
  • Create > Variables and Questions > Variable(s) > Logic Formula.

The logic

Referring to a single value

The most basic Logic Variable contains a simple Expression, this is either true or false for each case in the data. For example,

age(1)

is true for people with an age of 1 and false for people with an age of 2. Alternatively, this can be written as:

age = 1

Referring directly to a value, such as 1 or 2, in an expression can be error prone, and a better alternative is to instead refer to either the Label as shown in the Value Attributes (which may be different to on the table, if you have changed it), such as:

age([18 to 24])

or, equivalently:

age = [18 to 24]

Rather than typing such labels directly in, you can right click on the variable in the list above the Expression and select Insert Reference (but first ensure that Show value labels is ticked).

Another alternative is to instead refer to the Source Value (as shown in Value Attributes), which is denoted using a #. For example:

age(#1)

or, equivalently:

age = #1

The advantage of referring to the Source Value instead of the Value is that if the data is recoded, it will not cause the Expression's meaning to change. For example, if the Value for 18 to 24 was recoded from 1 to its midpoint of 21, this would change the meaning of age = 1 but not of age = #1.

Note, however, that where a Label is changed in the Value Attributes, this has no impact at all, as in its internals Q saves the Source Value when saving Logic Variables that refer to labels.

Show value labels When this is checked, Q converts numbers to their corresponding value labels, and vice versa.

Referring to variables in a Pick Any or Pick Any - Grid

Pick Any and Pick Any - Grid questions can only take values of 0, 1 and NaN. Consequently,they are referenced either by either directly referring to the Variable Name, or, one of these three values. For example, the following are all equivalent:

useCoke
useCoke(1)
NOT useCoke(0)

Note that there is no concept of Source Value or Label when creating a Logic Variable using Pick Any and Pick Any - Grid questions.

Referring to multiple values

Multiple values can be separate using commas. For example:

age(1, 2, 3, 9)
age(#1, #2, #3, #9)
age([18 to 24], [25 to 29], [30 to 34], [65 or more])

Where there are ranges, you only need to indicate the lowest and highest value. For example, where there are no decimals, we can rewrite the above as:

age(1-3, 9)
age([18 to 24]-[30-34],[65 or more])
age(#1-#3, #9)

AND, OR, NOT and inequalities (<=, =>)

The above expressions can be written in a variety of different ways using AND, OR, NOT and inequalities. For example:

age(1-3) OR age(9)
age <= 3 OR age = 9
NOT age([35-44]-[60 to 64])

Curly braces

Curly braces - { and } - can be used to control the order of operations. For example:

age >= [55-64] AND {occupation = [Retired] OR gender = [Female]}

Treatment of missing values and blanks

Make result missing if any inputs are missing Where this is checked, if the data for a case contains either NaNs or blanks then a NaN is the result. However, where it is not checked, missing values and blanks are treated as being no different to any other values. The default is for this setting to be checked, resulting in cases with NaN values when the input data has NaN values.

For example, where AGE is NaN and Income is 2, the following expression evaluates as NaN where Make result missing if any inputs are missing is checked, and 1 (true) where it is not checked:

AGE(1) OR INCOME(2)

Similarly, the expression:

NOT AGE(3)

is 0 (false) if Make result missing if any inputs are missing is checked when the value is NaN, and 1 (true) if not checked.

Treatment of different data types

Text Data

RespondentID >= "R0002312"

<, >, <= and >= compare text values in dictionary order. e.g. "mouse" is less than "rat" because "m" precedes "r" in the alphabet. Comparisons of text data are case insensitive.

Dates

date >= 2015-09-01
date < 2015-05-23 3:13 pm

Technical details

A Logic Variable assigns each case in a data file a value of 1, or 0 where Ignore missing values and blanks is selected and 1, 0 or NaN if not selected. When creating a category in a Banner, a value of 1 means that the case is represented by the category of the banner, a 0 means that it is not included but is compared against, while a NaN indicates it is excluded entirely from the analysis.

Operators are not sensitive to case. That is, you can write AND or and.

The following table lists the key aspects of the syntax, in order of operation (from highest to lowest priority).

NaN The missing value for numeric variables.
'text' or "text" Strings for comparison with text variables, surrounded by either single or double quotes (but beware "smart quotes" if pasting from Microsoft Word or an e-mail). Any visible Unicode character is acceptable. You can also include carriage return (\r), newline (\n), quotes (\" or \') and control characters (e.g.\x0F). <, >, <= and >= compare text values in dictionary order (lexicographically).
yyyy-mm-dd hh:mm:ss.mmm am|pm Dates and times can be entered according to this pattern. The time is optional, as are seconds and milliseconds within a time. If it ends with "am" or "pm" then hours will be interpreted accordingly; otherwise hours are taken as being from the 24-hour clock.
[value label] The label for a variable value. This must match the value label exactly. When you save your Logic variable Q will automatically convert this to a source value (e.g. #3) so that your expression will not be affected if the value label is changed. Tick Show value labels to see the value labels again.
#value Compare with the source value, which is not affected by recoding.
variable(value) This is the same as writing variable = value.
variable(value1-value2) This is the same as writing variable >= value1 AND variable <= value2.
variable(value1,value2,value3) This is the same as writing variable = value1 OR variable = value2 OR variable = value3.
=, <>, <, >, <=, >= Equal, not-equal, less-than, greater-than, less-than-or-equal, greater-than-or-equal. A comparison against a variable that contains a missing value will result in a missing value in the output variable, unless you are comparing against NaN. You can disable this behavior by ticking Ignore missing values.
NOT Reverses the test immediately to its right.
AND (or &) Matches (true) when the conditions on either side of it are both matched.
OR Matches (true) when either or both of the conditions on either side of it are matched.
{ ... } Curly braces group a sub-expression, forcing it to be calculated before the things outside the braces.

Alternatives to Logic Variables

Logic variables are designed to be relatively simple and efficient to use. There are a variety of other approaches to creating variables that are more general (i.e., can do more). See Creating New Variables for more information.

Other uses of Logic Variables

The principal use of Logic Variables is when creating complicated definitions as a part of Banners. However, they can be used for other purposes as well (e.g., to create filters or for general analysis).