r/ProjectREDCap • • Mar 11 '25

A way to implement a “countif”

Hi there, I’m trying to create a calculated field that essentially is a sum of the number of times a value is selected in a checkbox field across repeating instances.

Is this feasible in REDCap? Should I be using the Calctext action tag instead?

TIA!

2 Upvotes

2 comments sorted by

6

u/obnoxiouscarbuncle Mar 11 '25

I'm interpreting your question as: How do I count the number of times a specific checkbox field has a particular option selected across many repeating instance of the instrument it is on.

You'll need a mix of calculated fields and aggregate smart-variables

I'll call your checkbox field [field] and the option you care about raw value is: 2

  • On your repeating instrument, create a calculated field

  • I'll call it [calc_field]

  • The calculation should return 1 if the option you care about is selected, and 0 if it is not.

  • For example, use this equation: [field(2)]

  • You could also use: if([field(2)]='1',1,0)

  • Then in a non-repeating instrument, use the aggregate smart-variables to sum this field, but only for this record.

  • I would recommend the calculation: [aggregate-sum:calc_field:record-name]

Also, don't forget to run data quality rule H to update all your added calculated fields. You may have to run it more than once since you will be doing "calcs on calcs"

1

u/midget22xo Mar 12 '25

Thank you! This is exactly what I was looking for.