Calculated Fields - REDCap How
Calculated Fields - REDCap How
Calculated Fields - REDCap How
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?
+ Add
- Subtract
* Multiply
/ Divide
Null or blank values can be referenced using ”” or “NaN”
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.
Can I 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.
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.
date format
"ymd" Y-M-D (default)
"mdy" M-D-Y
"dmy" D-M-Y
Can fields from different EVENTS be used in calculated fields (longitudinal only)?
Yes, for longitudinal projects (i.e. with multiple events defined), a calculated field's equation may utilize
fields from other events (i.e. visits, time-points). The equation format is somewhat different from the
normal format because the unique event name must be specified in the equation for the target event.
The unique event name must be prepended (in square brackets) to the beginning of the variable name
(in square brackets), i.e. [unique_event_name][variable_name]. Unique event names can be found
listed on the project's Define My Event's page on the right-hand side of the events table, in which the
unique name is automatically generated from the event name that you have defined.
For example, if the first event in the project is named "Enrollment", in which the unique event name for
it is "enrollment_arm_1", then we can set up the equation as follows to perform a calculation utilizing
the "weight" field from the Enrollment event: [enrollment_arm_1][weight]/[visit_weight]. Thus,
presuming that this calculated field exists on a form that is utilized on multiple events, it will always
perform the calculation using the value of weight from the Enrollment event while using the value of
visit_weight for the current event the user is on.
Can REDCap perform advanced functions in calculated fields?
Yes, it can perform many, which are listed below. NOTE: All function names (e.g. roundup, abs) listed
below are case sensitive.
Name/Type of
Function Notes / examples
function
If the "decimal places" parameter is not provided, it defaults
round(number,decimal
Round to 0. E.g. To round 14.384 to one decimal place:
places)
round(14.384,1) will yield 14.4
If the "decimal places" parameter is not provided, it defaults
roundup(number,decimal
Round Up to 0. E.g. To round up 14.384 to one decimal place:
places)
roundup(14.384,1) will yield 14.4
If the "decimal places" parameter is not provided, it defaults
rounddown(number,decimal
Round Down to 0. E.g. To round down 14.384 to one decimal place:
places)
rounddown(14.384,1) will yield 14.3
sqrt(number) Square Root E.g. sqrt([height]) or sqrt(([value1]*34)/98.3)
Use caret ^ character and place both the number and its
(number)^(exponent) Exponents exponent inside parentheses: For example, (4)^(3) or
([weight]+43)^(2)
Returns the absolute value (i.e. the magnitude of a real
abs(number) Absolute Value number without regard to its sign). E.g. abs(-7.1) will return
7.1 and abs(45) will return 45.
Returns the minimum value of a set of values in the format
min(number,number,...) Minimum
min([num1],[num2],[num3],...)
Returns the maximum value of a set of values in the format
max(number,number,...) Maximum
max([num1],[num2],[num3],...).
Returns the mean (i.e. average) value of a set of values in the
mean(number,number,...) Mean
format mean([num1],[num2],[num3],...).
Returns the median value of a set of values in the format
median(number,number,...) Median
median([num1],[num2],[num3],...).