Skip to main content

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.

  1. When you add the field, select "Calculated Field" from the Field Type drop-down:

A list of the field types available in the "Add New Field"
menu of the REDCap instrument design screen. "Calculated Field" is
highlighted and has a red box around it.

  1. The field interface will look the same as any other field type, except for the "Calculation Equation" box.

An image of a Calculated Field with a red box around the
Calculation Equation box.

  1. 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.

The "Logic Editor" screen that pops up when the "Calculation
Equation" area is selected. A red box surrounds the buttons for Smart
Variable, Special Functions and Action Tags.

  1. 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.

The Codebook page with a red box around "(integer)", which
indicates the field is validated for integer.

Looking at the Designer page, you can see the words "View equation" next to the calculated field:

A screencap of the Designer view where three fields are
visible: "First Number", "Second Number", and "Calculation". Next to the
"Calculation" field there is text that says "View equation".

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.

A screencap of the equation view for the calculated field. It
provides information about the calculation and also the fields utilized
in the calculation (including Variable Name, Field Label for each field,
and which form each field is found in).

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:

The data entry view of the calculated field. The first number
is 3, the second number is 2, and the result from the calculation is 4.

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:

Logic Editor box with red boxes around "//".

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.

Calculation Equation section of a calculated field with red
boxes around "How do I format the equation?" and "Special Functions".