REDCap conditional logic in automated survey invitations

270 Views Asked by At

I want survey 2 to be sent if survey 1 was completed before 09/03/2023. Does anyone know how that might be achieved?

I am specifying conditions for sending surveys. I tried using a smart variable: [survey-date-completed:survey_1] <= 2023-09-03. It says my syntax is valid, but when I test it on records it comes back false for those that should be true.

1

There are 1 best solutions below

0
Jangari On

The way to compare dates in REDCap is to use the datediff() function in your logic. Here is the syntax:

datediff ([date1], [date2], "units", returnSignedValue)

And the documentation from within REDCap:

Calculate the difference between two dates or datetimes. Options for 'units': 'y' (years, 1 year = 365.2425 days), 'M' (months, 1 month = 30.44 days), 'd' (days), 'h' (hours), 'm' (minutes), 's' (seconds). The parameter 'returnSignedValue' must be either true or false and denotes whether you want the returned result to be either signed (have a minus in front if negative) or unsigned (absolute value), in which the default value is false, which returns the absolute value of the difference. For example, if [date1] is larger than [date2], then the result will be negative if returnSignedValue is set to true. If returnSignedValue is not set or is set to false, then the result will ALWAYS be a positive number. If returnSignedValue is set to false or not set, then the order of the dates in the equation does not matter because the resulting value will always be positive (although the + sign is not displayed but implied).

So, to use logic to check whether a survey was completed before or on a specific date (namely Sep 3), use:

datediff([survey-date-completed:survey_1:value], '2023-09-03', 'd', True) >= 0

This will return True if the number of days difference between the survey_1 completed date and September 3 is greater than or equal to 0. If the survey_1 completed date was September 4, the datediff calculation would return -1 due to the returnSignedValue parameter being set to True, and thus the comparison -1 >= 0 returns False.

Also note that the [survey-date-completed:instrument] smart variable can be appended with :value to ensure that the date returned is in the format "yyyy-mm-dd" which is more appropriate for a datediff function.

You might be tempted to use the :value parameter in your original, but operators like <= and >= (or indeed any such comparison) are simply not reliable with datestrings. In my testing just now, everything returns False.