What is the Build My Calculation Tool? 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).
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:

Learn more about the Build My Calculation Tool

Learn more about REDCap calculations

Learn more about the Build My Calculation Tool

Learn more about REDCap calculations

Build My Calculation Tool Information
This tutorial has five different functions to create calculations with. Certain functions can be combined to do multiple calculations. There are more functions available in REDCap under the FAQ section , 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 the REDCap Administrator 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
can 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

Round

If/Then Statement

Sum

Mean

Date Difference

Round

If/Then Statement

Sum

Mean

Date Difference Function
What is the Date Difference function?
This function is used 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?
datediff([date1],[date2],'d','mdy')
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 the Round function?
This function is used 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 calculation, 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 rounding an age.
What does a round function look like?
round(([weight]/([height]*[height]))*703,2)
The number will round to whatever decimal point you specify: If/Then Statement Function
What is an If/Then Statement?
An IF statement is a function used to create conditional logic and generate an output based on the logic you specify. In its simplest form, the IF function says:
IF ( statement with condition, result if statement is true, result if statement is false )
Example:
IF ( sky=blue, day , night )
So an IF statement can have two results. The first result is if your comparison to the statement is True, the second is if your comparison to the statement is False.
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. Although you can use If/Then statements for more than calculations and can use them to compare strings of text please remember that calculations cannot read or compute text fields. The calculation output must always be a number.

Clinical Use Examples of If/Then statements

Clinical Use Examples of If/Then statements

Some common examples include: scoring tools that have variables that contain options like "prefer not to answer" that are coded with a raw value, for example "98", which would skew your calculation. It could also be used to give you a certain output (this output will always be a number) based on another calculation.
For example: You are asking 3 questions about depression (variables [depr1], [depr2], and [dep3]) and getting the sum of the raw values. An IF statement can eliminate the raw values for "Prefer not to answer" coded 98 and sum the remaining variables.
So I want to write if([depr1]='98',0,[depr1]). This statement says that if my "depr1" variable equals 98, then make it 0, if "depr1" does not equal 98, then keep it at the score input. So the "Prefer not to answer" will not affect my overall score.
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:

Advanced If/Then Statements

Advanced If/Then Statements

Can I create nested if statements?
IF statements may be used inside other IF statements ("nested"). For example, if you wanted a calculation to eliminate a variable that has 2 raw values that would skew the score like 98 "Prefer not to answer and 99 "Unknown", you could do the following:
(sum((if([depr1]='99','0',(if([depr1]='98','0',[depr1])))),[depr2],[depr3]))
This statement says that IF depr1 is marked '98', then show '0', IF not, then calculate IF depr1 is marked as '99', show '0', and if not, then show the raw value of depr1.
Sum Function
What is the Sum function?
This function is used to add up any variables in your project that return a number value.
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?
Yes and No. 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. This means you can sum the number of checkboxes that are marked as "checked" but you will not be allowed to sum a value that is assigned to a checkbox.
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 function will fail and the output will be blank. The sum function will sum all of the values that are filled out, ignore any blank values, and give you an output.
What does a sum function look like?
sum([value1],[value2], [value3])
Once your sum calculation is added as a calculated field, you will see the numerical output in red: Mean Function
What is the Mean function?
This function calculates the mean for any variables in your project (except text fields that are not validated to a numerical value and checkboxes).
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?
mean([hemoglobin1],[hemoglobin2],[hemoglobin3])
Once your mean calculation 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

Next Page >>
Save & Return Later