Calculated variables are entries calculated based on other entries in the dataset. As of today, you can only use numerical and date variables in the calculations, but the possibility of including categorical variables as part of condition-based calculations will be introduced shortly. You can create calculations for new categorical, numeric, and date variables with this function, which can be helpful in many settings.
Create a new variable as described under “Create variables”.
Choose "Calculated" under "Variable type".
Define if you want to calculate a categorical, numeric, or date variable under "Data type" (data types category multiple, text, file/image, and unique can not be calculated). NB: Your dataset should contain at least one numeric variable to create a calculated categorical or numerical variable and at least one date variable to create a calculated date variable.
To set up the rules for your calculation, switch to the "Calculation rules" tab (on top of the page).
The further steps will depend on what data type you want to calculate.
Note that the value for a calculated variable will only become visible in the entry form after saving it.
Create a calculated categorical variable.
Calculated categorical variables must always be fixed values. The values are constructed based on the set of conditions you define under "Calculation rules".
After creating a new calculated categorical variable, define your fixed values (category groups) in the field under the "Fixed values” toggle switch.
Switch to the "Calculation rules" tab (on top of the page)
Click "+ Add new rule"
Choose "Condition" (this is the only choice, as categorical values will always be based on a set of conditions)
Click "+ Add" to add a new condition. The conditions can be set based on numbers you enter (“Input”) or existing numerical variables in your dataset (“Variable”), optionally combined by operations (+, -, *, /).
Add a new rule to each fixed value defined.
Example:
Say you have a dataset containing the age of your study subjects (aged between 20 and 80 years) entered as a numeric value. You would like to divide your participants into age groups, a categorical variable, to compare between age groups in your analysis. You want to divide the subjects into three groups: Age 20-39, 40-59, and 60-80.
Create a new variable named, i.e., “Age group”, and set the variable type as “Calculated” and the data type as “Category”.
Define your three groups in the field under “Fixed values”.
Switch to the "Calculation rules" tab (on top of the page) and click “+ Add new rule”.
Choose “Condition”.
Click “Add” right to IF, and choose “Variable” – select the age variable from the dropdown menu.
Click the dropdown arrow beside “is greater than” and change it to “is greater than or equals to”.
Click “Add” right to “is greater than or equals to” and choose “Input” – enter “20” in the field.
Click the dropdown arrow beside “THEN VALUE IS” and change it to “AND”.
Click “Add” right to IF, and choose “Variable” – select the age variable from the dropdown menu.
Click the dropdown arrow beside “is greater than” and change it to “is less or equals to”.
Click “Add” right to “is less than or equals to” and choose “Input” – enter “39” in the field.
Select the correct group (Age 20-39) in the field to the right of “THEN VALUE IS”.
Click “+ Add new rule” and repeat the steps for your other groups with correct age intervals.
Click "Create"
Create a calculated numeric variable
Calculated numeric variables can be calculated based on a set of conditions (IF A AND B, THEN VALUE IS C), mathematical operations (+, -, *, /), or date calculations.
After creating a new calculated numeric variable, switch to the "Calculation rules" tab (on top of the page).
Click “+ Add new rule” – and choose “Condition”, “Operation”, or “Date calculation” (based on what you want to do).
The calculation can be based on numbers you enter (“Input”) as part of an operation, existing numerical variables in your dataset (“Variables”), combined by operations (+, -, *, /). Click “+Add” and choose how to build your calculation.
To illustrate how this works, look at this example, where we calculate BMI.
Example:
We have a dataset containing variables for height and weight, and we want to calculate body mass index (BMI). The formula is BMI = kg/m2.
Create a new variable named “BMI” and set the variable type as “Calculated” and the data type as “Number with decimals”.
Switch to the "Calculation rules"-tab and click “+ Add new rule”.
Select “Operation”.
Click the first “+ Add” button and select “Variable”.
Choose the variable in your dataset that contains the weight (in kg).
In the following line, select “/” to divide.
Click the “+Add” button and select “Operation”.
Click the “+Add” button again and select “Variable”.
Select the variable that contains the height (in m).
In the following line, select “*” to multiply.
Click the following “+Add” button and select “Variable”.
Select the variable that contains the height (in m) again.
Click “Create”.
Go back to your dataset, refresh the page, and BMI will appear as a new calculated variable.
HINT: If your dataset contains the height in cm and not m, select “Operation” and not “Variable” for step 8. The first part of the calculation is the same (weight kg/height cm * height cm) but for the final part of the calculation, add “/” by “Input” 10000.
Create calculated numeric variable based on dates
In your dataset, you can use date variables to calculate numeric variables, i.e., the time difference between two dates or a new date variable (e.g., time to next follow-up). Use “Date Calculation” in the “Composite variable rules” section for such calculation.
Example:
We have a dataset containing variables for date of birth and date of registration, and we want to calculate the person’s age at the time of registration.
Create a new variable named “Age” and set the variable type as “Calculated” and the data type as “Number without decimals”.
Switch to the "Calculation rules" tab (on top of the page) and click “+ Add new rule”.
Select “Date Calculation”
Click the first “+Add” button and select “Variable”.
Select the date variable containing the date of registration.
Repeat for the second “+Add” button and select the date variable containing the date of birth.
Select the desired unit for your calculated variable (for age, this would typically be "years")
Click “Create”