Click on the magnifying glass to the right. Starting typing a topic you wish to learn about

Calculated Fields - General Overview

REDCap has the ability to make real-time calculations on data entry forms and surveys.

This article reviews the below topics:

  • How to Format & Create a Calculated field + reporting considerations

    • Calculations using (Radio Buttons or Dropdown Lists)

    • Calculations using (Checkboxes – select all that apply)

    • How to use conditional logic in a calculated field

    • How to use the “sum” function vs. the “+” function

    • BMI Calculation

    • How to use DATEDIFF special function to calculate the difference between two dates or date times

    • Use different EVENTS in calculated fields (longitudinal projects only)

    • Preform advanced functions in calculated fields

It is recommended that calculation field types are not excessively utilized on REDCap data collection instruments as they can slow down the webpages.

The Process

In order for the calculated field to function, it will need to be formatted in a particular way. This is somewhat similar to constructing equations in Excel or with certain scientific calculators.

The variable names/field names can be used as variables in the equation, but you must place [ ] brackets around each variable. Please be sure that you follow the mathematical order of operations when constructing the equation or else your calculated results might end up being incorrect.

 Calculations in REDCap are formatted the same way as Excel, but instead of using the cell names ([A2]+[A3]), the variable names are used ([bpi_q1]+[bpi_q2]).

 How do I create a calculated field?

  • On the Online Designer page, you would select Calculated Field as the field type. Type the calculation into the Calculation Equation box. If you have test data in the project, you can test the calculation to see if it’s valid by selecting a study ID from the Test calculation with a record dropdown list.


Can I create a calculation that returns text or a date as a result (Ex: "True" or "False," “[visit_date] + 30 days”)?

  • No, calculations can only result in numbers.

 

What mathematical operations are available for calculated fields?

  • Null or blank values can be referenced using ”” or “NaN”

