Submit a ticket My Tickets
Welcome
Login

Logical Expressions Overview


Logical expressions

Logical expressions are used in filter="..." attributes of the <subq>, <section>, <r>, <other>, <info> , <say> and <do> tags, in the if attribute of the <goto> tag and in dependent range lists.
Logical expressions always evaluate to either "TRUE" or "FALSE".
In general they refer to previous responses/answers in the questionnaire.

E.g. if the questionnaire contains a question about "Gender" that looks like this:an expression like:
\Q1=1

will evaluate to "TRUE" for "Male" respondents and "FALSE" for "Female" respondents.

A logical expression always contains an "=" (equal) sign. On the left handside you refer to an address of a previously asked question (ques-item). An address always starts with "\" (backslash). In QML you refer to a question by its id (label) and not the actual question number. This means that all questions we want to refer to, must have an "id".
On the right hand side of "=" we have a value or value list. If the referred quest-item has an answer equal to this value or list of values, the expression is "TRUE".

The platform automatically keeps track of which ques-items have been asked and/or answered through the QR-bits.
As surveys are conducted over time, there might also be questions and answers that have not existed for the whole duration. We might therefore also have "not-existed" represented for ques-items and for answers. "Not-existed" is represented internally as the “illegal” combination of the "QR-bits" being not-asked but answered.

This means that the "QR-bits" have the following meaning:

QRMeaning
00Not asked, not answered
10Asked, not answered
11Asked and answered
01
No existence

Value/value lists

The value or value list can be expressed as follows:

ValueExplanation
5Single value
1;5;8Discrete list (1 or 5 or 8)
2:5Interval (from 2 through 5)
1;4;7:10Combination of discrete list and interval
In addition special codes:
¤Exists (all QR-bit combinations except "01")
?Asked (QR-bit combination "10" or "11")
*Answered (QR-bit combination "11")
-Not answered (QR-bit combination "10")
%The "%" sign is used when we refer to a subq-type N or M question from a subq-type with ques-items in each row (subq-types f/h/rn/rm/a). The significance here is that for each row we exchange the "%" with the corresponding code from the n/m subq in this row. This means that the expression is evaluated once for each row.
#The "#" sign means count the number of responses. E.g. \10=#3 is "TRUE" if question 10 has exactly 3 responses. \10=#2:6 is "TRUE" if question 10 has between 2 and 6 responses. \10=1:12#3:12 is "TRUE" if question 10 codes 1 through 12 has between 3 and 12 responses. Basic logical expressions can be combined with logical operators "|" (or), "&" (and) and "!" (not) and using parentheses.

E.g. a comlex example:

