Documentation

Calculated variables

When and Why to Use

Use these to save calculated values into your survey data so they show up in reporting, just like answers to real questions.

  • store_value() saves a single value (like a derived age or score) as a named calculated variable
  • store_values() saves a list of values as a calculated multi-select variable

Common use cases include:

  • Calculating age from birth year
  • Storing a derived score

For survey logic — branching, filtering, termination — keep using normal Python variables instead of retrieving stored reporting values. Both functions also return the stored value, so you can store a value and keep working with it in one step.

store_value(name, value, tags)

Stores a single scalar value as a calculated variable. Returns the stored value as a typed response value (StringResponse, IntResponse, or BoolResponse).

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. Returns the stored list as a ListResponse.

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

Example Code

Single value:

python
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)) # Keep the calculated value in a normal Python variable for survey logic... age = datetime.now().year - birth_year # ...and store it so it also appears in reporting. s.store_value("age", age) s.terminate_if(age < 18, "Sorry, this survey is for adults 18 and older.")

List of values:

python
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. # (The sandbox does not allow the set() builtin, so use a comprehension.) s.store_values("aware_and_considering", [brand for brand in known if brand in 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
  • For survey logic, keep using normal Python variables (as in the examples above) rather than retrieving stored reporting values
  • You can apply tags for each stored value to categorize them in your reporting schema