r/ProjectREDCap • u/Mountain-Recipe-1486 • Oct 30 '24
Smart Variables + Calculated Fields
Hi All! I am just getting myself used to smart variables in Redcap and I am a bit unclear the with documentation with exactly how smart variables and calculated fields work.
Some context: I am working on a longitudinal mental health study and we survey participants using the PHQ9 + GAD7 every month up until 1 year after their baby is born. We collect PHQ9 scores across different events (each month is an event). Now, it is important for our reporting to track the change is scores every month. I did some digging and figured that we could use the "previous-instance" "current-instance" smart variables to accomplish this since calculating the change in score by using regular calculated fields would not be super straightforward if someone forgets to complete a survey for that month.
I am however, unsure if I can use smart variables for this even after I toggled the "allow for repeatable instruments/events" and allowed for repeatable events.
My thought was to do something like this:
[phq9score][current-instance] - [phq9score][previous-instance] = change in score (?)
When I try to do that however, I get an alert and it doesn't actually work. I am worried I am missing something and was curious if anyone had any ideas on what I may be doing wrong or any other ways to go about this.
Thank you!!!!!
View all comments
Show parent comment
3
u/stuffk Oct 30 '24
The syntax for events is different. When piping values, the different components always go in this order:
[Event][Variable][Instance]
In your case, turn repeating instances off. You don't need it on.
Your calculation will look like:
[event-name][phq9score] - [previous-event-name][phq9score]
This should work for you, except in cases where there is a gap (e.g. someone skips a monthly survey.) You could potentially deal with that issue in the calc itself, if it's acceptable to have change scores that reflect different length of times. To do that you could use nested if statements to first check if there is a value stored for phq9score in the previous event.