((\Q10=3)&(\Q11=2:5))|((\Q14=#2:5)&(\Q17=-))

This translates to

Q10=3 AND Q11= 2 or 3 or 5
OR
Q14 has between 2 and 5 answers AND Q17 is not answered

Address

An address to a ques-item consists of up to 3 parts:

  • Question number (always expressed through its ID)
  • Sub question letter (A-Z)
  • Row number (for sub questions with ques-items in each row rn/rm/f/h/a)

An address is always introduced through the "\" (backslash) character. Each component is separated with "." (punctuation mark). When referring within a question, one might skip the question number like this: \.a=2.

One might also combine questions, subqs or rows using ";" or ":" like:
\Q10.a:c=#1:3
Meaning 1 to 3 answers total, in question 10 both subq A, B and C.
When combining questions or subqs (using “;” or “:”the following rules apply:
The questions/subqs must be of the same type.
Only subqtype n/m
Only subqtype rn/rm
Only subqtype f/h
Same number of rows
Same number of codes (rn/rm)

The "%" (percent sign) might be used in place of a row number. In this case the expression is evaluated for each row, and "%" is replaced with the current row number.
The referred question must have at least the same number of rows.

Special addresses

The following special addresses might be used:

\script:NAME(P1,P2...)

This calls an external routine NAME. The parameters provided may be any valid address or a constant. Addresses will be replaced by actual answers, before the routine is called. In this way intricate logic may be implemented. Such scripts must be implemented by MI Pro.

System variables
These variables are described in the section: System Variables


Sample variables
Variables from the sample file, or passed as parameters in the link can also be used directly. These should be preceeded by “\@sms_” .
E.g. If an imported sample has a column labelled “gender”, “\@sms_gender” will refer to this value.

Backward referencing

All expressions in filters etc. have to be backward in the questionnaire. Reference within a block of rotated questions is not allowed (only to other questions guaranteed to come before).

Range expressions (valid answers)

The "range" attribute specifies absolute rules. This means that responses outside of valid ranges are not permitted at all. An example might be a range="15:99" to limit the responses for an age question to ages 15 through 99 years old. Any other value is not possible.

Syntax

Range expressions can be simple or quite complex. The basic part is what we call a "list-part". This consists of a response-value or a list of allowed responses.

List-partDescription
4only 4 is allowed
1:5responses 1, 2, 3, 4 and 5 are allowed
1;3;8responses 1, 3 and 8 are allowed
1:3;5;7:9responses 1, 2, 3, 5, 7, 8 and 9 are allowed
*we must have an answer/response required
-we allow no answer/response
\4any response from question 4 is allowed
!negates the list
!\4any response not given to question 4 is allowed
!3:63, 4, 5 and 6 are not allowed
rank NThe number of type F rows to be ranked
script:NAME(P1, P2..)Call an external routine (NAME). Supply any number of parameters which can be addresses of previous ques-items or constants. The external routine should return the allowable answers. MI Pro must implement such routines.
%Code at current row. Can be used in n/m subqs. Use together with a % for the row part of a conditional construct, meaning evaluate for each row and allow code if condition is "TRUE" Eg. % when \car.%=1:3 The referred question must have at least the same number of rows.
#N or #N:MNumber of answers for subq-types m and rm. Sum for subq-type f. N gives number of answers or exact sum. N:M gives an interval. For subq-types m/rm further limitations on which codes to count by prefixing E.g. 1:7#3 meaning 3 answers of codes 1 through 7. For subq-type m, number of asnwers should be checked before moving to next subq. For subq-type rm, check each row. For subq-type f, check the sum of all rows before moving to next subq.

These "range" lists can be combined with logical operators.

E.g.
(1:5)&(\Q3) must be 1, 2, 3, 4, or 5 but also given as response to question 3

(!\Q3.a)|(!\Q5.b) all responses not given to question 3.a or any response given to question 5.b
If a range expression has at least one logical operator then parentheses must be used to group the basic list expressions.
The range list can again be valid under certain circumstances. E.g. One list for “men” and another for “women”.

Conditional Constructs in the Range

There are two conditional constructs:
1. the "when" construct
2. the "try" construct

The "when" construct

This consists of an optional default list followed by one or more "when" lists followed by an optional "else" list (“when” constructs are typically used on single choice questions).

E.g.
(1:5)&(\Q3) must be 1, 2, 3, 4, or 5 but also given as response to question 3

(!\Q3.a)|(!\Q5.b) all responses not given to question 3.a or any response given to question 5.b
If a range expression has at least one logical operator then parentheses must be used to group the basic list expressions.
The range list can again be valid under certain circumstances. E.g. One list for “men” and another for “women”.

The "try" construct

This consists of an optional default list followed by one or more "try" lists (“try” constructs are typically used on multiple choice questions).

E.g.
(1;2) (3) try \3.A=1 (3;5;7) try \3.A=2:4

Default list (1;2) means 1 and 2 are always ok.
3 is ok if response to question 3.A is 1.
3, 5 or 7 is ok if response to question 3.A is 2, 3 or 4.

The "try" construct first inspects the default list if present. Then it will inspect one after the other of the "try" expressions. If the "try" expression evaluates to "TRUE" the list is inspected. If the response is found, everything is ok. If not check the next "try" expression. If a valid response is not found an error message informing the user of an invalid response, should be issued.

System variables

A list of the most relevant system variables is shown below:

NameDescription
sys_altidThe altid of the interview
sys_computerThe computer name (server name)
sys_dateThe date yyymmdd
sys_dayofweekDay of week 1:Monday
sys_elapsedtimeTime in seconds from start
sys_langCurrent language
sys_methodMethod (not relevant anymore, 2 = cawi)
sys_quesposCurrent question
sys_respguidGUID of interview
sys_randomThe sys_random variable
sys_rotseedThe seed used to generate the random variable
sys_timenowfCurrent time (servertime) hhmmss
sys_timenowhCurrent time (servertime) hh/mm
sys_timestartfStart time of interview hhmmss
sys_timestarthStart time of interview hh/mm
sys_weekWeek number 1-52

Scripts:

Full list here: https://support.walr.com/a/solutions/articles/101000500353


Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.