How you code a calculation can determine if it will display on a report.   

  • In the below equations, both will provide an answer on the form.  However if it is desired that the calculation display on a report, you must  use nested "if's" and can not use the "or" function.

    • The below calculation will show on the form but does NOT display on the report:

      (if([statement2]=1,0,0) or
      if([statement2]=2,1,0) or
      if([statement2]=3,2,0) or
      if([statement2]=4,3,0))

    • However, this calculation (with the same calculated outcome) will show on both the form and the report:

      if ([statement1]=1, 0,
      if ([statement1]=2, 0,
      if ([statement1]=3, 1,
      if([statement1]=4, 3, 0)))


**NOTE: You must EXPAND the below sections to view the full content. Click the (>) carrot to expand.

Performing calculations on multiple choice questions (Radio Buttons or Dropdown Lists):

REDCap uses the numerical value assigned to the answer as the answer’s value/score.

0, Never

1, Occasionally

2, Often

3, Always

 In this case, REDCap will score Never as ‘0,’ Occasionally as ‘1,’ Often as ‘2,’ and Always as ‘3.’

 For scoring questions that are radio buttons/dropdown lists, the Calculation Equation would contain the field/variable names of the questions you want to sum.

Note: If two or more answers need to have the same scoring, i.e., both Never and Always have a score of ‘4’, conditional logic will have to be used instead since REDCap will not allow two answer choices to have the same values/scores.


Performing calculations on multiple choice questions (Checkboxes – select all that apply):

Calculations for Checkbox field types are tricky because they are given a value of ‘1’ if checked and ‘0’ if unchecked. If possible, avoid use calculations for Checkbox field types.

 Although the field (‘exercise’) looks like this:

1, Monday

2, Tuesday

3, Wednesday

4, Thursday

5, Friday

 

The data comes out like this:

[exercise (1)]

[exercise (2)]

[exercise (3)]

[exercise (4)]

[exercise (5)]

1

0

1

0

0

So even though Friday has a value of ‘5’ its values are either ‘0’ or ‘1.’ The ‘5’ only refers to it being the 5th choice on the list. This is because statistical packages would not understand [exercise] = ‘1’ and ‘3.’

To score this field, each answer choice will need its own calculation, which afterwards can be summed:

[variablename(code)] (‘code’ refers to the position on the list, aka the value assigned to the answer)

if([exercise(1)] = 1, 1, 0) (if Monday is selected, give me 1, otherwise give me 0)

if([exercise(2)] = 1, 1, 0) (if Tuesday is selected, give me 1, otherwise give me 0)

if([exercise(3)] = 1, 1, 0) (if Wednesday is selected, give me 1, otherwise give me 0)

if([exercise(4)] = 1, 1, 0) (if Thursday is selected, give me 1, otherwise give me 0)

if([exercise(5)] = 1, 1, 0) (if Friday is selected, give me 1, otherwise give me 0) Then you could sum the five fields above, thereby creating a sixth calculated field.

Another method of would be as follows:

If you have a checkbox field [cities] asking which cities they’ve visited with 5 cities listed and they want the total number of cities the participant visited, they’d create a calculated field [cities_total]

sum([cities(1)],[cities(2)],[cities(3)],[cities(4)],[cities(5)])

To calculate if the person has visited 2+ cities:

if(sum([cities(1)],[cities(2)],[cities(3)],[cities(4)],[cities(5)])>1,1,0)

with a field note that 1=yes they’ve visited 2+ cities, and 0=no


Use conditional logic in a calculated field:

Yes, you may use conditional logic (i.e. an IF/THEN/ELSE statement) by using the function:

  • if(CONDITION, value if condition is TRUE, value if condition is FALSE)

This construction is similar to “IF” statements in Excel. Provide the condition first (e.g. [weight] = 4), then give the resulting value if it is true, and lastly give the resulting value if the condition is false.

Note: that if statements are calling text fields you will need to have “ “ around the string of text. For example if you are searching for a specific city, the conditional logic would be if ([city]=”Chicago”,1,0)

  • if([weight] > 100, 44, 11)

In this example, if the value of the field 'weight' is greater than 100, then it will give a value of 44, but if 'weight' is less than or equal to 100, it will give 11 as the result.

Consider the question (‘xxx’) with the following answers:

 0, Never

1, Occasionally

2, Often

3, Always

99, N/A

 If someone selects ‘N/A’ as an answer and you do not want REDCap to score that value as ’99,’ you want the value to be blank, you would use the conditional logic:

  •  if([xxx] < 99, [xxx], “NaN”)

In this example, if the value of ‘xxx’ is less than 99, then it will give the value of the answer that was chosen (0-3), but if ‘xxx’ is equal to 99 then it will give “NaN” (blank) as the result.


Using the “sum” function vs. the “+” function:

sum([q1],[q2],[q3],[q4]) will give the sum of questions 1-4, even if one of the values is blank. NOTE: All blank values will be ignored and thus will only return the sum total computed from all numerical, non-blank values.

 [q1]+[q2]+[q3]+[q4] will give the sum of questions 1-4 but only if every question has a value. NOTE: Blank values will not be ignored and thus will only return the sum total if all the fields in the equation have values.

 Weather to use the “+” or “sum” function depends upon how that particular instrument should be scored.


BMI Calculation:

[weight]*10000/([height]*[height]) = units in kilograms and centimeters

([weight]/([height]*[height]))*703 = units in pounds and inches

 Note: ‘weight’ and ‘height’ need to be field names in your project. If you called these fields something else, use the names you called those fields instead of the ones listed above.


Calculate the difference between two dates or date times: (DATEDIFF Special function)

  • (i.e., age at enrollment based upon DOB and date of enrollment, length of hospital stay based on admit and discharge dates): datediff([date1],[date2], "units", "date format", Return Signed Value)


Use different EVENTS in calculated fields (longitudinal projects only):


Perform advanced functions in calculated fields:

  • NOTE: All function names (e.g. roundup, abs) listed below are case sensitive.


 **This Knowledge Article has been developed by the University of Utah’s REDCap Analysts using data garnered from the REDCap Consortium Library and other REDCap Affiliate members’ online resources and documents