Calculated Fields
Introduction
Calculated fields are a type of field that automatically evaluates any
math involved with other fields in the REDCap project. Calculated fields
can perform any standard mathematical operations (addition +
,
subtraction -
, multiplication *
, or division /
).
Fields that can contribute to the equation part of a calculated field are:
- integer and number validated fields
- fields that have a numerical value as the raw code value in a choice list.
Instructional Video Guide
Step-By-Step Guide
Calculated fields are built in the same way as any other type of REDCap field.
- When you add the field, select "Calculated Field" from the Field Type drop-down:
- The field interface will look the same as any other field type, except for the "Calculation Equation" box.
- When you click in the "Calculation Equation" box, the Logic Editor will appear. This is a space where you will enter your equations. You can get help with building your equations using Smart Variables, Special Functions, and Action Tags by selecting each of the three colored buttons respectively above the box in the Logic Editor.
- When writing calculations for a calculated field, fieldnames must be
enclosed in square brackets
[ ]
. The order of operations is preserved during calculations, so the expression can be kept clear with parentheses( )
.
Example
An example calculation might look like:
(\[first_number\] \* 2) - \[second_number\]
If we look at the Codebook, we can see that the two variables that contribute to the equation are validated for integer.
Looking at the Designer page, you can see the words "View equation" next to the calculated field:
Upon clicking the "View equation" link, you will see the components of the calculation, including the variable name, field label and the name of the instrument in which they appear.
In the data entry view, you will see the Calculated field populate with a number, based on the numbers entered into the First Number and Second Number fields:
Implementation for UDS4
Calculated fields will appear in the Data Element Dictionary. This Data Element Dictionary will be distributed by NACC.
A more complicated equation, like the CDR calculated field that is included in B4, might pull several calculated fields into it. To keep the text clear, the developer can enter two forward slashes and then write in regular English what it is that the statement is doing:
Troubleshooting / Caveats
-
When performing calculations on categorical fields, remember to add quotes around the raw code values. Numeric fields do not need the quotes (as seen in the example above).
-
All calculations that do not result in a number (such as a blank or Null value) will instead output
""
, so when using any logic or action tags, keep in mind that the calculated field will be""
rather than empty. -
The values that populate a calculated field cannot be edited on the data entry page.
-
You cannot import a value into a calculated field (using the Data Import Tool, for example.) You can only import values into the fields which contribute to the calculated field.
-
If you wish to hide the calculation from the user entering data, you can apply the action tag
@HIDDEN-FORM
to the field.
Additional Resources
The REDCap interface provides additional help with constructing calculated fields. See "How do I format the equation?" or "Special Functions" next to Calculation Equation.