Documentation

Calculated variables

When and Why to Use

Use these to save and retrieve values for use in survey logic or reporting.

  • store_value() lets you assign a value (like a derived age or score) to a named variable
  • get_value() retrieves that value later in the survey for use in branching, filtering, or display

Common use cases include:

  • Calculating age from birth year
  • Storing a derived score
store_value(name, value, tags)

Stores a single scalar value as a calculated variable.

ParameterTypeRequiredDescription
namestryesName to assign to the stored value
valuestr, int, or boolyesValue to store, often calculated from a previous answer
tagss.tag()noUsed for grouping and metadata in reporting
store_values(name, values, tags)

Stores a list of values as a calculated multi-select variable — useful when the derived value naturally has more than one entry, such as a set of brands a respondent qualifies for, or a list of segments they belong to.

ParameterTypeRequiredDescription
namestryesName to assign to the stored values
valueslist of str, int, or boolyesList of values to store
tagss.tag()noUsed for grouping and metadata in reporting
get_value(name)
ParameterTypeRequiredDescription
namestryesName of the value to retrieve

Returns: The stored value (any type)

Example Code

Single value:

from datetime import datetime
from survey import Survey
s = Survey(**globals())
birth_year = s.numeric_question("What year were you born in?", min_max=(1920, 2020))
s.store_value("age", datetime.now().year - birth_year)
s.terminate_if( s.get_value("age") < 18, "Sorry, this survey is for adults 18 and older." )

List of values:

from survey import Survey
s = Survey(**globals())

known = s.multi_select_question(
    "Which brands have you heard of?",
    options=["Acme", "Globex", "Initech", "Umbrella"],
)
considering = s.multi_select_question(
    "Which would you consider buying?",
    options=known,
)

# Save the intersection as a derived multi-select variable.
s.store_values("aware_and_considering", list(set(known) & set(considering)),
               tags=s.tag(source="derived"))
Notes
  • store_value can be used multiple times to keep track of intermediate calculations
  • store_values produces a multi-select variable in reporting, just like a real multi-select question would
  • Values can be used in logic, shown in text, or passed to external APIs
  • You can apply tags for each stored value to categorize them in your reporting schema