What is Build My Calc? This is a tutorial to help you build your REDCap calculation one step at a time. The framework of the calculation is provided, and you fill in the blanks. How many blanks, and what kind of information is needed will depend on the calculation you want to build. As you specify the different elements, you can see them being added to the calculation in real time at the bottom of the survey page.
To get a PDF copy of your calculation, click 'Submit' once you reach the end, and you will see an option to download a PDF of the completed tutorial and calculation.
You can also click the "Save and Return Later" button, where you can enter your email address and a private link to this survey will be sent to you. You can access that link and your calculation anytime (no return code needed).
NOTE: If you are creating calculations using fields from repeating instruments or repeating events, you may need to adjust your calculation accordingly using Smart Variables.
You may need to adjust the calculation further once it is in your project. Each project is different, and while the formatting for calculations is fairly standard, you may need to make further adjustments.
Click on the buttons below to learn more:
Build My Calc Information
This tutorial has five different functions to create calculations with. You can combine certain functions if you like. There are more functions available in REDCap, but this tutorial only includes the most common functions and combinations of those functions.
See the fields below for more information on each these functions, and to learn how they work.
If you are building a complex calculation in REDCap, you may need more help than this tutorial can provide and you should contact redcap@bcchr.ca for assistance. How REDCap Calculations work:
REDCap can automatically pull data from fields across your project, perform a function, and then display an output. Calculations must be formatted in a particular way, which this tutorial will help you with.
Calculations in REDCap:
work in real time
can pull data from different instruments and events
only compute numbers and dates
work in data entry forms and in surveys
can only output numbers
Click on any of the calculations below to learn more about how they work:
Date Difference Function
What is it?
Use this function to calculate the difference between two date or datetime fields.
When would you use it?
Some common examples include: To calculate a participant's age, to know the time lapse between two clinic visits, or to send surveys after a certain amount of time. Basically anytime you want to display a time difference.
How does it work?
Specify two date variables and an output format to get the difference between them. At least one or both date or datetime fields must exist as variables in your project and be validated as date or datetime fields. If you don't have two date fields, you can use 'today' or a fixed date as the second date field.
Can I use today's date as a variable?
Yes. Using 'today' is a great way to calculate the difference between a date field and today's date, but 'today' can cause data quality issues , as any time you open and save a form using 'today' in a calculation, that calculation will automatically take today's date and update your calculation. Only use 'today' if it's necessary.
What does a date difference calculation look like?
If you put your date difference equation in a calculated field, the number output will appear in red and in the units you specify:
Round Function
What is it?
Use this function to round a number to a certain decimal point.
When would you use it?
Anytime you want to round a number. You can add this function to any calculation, including a date difference calc, a mean function, or BMI calculation.
How does it work?
All you need to do is specify how many decimal points you want your output to round to at the end of calculation.
Can this function be combined with other functions?
Yes. It is common to use the round function with a BMI calculation, an average, or any number output.
What does a round function look like?
The number will round to whatever decimal point you specify:
If/Then Statement Function
What is it?
Use this function to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
When would you use it?
Some common examples include: to create ranges for raw values- so I can have a calculation take a raw value, say from 0-100 and convert that into a number on a scale, say from 1-10. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: An IF statement can compare data that I have in a variable against a condition. My variable, let's call it [score], could be a radio button, a short text box validated as a number field, or another calculation. In this case, my condition is '10'.
So I want to write if([score]='10', 1, 0). This statement says that if my "score" variable equals 10, then display 1, if "score" does not equal 10, display a 0. So I won't see the score- what I will see are my defined outputs.
How does it work?
An IF statement is a comparison. Here you are defining what data the statement will pull to use in the comparison. In REDCap, you want the comparison to be done against a variable that exists in your project. This variable must be a radio button or text box that is validated as a number field. Calculations cannot read or compute text fields. The calculation input and output will always be a number.
Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to help assess participant eligibility and consent, you could do the following:
if([consent]="yes",1,if([eligible]="yes",2,0))
This statement says that IF the consent is marked yes, then show '1', IF not, then calculate IF eligibility is marked as yes, show '2', and if not, then show '0'.
What does an if/then statement look like?
Once your if statement is added as a calculated field, you will see the numerical output in red:
Sum Function
What is it?
Use this function to add up any variables in your project (except text fields and check boxes).
When would you use it?
Some common examples include: adding up scores on a scale, adding up answers to a quiz, adding up data across events.
How does it work?
The fields you want to include in the sum calculation must exist as variables in your project. You can sum up radio buttons, drop down menus, text fields validated as numbers, and other calculated fields. The calculation will add up the raw values assigned to the answer options.
Can I use reverse coding in a sum calc?
Yes, make sure your answer options are coded according to the meaning of the question and that the coding is consistent. This way when you go to add them up, the score is reflective of the data meaning.
Can I sum up checkboxes?
No. Do not include checkboxes in your sum equation, as checkboxes function differently and the raw value will always be either '1' (checked) or '0' (unchecked) as opposed to other fields, where you can specify the raw number value.
Why can't I just add up variables like this: [variable1] + [variable2]?
This works too. However, if one or more of the variables is blank, then the above calculation won't work (it will be blank with no output). The sum function will take whatever values are filled out, ignore any blank values, and give you an output.
What does a sum function look like?
Once your sum calc is added as a calculated field, you will see the numerical output in red:
Mean Function
What is it?
Use this function to calculate the mean for any variables in your project (except text fields and check boxes).
When would you use it?
Anytime you want the mean score from multiple numerical fields.
How does it work?
Returns the mean (i.e. average) value of a set of values provided in the format mean([num1],[num2],[num3],...). The fields you want to include in the mean calculation must exist as variables in your project. You can use radio buttons, drop down lists, text fields validated as numbers, and other calculated fields. The calculation will average the raw values assigned to the answer options.
Can I average checkboxes?
No. Do not include checkboxes in your mean equation, as checkboxes function differently and the raw value will always be either '1' (checked) or '0' (unchecked) as opposed to other fields, where you can specify the raw number value.
What does a mean function look like?
Once your mean calc is added as a calculated field, you will see the numerical output in red: To start building your calculation, click the 'Next Page' button below
Let's set up your calculation:
What kind of project do you have?
* must provide value
Classic
Longitudinal
Some functions can be used in combination with each other, while others cannot. The three options below cannot be combined with each other in this tutorial . Select one of the options below, or select a function from the next field:
Date Difference
Sum
Mean
The functions below can be used as stand alone calculations, or they can be used in combination with each other or with one of the functions above.
Click the "if" button to add an If statement to your calculation:
* must provide value
if(
Click the button below to round your equation:
* must provide value
round(
roundup(
rounddown(
Click the "datediff" button below to add this function to your calculation:
* must provide value
datediff(
Click the "sum" button to add variables together:
* must provide value
sum(
Click the "mean" button below to add this function to your calculation:
* must provide value
mean(
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Date Difference Function
What is it?
Use this function to calculate the difference between two date or datetime fields.
When would you use it?
Some common examples include: To calculate a participant's age, to know the time lapse between two clinic visits, or to send surveys after a certain amount of time. Basically anytime you want to display a time difference.
How does it work?
Specify two date variables and an output format to get the difference between them. At least one or both date or datetime fields must exist as variables in your project and be validated as date or datetime fields. If you don't have two date fields, you can use 'today' or a fixed date as the second date field.
Can I use today's date as a variable?
Yes. Using 'today' is a great way to calculate the difference between a date field and today's date, but 'today' can cause data quality issues , as any time you open and save a form using 'today' in a calculation, that calculation will automatically take today's date and update your calculation. Only use 'today' if it's necessary.
What does a date difference calculation look like?
If you put your date difference equation in a calculated field, the number output will appear in red and in the units you specify: Enter the unique event name , or the location, of the field you would like to pull from:
Put the event name (not the label) of the date field in your project you would like to use.
Make sure it is spelled correctly and that you put it in square brackets [ ] or the calculation will not work. Your unique event name is on the "Define My Events" page in your project.
You need to specify an event name, for each variable in you calculation, when your project has a longitudinal setup. This is because your variable can now exist in more than one event, and you need to tell the system which event you want to pull the data from. You don't have to do this IF you're pulling data from within a single event.
* must provide value
Enter the first date field variable name that you want to include in the function, a 'today' variable, or a fixed date:
Put the variable name (not the label) of the date field you would like to use. This variable must already exist in your project. Make sure your variable name is spelled correctly and that you put it in square brackets [ ] or the calculation will not work. If you are using today's date or a fixed date, instead of a variable in your project, make sure to enter it like this: 'today'. You must include the single equations.
Make sure to include a comma after your value. The comma is there to separate your values.
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Date Difference Function
What is it?
Use this function to calculate the difference between two date or datetime fields.
When would you use it?
Some common examples include: To calculate a participant's age, to know the time lapse between two clinic visits, or to send surveys after a certain amount of time. Basically anytime you want to display a time difference.
How does it work?
Specify two date variables and an output format to get the difference between them. At least one or both date or datetime fields must exist as variables in your project and be validated as date or datetime fields. If you don't have two date fields, you can use 'today' or a fixed date as the second date field.
Can I use today's date as a variable?
Yes. Using 'today' is a great way to calculate the difference between a date field and today's date, but 'today' can cause data quality issues , as any time you open and save a form using 'today' in a calculation, that calculation will automatically take today's date and update your calculation. Only use 'today' if it's necessary.
What does a date difference calculation look like?
If you put your date difference equation in a calculated field, the number output will appear in red and in the units you specify: Enter the second unique event name , or the location, of the field you would like to pull from:
Put the event name (not the label) of the date field in your project you would like to use. Make sure it is spelled correctly and that you put it in square brackets [ ] or the calculation will not work. Your unique event name is on the "Define My Events" page in your project.
You need to specify an event name, for each variable in you calculation, when your project has a longitudinal setup. This is because your variable can now exist in more than one event, and you need to tell the system which event you want to pull the data from. You don't have to do this IF you're pulling data from within a single event.
* must provide value
Enter a second date field variable name that exists in your project, a 'today' variable, or a fixed date:
Put the variable name (not the label) of the date field you would like to use. This variable must already exist in your project. Make sure your variable name is spelled correctly and that you put it in square brackets [ ] or the calculation will not work. If you are using today's date or a fixed date, instead of a variable in your project, make sure to enter it like this: 'today'. You must include the single equations.
Make sure to include a comma after your value. The comma is there to separate your values.
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
If/Then Statement Function
What is it?
Use this function to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
When would you use it?
Some common examples include: to create ranges for raw values- so I can have a calculation take a raw value, say from 0-100 and convert that into a number on a scale, say from 1-10. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: An IF statement can compare data that I have in a variable against a condition. My variable, let's call it [score], could be a radio button, a short text box validated as a number field, or another calculation. In this case, my condition is '10'.
So I want to write if([score]='10', 1, 0). This statement says that if my "score" variable equals 10, then display 1, if "score" does not equal 10, display a 0. So I won't see the score- what I will see are my defined outputs.
How does it work?
An IF statement is a comparison. Here you are defining what data the statement will pull to use in the comparison. In REDCap, you want the comparison to be done against a variable that exists in your project. This variable must be a radio button or text box that is validated as a number field. Calculations cannot read or compute text fields. The calculation input and output will always be a number.
Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to help assess participant eligibility and consent, you could do the following:
if([consent]="yes",1,if([eligible]="yes",2,0))
This statement says that IF the consent is marked yes, then show '1', IF not, then calculate IF eligibility is marked as yes, show '2', and if not, then show '0'.
What does an if/then statement look like?
Once your if statement is added as a calculated field, you will see the numerical output in red:
Sum Function
What is it?
Use this function to add up any variables in your project (except text fields and check boxes).
When would you use it?
Some common examples include: adding up scores on a scale, adding up answers to a quiz, adding up data across events.
How does it work?
The fields you want to include in the sum calculation must exist as variables in your project. You can sum up radio buttons, drop down menus, text fields validated as numbers, and other calculated fields. The calculation will add up the raw values assigned to the answer options.
Can I use reverse coding in a sum calc?
Yes, make sure your answer options are coded according to the meaning of the question and that the coding is consistent. This way when you go to add them up, the score is reflective of the data meaning.
Can I sum up checkboxes?
No. Do not include checkboxes in your sum equation, as checkboxes function differently and the raw value will always be either '1' (checked) or '0' (unchecked) as opposed to other fields, where you can specify the raw number value.
Why can't I just add up variables like this: [variable1] + [variable2]?
This works too. However, if one or more of the variables is blank, then the above calculation won't work (it will be blank with no output). The sum function will take whatever values are filled out, ignore any blank values, and give you an output.
What does a sum function look like?
Once your sum calc is added as a calculated field, you will see the numerical output in red:
Mean Function
What is it?
Use this function to calculate the mean for any variables in your project (except text fields and check boxes).
When would you use it?
Anytime you want the mean score from multiple numerical fields.
How does it work?
Returns the mean (i.e. average) value of a set of values provided in the format mean([num1],[num2],[num3],...). The fields you want to include in the mean calculation must exist as variables in your project. You can use radio buttons, drop down lists, text fields validated as numbers, and other calculated fields. The calculation will average the raw values assigned to the answer options.
Can I average checkboxes?
No. Do not include checkboxes in your mean equation, as checkboxes function differently and the raw value will always be either '1' (checked) or '0' (unchecked) as opposed to other fields, where you can specify the raw number value.
What does a mean function look like?
Once your mean calc is added as a calculated field, you will see the numerical output in red:
Round Function
What is it?
Use this function to round a number to a certain decimal point.
When would you use it?
Anytime you want to round a number. You can add this function to any calculation, including a date difference calc, a mean function, or BMI calculation.
How does it work?
All you need to do is specify how many decimal points you want your output to round to at the end of calculation.
Can this function be combined with other functions?
Yes. It is common to use the round function with a BMI calculation, an average, or any number output.
What does a round function look like?
The number will round to whatever decimal point you specify: Enter the unique event name , or the location, of the field you would like to pull from:
Put the event name (not the label) of the date field in your project you would like to use. Make sure it is spelled correctly and that you put it in square brackets [ ] or the calculation will not work. Your unique event name is on the "Define My Events" page in your project.
You need to specify an event name, for each variable in you calculation, when your project has a longitudinal setup. This is because your variable can now exist in more than one event, and you need to tell the system which event you want to pull the data from. You don't have to do this IF you're pulling data from within a single event.
* must provide value
Enter the variable name of a field you want add to the calculation:
Put the variable name (not the label) of the multiple choice or number field you would like to included in the calculation. This variable must already exist in your project. You can also write a second equation in this box, if you want to add a second layer to your calculation. Make sure that the variables in your second calculation exist your project.
Make sure variable names are spelled correctly and that you put them in square brackets [ ] or the calculation will not work.
This field can only be a radio button, a drop down list, or a text box validated as a number field.
* must provide value
Make sure to include a comma after your value, like so: [var1],
The comma is there to separate your values.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______
______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Sum Function
What is it?
Use this function to add up any variables in your project (except text fields and check boxes).
When would you use it?
Some common examples include: adding up scores on a scale, adding up answers to a quiz, adding up data across events.
How does it work?
The fields you want to include in the sum calculation must exist as variables in your project. You can sum up radio buttons, drop down menus, text fields validated as numbers, and other calculated fields. The calculation will add up the raw values assigned to the answer options.
Can I use reverse coding in a sum calc?
Yes, make sure your answer options are coded according to the meaning of the question and that the coding is consistent. This way when you go to add them up, the score is reflective of the data meaning.
Can I sum up checkboxes?
No. Do not include checkboxes in your sum equation, as checkboxes function differently and the raw value will always be either '1' (checked) or '0' (unchecked) as opposed to other fields, where you can specify the raw number value.
Why can't I just add up variables like this: [variable1] + [variable2]?
This works too. However, if one or more of the variables is blank, then the above calculation won't work (it will be blank with no output). The sum function will take whatever values are filled out, ignore any blank values, and give you an output.
What does a sum function look like?
Once your sum calc is added as a calculated field, you will see the numerical output in red:
Mean Function
What is it?
Use this function to calculate the mean for any variables in your project (except text fields and check boxes).
When would you use it?
Anytime you want the mean score from multiple numerical fields.
How does it work?
Returns the mean (i.e. average) value of a set of values provided in the format mean([num1],[num2],[num3],...). The fields you want to include in the mean calculation must exist as variables in your project. You can use radio buttons, drop down lists, text fields validated as numbers, and other calculated fields. The calculation will average the raw values assigned to the answer options.
Can I average checkboxes?
No. Do not include checkboxes in your mean equation, as checkboxes function differently and the raw value will always be either '1' (checked) or '0' (unchecked) as opposed to other fields, where you can specify the raw number value.
What does a mean function look like?
Once your mean calc is added as a calculated field, you will see the numerical output in red: Enter the unique event name , or the location, of the field you would like to pull from:
Put the event name (not the label) of the date field in your project you would like to use. Make sure it is spelled correctly and that you put it in square brackets [ ] or the calculation will not work. Your unique event name is on the "Define My Events" page in your project.
You need to specify an event name, for each variable in you calculation, when your project has a longitudinal setup. This is because your variable can now exist in more than one event, and you need to tell the system which event you want to pull the data from. You don't have to do this IF you're pulling data from within a single event.
* must provide value
Enter the variable name of a field you want add to the calculation:
Put the variable name (not the label) of the multiple choice or number field in your project you would like to included in the calculation. You can also write a second equation in this box, if you want to add a second layer to your calculation. Make sure that the variables in your second calculation exist your project.
Make sure it is spelled correctly and that you put it in square brackets [ ] or the calculation will not work.
This field can only be a radio button, a drop down list, or a text box validated as a number field.
* must provide value
Make sure to include a round bracket after your value, like so: [var2])
The bracket is there to close your function.
Add more variables
It looks like you've chosen to build a "sum" or "mean" function. Note that you can include as many variables as you want, but that this tutorial only gives you two slots for variable names.
If you want to sum up or take the mean for more than two variables, you can add more than one variable name into the text boxes (make sure your variable names are in square brackets and that you separate each variable name with a comma). You can also make this adjustment in REDCap.
Be sure to include the event name before each variable if this is for a longitudinal project.
Example: sum([var1],[var2],[var3],[var4],[var5]).
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
______ ______ ______
Date Difference Function
What is it?
Use this function to calculate the difference between two date or datetime fields.
When would you use it?
Some common examples include: To calculate a participant's age, to know the time lapse between two clinic visits, or to send surveys after a certain amount of time. Basically anytime you want to display a time difference.
How does it work?
Specify two date variables and an output format to get the difference between them. At least one or both date or datetime fields must exist as variables in your project and be validated as date or datetime fields. If you don't have two date fields, you can use 'today' or a fixed date as the second date field.
Can I use today's date as a variable?
Yes. Using 'today' is a great way to calculate the difference between a date field and today's date, but 'today' can cause data quality issues , as any time you open and save a form using 'today' in a calculation, that calculation will automatically take today's date and update your calculation. Only use 'today' if it's necessary.
What does a date difference calculation look like?
If you put your date difference equation in a calculated field, the number output will appear in red and in the units you specify: Select the unit would you like the date output to be in:
When your date difference calculation runs, it will appear in the time units you specify here.
Options for output units:
"s" - seconds
"m" - minutes
"h" - hours
"d" - days
"M" - months
"y" - years
Example: Let's say the difference between two dates is 1 year. If you specify that you want your output in 'y' you will see a '1'. If you specify your output in 'M' you will see '12'. If you specify your output in 'd' you will see '365'.
* must provide value
's',
'm',
'h',
'd',
'M',
'y',
Your Calculation in Progress
______ ______ ______ ______ ______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Date Difference Function
What is it?
Use this function to calculate the difference between two date or datetime fields.
When would you use it?
Some common examples include: To calculate a participant's age, to know the time lapse between two clinic visits, or to send surveys after a certain amount of time. Basically anytime you want to display a time difference.
How does it work?
Specify two date variables and an output format to get the difference between them. At least one or both date or datetime fields must exist as variables in your project and be validated as date or datetime fields. If you don't have two date fields, you can use 'today' or a fixed date as the second date field.
Can I use today's date as a variable?
Yes. Using 'today' is a great way to calculate the difference between a date field and today's date, but 'today' can cause data quality issues , as any time you open and save a form using 'today' in a calculation, that calculation will automatically take today's date and update your calculation. Only use 'today' if it's necessary.
What does a date difference calculation look like?
If you put your date difference equation in a calculated field, the number output will appear in red and in the units you specify: Select the date format:
You need to specify the "dateformat" parameter. It must be "ymd", "mdy", or "dmy", which refers to the format of both date/time fields in your project. Both date or date/time fields must be validated in the same format for the calculation to work.
Your date/datetime fields must be validated in either "ymd", "mdy" or "dmy" for your calculation to work. Double check that these fields are validated in your project, and indicate which output format you would like to see below.
If this element is not defined, the format will default to "ymd".
'dmy'
'mdy'
'ymd'
Your Calculation in Progress
______ ______ ______ ______ ______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Date Difference Function
What is it?
Use this function to calculate the difference between two date or datetime fields.
When would you use it?
Some common examples include: To calculate a participant's age, to know the time lapse between two clinic visits, or to send surveys after a certain amount of time. Basically anytime you want to display a time difference.
How does it work?
Specify two date variables and an output format to get the difference between them. At least one or both date or datetime fields must exist as variables in your project and be validated as date or datetime fields. If you don't have two date fields, you can use 'today' or a fixed date as the second date field.
Can I use today's date as a variable?
Yes. Using 'today' is a great way to calculate the difference between a date field and today's date, but 'today' can cause data quality issues , as any time you open and save a form using 'today' in a calculation, that calculation will automatically take today's date and update your calculation. Only use 'today' if it's necessary.
What does a date difference calculation look like?
If you put your date difference equation in a calculated field, the number output will appear in red and in the units you specify: Select which return value would you like:
The parameter "returnSignedValue" denotes whether you want the numerical output to be either signed (have a minus in front if negative) or unsigned (absolute value).
For example, if [date1] is larger than [date2], then the result will be negative, if returnSignedValue is set to TRUE. If returnSignedValue is not set or is set to FALSE, then the result will ALWAYS be a positive number.
If returnSignedValue is set to FALSE or not set, then the order of the dates in the equation does not matter because the resulting value will always be positive (although the + sign is not displayed but implied).
The default value is FALSE, which returns the absolute value of the difference, and will always give you a positive number. This is the recommended option.
,true)
,false)
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______
______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Round Function
What is it?
Use this function to round a number to a certain decimal point.
When would you use it?
Anytime you want to round a number. You can add this function to any calculation, including a date difference calc, a mean function, or BMI calculation.
How does it work?
All you need to do is specify how many decimal points you want your output to round to at the end of calculation.
Can this function be combined with other functions?
Yes. It is common to use the round function with a BMI calculation, an average, or any number output.
What does a round function look like?
The number will round to whatever decimal point you specify: What decimal place would you like to round to?
This value must be a number. Be sure to include a comma in front of your number, and a round bracket at the end.
The comma is to separate your rounding requirement from the rest of equation. The round bracket is to close your round function.
Example: to round to two decimal places write: ,2)
Note: if you would like to round to the nearest whole number enter: ,0) or just type in the round bracket: )
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______
______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
If/Then Statement Function
What is it?
Use this function to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
When would you use it?
Some common examples include: to create ranges for raw values- so I can have a calculation take a raw value, say from 0-100 and convert that into a number on a scale, say from 1-10. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: An IF statement can compare data that I have in a variable against a condition. My variable, let's call it [score], could be a radio button, a short text box validated as a number field, or another calculation. In this case, my condition is '10'.
So I want to write if([score]='10', 1, 0). This statement says that if my "score" variable equals 10, then display 1, if "score" does not equal 10, display a 0. So I won't see the score- what I will see are my defined outputs.
How does it work?
An IF statement is a comparison. Here you are defining what data the statement will pull to use in the comparison. In REDCap, you want the comparison to be done against a variable that exists in your project. This variable must be a radio button or text box that is validated as a number field. Calculations cannot read or compute text fields. Input and the output will always be a number.
Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to help assess participant eligibility and consent, you could do the following:
if([consent]="yes",1,if([eligible]="yes",2,0))
This statement says that IF the consent is marked yes, then show '1', IF not, then calculate IF eligibility is marked as yes, show '2', and if not, then show '0'.
What does an if/then statement look like?
Once your if statement is added as a calculated field, you will see the numerical output in red: Choose an operator:
Possible operators you can type in:
Equals: =
Greater than: >
Less than: < (make sure to add space after this operator in the field below )
Greater or equals to: >=
Less or equals to: <=
Does not equal: <>
For example: Here the operator is an equals sign, because the IF statement wants to know if the [score] variable is equal to blank.
if([score]= '10',1,0).
Below, type in the operator you want to use:
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______
______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
If/Then Statement Function
What is it?
Use this function to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
When would you use it?
Some common examples include: to create ranges for raw values- so I can have a calculation take a raw value, say from 0-100 and convert that into a number on a scale, say from 1-10. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: An IF statement can compare data that I have in a variable against a condition. My variable, let's call it [score], could be a radio button, a short text box validated as a number field, or another calculation. In this case, my condition is '10'.
So I want to write if([score]='10', 1, 0). This statement says that if my "score" variable equals 10, then display 1, if "score" does not equal 10, display a 0. So I won't see the score- what I will see are my defined outputs.
How does it work?
An IF statement is a comparison. Here you are defining what data the statement will pull to use in the comparison. In REDCap, you want the comparison to be done against a variable that exists in your project. This variable must be a radio button or text box that is validated as a number field. Calculations cannot read or compute text fields. Input and the output will always be a number.
Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to help assess participant eligibility and consent, you could do the following:
if([consent]="yes",1,if([eligible]="yes",2,0))
This statement says that IF the consent is marked yes, then show '1', IF not, then calculate IF eligibility is marked as yes, show '2', and if not, then show '0'.
What does an if/then statement look like?
Once your if statement is added as a calculated field, you will see the numerical output in red: Specify the raw variable, number or date to use as a condition:
The value can be a number, a piped variable (that is a number), or a blank value. You can code a blank value as " " or as "NaN".
An IF statement is a comparison. Here you are defining what the statement will compare your data to.
For example: In this IF statement, the variable name is defining what data to pull, and now you have to define what value to compare that data against. In this example, the value the variable is being compared to is "10":
if([score]=10, 1, 0).
This statement says that if the data in my [score] variable equals 10, then display 1, if the data does not equal 10, display a 0.
Below, enter the value you want to use as the comparison.
Make sure to include a comma after your value. The comma is there to separate your output values.
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
If/Then Statement Function
What is it?
Use this function to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
When would you use it?
Some common examples include: to create ranges for raw values- so I can have a calculation take a raw value, say from 0-100 and convert that into a number on a scale, say from 1-10. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: An IF statement can compare data that I have in a variable against a condition. My variable, let's call it [score], could be a radio button, a short text box validated as a number field, or another calculation. In this case, my condition is '10'.
So I want to write if([score]='10', 1, 0). This statement says that if my "score" variable equals 10, then display 1, if "score" does not equal 10, display a 0. So I won't see the score- what I will see are my defined outputs.
How does it work?
An IF statement is a comparison. Here you are defining what data the statement will pull to use in the comparison. In REDCap, you want the comparison to be done against a variable that exists in your project. This variable must be a radio button or text box that is validated as a number field. Calculations cannot read or compute text fields. Input and the output will always be a number.
Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to help assess participant eligibility and consent, you could do the following:
if([consent]="yes",1,if([eligible]="yes",2,0))
This statement says that IF the consent is marked yes, then show '1', IF not, then calculate IF eligibility is marked as yes, show '2', and if not, then show '0'.
What does an if/then statement look like?
Once your if statement is added as a calculated field, you will see the numerical output in red: Please specify the numerical return you would like see if your "if" statement is true :
The value can be a number, a piped variable (that is validation as a number), or a blank value. You can code a blank value as " " or as "NaN".
The value you enter should answer this question: what number do I want to see if my condition is true. Or in REDCap language: what number do I want to see if a record has data that matches my condition?
For example: Here the the number 1 is specified to appear the [score] variable equals '10':
if([score]=10,1, 0).
Below, enter the value or variable, you want to see if your IF statement is true.
Make sure to include a comma after your value.
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______
______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
If/Then Statement Function
What is it?
Use this function to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
When would you use it?
Some common examples include: to create ranges for raw values- so I can have a calculation take a raw value, say from 0-100 and convert that into a number on a scale, say from 1-10. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: An IF statement can compare data that I have in a variable against a condition. My variable, let's call it [score], could be a radio button, a short text box validated as a number field, or another calculation. In this case, my condition is '10'.
So I want to write if([score]='10', 1, 0). This statement says that if my "score" variable equals 10, then display 1, if "score" does not equal 10, display a 0. So I won't see the score- what I will see are my defined outputs.
How does it work?
An IF statement is a comparison. Here you are defining what data the statement will pull to use in the comparison. In REDCap, you want the comparison to be done against a variable that exists in your project. This variable must be a radio button or text box that is validated as a number field. Calculations cannot read or compute text fields. Input and the output will always be a number.
Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to help assess participant eligibility and consent, you could do the following:
if([consent]="yes",1,if([eligible]="yes",2,0))
This statement says that IF the consent is marked yes, then show '1', IF not, then calculate IF eligibility is marked as yes, show '2', and if not, then show '0'.
What does an if/then statement look like?
Once your if statement is added as a calculated field, you will see the numerical output in red: Please specify the numerical return you would like see if your "if" statement is false :
The value can be a number, a piped variable (is validated as a number), or a blank value. You can code a blank value as " " or as "NaN".
The value you enter should answer this question: what number do I want to see if my condition is false. Or in REDCap language: what number do I want to see if a record has data that DOES NOT match my condition?
For example: Here the the number 0 is specified to appear the [score] variable does not equal '10':
if([score]=10,1,0) .
Enter a value or a variable you want to see if you're IF statement is false.
Make sure to include a round bracket after your value. The round bracket is there to close your if statement.
* must provide value
Your Calculation in Progress
______ ______ ______ ______
______ ______
______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all.
Your Calculation in Progress
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
Don't worry if there are blank underscores in your calculation in progress. This just means that you either haven't filled that field in yet, or you won't be using that field at all. You can copy and paste the calculation below into a calculated field in your REDCap project. Make sure the calculation is valid and that you test the calculation to make sure it works.
Your Completed Calculation
Your Completed Calculation
If you want to go back and make a correction to your calculation, you can DELETE the calculation in the notes box above, and then click the "previous page" button to go back to make edits.
You can also edit the calculation in the notes box or in REDCap.
Your calculation will not work if there are missing commas or brackets, if any variable names or events names are spelled incorrectly. Double check everything is correct!
Click 'Submit' to generate a PDF copy of the completed tutorial!
You can also click the "Save and Return Later" button, where you can enter your email address and a private link to this survey will be sent to. You can access that link and your calculation anytime (no return code needed).
How helpful was this tutorial?
Common calculation questions:
Can calculated fields be referenced or nested in other calculated fields?
Yes. Calculations can reference other calculations. Be sure to thoroughly test to ensure correct expected values.
For example, you could have a BMI calculation within an if/then statement:
if([bmi] > 20, 44, 11)
This calculation is referencing the BMI calculation and say that if a record has a BMI over 20, then display the number 44, otherwise display the number 11.
What mathematical operations are available for calc fields?
+ Add
- Subtract
* Multiply
/ Divide
For example, you can divide a number to get an average.
Null or blank values can be referred to as "" or "NaN". Be careful to include the quotes around NaN. You would use NaN to indicate you want no value to be displayed
What are some common examples of calculated fields?
Body Mass Index is one of the most common calculations used in REDCap.
Below is an example equation for the BMI field above in which the fields named 'height' and 'weight' are used as variables.
[weight]*10000/([height]*[height]) = units in kilograms and centimeters
([weight]/([height]*[height]))*703 = units in pounds and inches
If I import data will new and modified data re-run and update the calculate fields?
Yes. When performing a data import (via Data Import Tool), REDCap will perform the calculations for any calculated fields that are triggered by the values being imported.
For example, if you have a BMI field whose calculation is based off of a height field and a weight field, then if you perform a data import of height and weight values, it will automatically calculate the BMI for each record that is imported and also save those calculations and log them on the Logging page.
If I need to modify a calculated field, how can I update all the records previously entered?
Data Quality rule (rule H) will find and fix all incorrect values for calculated fields in a project.
If any calc fields have ended up with incorrect values (whether due to field changes in the project or due to previous data imports), users can now run rule H not only to find any incorrect calculated values, but it will additionally display a button that, when clicked, will auto-fix ALL of the calculation errors in a project at once.
Can I create a calculation that returns text as a result (Ex: "True" or "False")?
No. Calculations can only result in numbers. You could indicate "1" = True and "0" = False.
Why is my advanced calculation not working?
The equation may not be formatted correctly. You may try troubleshooting the equation by simplifying the equation first and then add functionality in steps as you test.
For example, if you have a round function and a sum function in one equation, create a new calculation with just one of those functions. You can then test your simpler equation, and add layers to the equation to isolate the problem.
Another way to troubleshoot is to click "view equation". All the variables you are referencing will be listed. If they are not, you will need to check and confirm the variable and event names.
I created a calculated field after I entered data on a form, and it doesn't look like it's working. Why not?
If you add a calculated field where data already exist in a form, data must be re-saved for the calculation to be performed.
This is true for updating calculations as well. You may see the calculated value change or update on a data entry form or survey (as you enter or change data), however that change is NOT saved in the database until you save the form or submit the survey.
Use the Data Quality rule H to find and fix all incorrect values for calculated fields at once in a project.
Can I calculate a new date by adding days / months / years to a date entered (Example: [visit1_dt] + 30days)?
No. Calculations can only display numbers or dates. You can't add to a date using a calculation.
Can fields from different FORMS be used in calculated fields?
Yes, a calculated field's equation may utilize fields either on the current data entry form OR on other forms. The equation format is the same, so no special formatting is required.
Can I create calculations and use branching logic to hide the values to the data entry personnel and/or the survey participants?
If the calculations result in a value (including "0"), the field will display regardless of branching logic.
You can hide calc fields with branching logic if you include conditional logic and enter the "false" statement to result in null: " " or "". For example: if([weight] > 100, 44, "") Then the field will remain hidden (depending on branching logic) unless the calculation results in a value.
You could also use an Action Tag:
@HIDDEN
Hides the field on the survey page, the data entry form, and in the REDCap mobile app. Field will stay hidden even if branching logic attempts to make it visible.
@HIDDEN-FORM
Hides the field only on the data entry form (i.e., not on the survey page). Field will stay hidden even if branching logic attempts to make it visible.
@HIDDEN-SURVEY
Hides the field only on the survey page (i.e., not on the data entry form). Field will stay hidden even if branching logic attempts to make it visible.
@HIDDEN-APP
Hides the field only on the form ONLY on the REDCap Mobile App. Field will stay hidden even if branching logic attempts to make it visible.
If you're still having issues formatting or troubleshooting a calculation issue, please email redcap@bcchr.ca.