Computed Variables

Create calculated variables based on other answers.

Usage

  • Use COMPUTE: variable = expression to calculate a variable
  • Place at block-level for variables visible across the entire survey (all blocks share one namespace), or at page-level for variables scoped to a single page
  • Block-level names must be unique across the whole survey — defining the same name in two different blocks fails validation when the survey loads
  • A SHOW_IF: on any block, page, section, or question can reference a computed variable from any block, not just its own
  • Page-level computed variables can also reference any block-level variable, in addition to other page-level ones
  • Supports arithmetic expressions, conditions, and conditional string values
  • Evaluated dynamically as user answers questions

Arithmetic example

# **Monthly Expenses**
Q: Monthly rent
NUMBER
VARIABLE: rent

Q: Monthly food
NUMBER
VARIABLE: food

Q: Monthly transport
NUMBER
VARIABLE: transport

BLOCK: Summary
COMPUTE: total = rent + food + transport

# **Budget Summary**
Your total monthly expenses: **{total}**

Monthly Expenses

Monthly rent

Monthly food

Monthly transport

Comparisons can be summed the same way — each variable == value term counts as 1 or 0, so adding several together counts how many matched. Useful for tallying how many checkbox-style questions someone answered a certain way.

# Hazards

Q: Extreme heat
- Yes
- No
VARIABLE: heat

Q: Extreme cold
- Yes
- No
VARIABLE: cold

Q: Severe weather
- Yes
- No
VARIABLE: severe

# Result
COMPUTE: hazard_total = heat == Yes + cold == Yes + severe == Yes

You reported **{hazard_total}** hazard(s).

Hazards

Extreme heat

Extreme cold

Severe weather

Conditional values

Use IF <condition> THEN <value> ELSE <value> to assign a value based on a condition. The condition uses the same syntax as SHOW_IF. Values can be quoted strings, numbers, or references to other variables.

  • Unquoted values are first looked up as variable names, then treated as string literals if no matching variable exists
  • Computed variables can reference other computed variables — they are evaluated in dependency order
  • String results can be used in text placeholders and in SHOW_IF comparisons on pages, sections, or questions
# Satisfaction

Q: How satisfied are you? (1-10)
NUMBER
VARIABLE: score

# Result
COMPUTE: level = IF score >= 8 THEN "High" ELSE "Low"

Your satisfaction level: **{level}**

Satisfaction

How satisfied are you? (1-10)

Multiple categories

For more than two outcomes, use either ELSE IF chaining in a single expression, or multiple COMPUTE statements for the same variable.

With multiple statements, start with a default value and override it with conditions in increasing specificity. Each statement is evaluated in order — a condition that is false leaves the current value unchanged.

# Satisfaction

Q: How satisfied are you? (1-10)
NUMBER
VARIABLE: score

# Result
COMPUTE: level = "Low"
COMPUTE: level = IF score >= 5 THEN "Medium"
COMPUTE: level = IF score >= 8 THEN "High"

Your satisfaction level: **{level}**

Satisfaction

How satisfied are you? (1-10)

Alternatively, write it as a single chained expression:

# Satisfaction

Q: How satisfied are you? (1-10)
NUMBER
VARIABLE: score

# Result
COMPUTE: level = IF score >= 8 THEN "High" ELSE IF score >= 5 THEN "Medium" ELSE "Low"

Your satisfaction level: **{level}**

Satisfaction

How satisfied are you? (1-10)