How to streamline data capture and increase data quality using calculation fields in Castor
Learn how to configure calculation fields in Castor EDC for edit checks, multi-field dependencies, clinical scores like BMI and Apgar, and date calculations.
Jolien LamTechnical Customer Success Specialist, Castor
Jolien LamTechnical Customer Success Specialist, Castor
About this webinar
This on-demand webinar walks through how to build and configure calculation fields in Castor EDC to streamline data capture and improve data quality. Led by a Castor technical customer success specialist, the session is designed for study builders and data entry staff working on study form structures. It covers the syntax for referencing study variables, writing if-else statements with comparison and logical operators, and configuring edit checks and dependencies across multiple fields. The walkthrough also demonstrates how to calculate clinical scores such as BMI and the Apgar score, handle incomplete data using empty-field tags, and perform date calculations including age at visit, chronological validation, and follow-up date scheduling using JavaScript functions and moment objects.
Key takeaways
- Variables from any step or phase in a study structure are referenced in calculation fields using curly brackets, with quotation marks added for text and date fields.
- If-else statements combined with comparison and logical operators can evaluate multiple inclusion criteria at once to trigger data validation warnings or conditional follow-up questions.
- Clinical scores like BMI and the Apgar score are calculated using standard mathematical operators, intermediary JavaScript variables, and functions such as Math.max.
- Adding the allow empty and set empty to zero tags lets Castor execute a calculation even when some input fields have not yet been filled in.
- Date differences in days, months, or years are calculated using the getDateDiff function or by converting stored date text into JavaScript moment objects.
- Converting a visit date into a moment object allows study builders to automatically add days, months, or years to schedule follow-up dates and verify chronological order.
Full transcript
Hello everyone, welcome to the webinar on calculations in Castor et Cie. My name is Jolene and I am a technical customer success specialist at Casa ADC, and today we'll focus on the different kind of ways you can use calculation fields in your study, for example to streamline data capture and increase data quality. If you'll want to see this webinar, Beck will record it, and you will always be able to access it afterwards. We'll release it on YouTube in a few days, so don't worry if you need to miss part of the webinar. Um, so let's get started; I will share my screen with you.
All right, so before we start on calculation, before we dive into the details, let's see how we can use variables in calculation fields. And let's say we have a calculator, a field that is called H, a number field, and you want to use the age to calculate something. Now, how can we use a variable in your study, in your calculation field? You need to put it in between curly brackets—so the, these brackets. And if you have any text fields or dates, you can put them in between quotation marks, so as you can see over here.
So let's see how it looks like in Castor. So I open the form structure, and for example I have an H field in here, and I want to show the age in my study form. So as we told, as we spoke before, they can show the age in between curly brackets. Save it. So if you have your calculation field, you put it in a calculation template. We go to the record and see how it looks like, and we see the H displayed over here. So if we put here 18, then 18 will be shown. And I will also zoom in a little bit so you can see it better.
So if you want to show the date of event, you have to put it in between quotation mark in addition, as we discussed previously. So let's do that: these events, put it in between quotation mark, save it, and let's see how it looks like. So here we have the date, and we can change the value of it to 14 May of 2019, and the date will be shown here in a calculation field. So this is a little bit of an introduction on how you can use your variables in your calculations. But what can we actually do with this?
You can set up data validations, also called edit checks. It can create dependencies on multiple fields, so if you have a field that you only want to show based on the value of another field, then you can create the dependencies. You can calculate scores; for example, BMI is a very simple score that you can calculate, and we'll go through different examples of that. You can calculate dates with dates, for example if you want to know the difference between a date of birth and the date of a visit in years—so basically the age on the date of the visit, you can do that with a calculation field.
OK, so let's go to a different step of calculation fields. This is a very basic thing that is used very regularly, and it's good to know this before we actually dive in to real examples—no, what are we going to do? So what is an if-else statement? It is a piece of code, sort of a piece of code that is commonly used to perform different actions based on different conditions. It executes a block of code if a specified condition is true; if the condition is false, another block of code will be executed.
So what does this mean? It might sound very, yea, strange at first, but let's see how it looks like, how the code looks like. So if a certain condition is true, and a condition will be shown here, then we will execute this action, which will be shown here, which is in between the curly brackets. And if the condition is not true, then we will execute this action. So how does that look like in Castor?
For example, if H—and we saw that if you want to use the H, you have to put it in between curly brackets—if the H is greater than or equal to 18, then we say the patient should be included; otherwise, the patient should be excluded. So is, so if the H is less than 18, the patient should be excluded. So let's see how that looks like in Castor. So for example, we have two H over here, and if I had could entered 17, then we want to show the text the patie...
Nt should be excluded, so let's put that in a calculation: so if H is greater or equal to 18, then the patient can be included; otherwise, the patient should be excluded. Elisee, let's see how that looks like from the data entry point of view. I have entered 17, and if I now enter 18 in the H, then it says the patient can be included. So, as you can see, you can also use variables that are in different steps or phases in your study structure; as long as it is real variable in your study structure, you can use this variable in, in your study.
So let's focus on this sign over here. So I told you before that this means greater than or equal to 18, but let's say we want to include only children in our study, that we should say if H is smaller than or equal to 18, then the patient should be included. So then your code should look like this, and these kinds of characters that can be used in compare different values are called comparison operators. And there are different operators that you can use, and these are the most commonly used ones, and you can also find these on our help desk very easily if you—if you forget how to write them. Um, yeah, so this was kind of sort of a data validation already.
But let's make it more complicated: what if I have more than one field that I want to check? So, for example, I have a couple of inclusion criteria to check if a certain patient needs to be included in my study. For example, if my age is greater or equal to 18, and if the patient has signed informed consent—so a form consent is equal to yes—and if the patient has diabetes, for example, then we say the patient can be included, and otherwise the patient should be excluded. You can write any message that you want to show to your data entry stuff.
So let's see: we have three variables in here, and we know how we can say the age should be greater or equal to 18—we did that previously. So how do we add multiple conditions? If we want to add multiple conditions, you have logical operators, and those are characters that are used to say, "I want to have this condition and this condition and this condition." So this to end tokens are used for "and", and to vertical stripes are used for "or". So in this example, I use three conditions. Let's see again how that looks like in Castor. So I have three variables in here: age, it's informed consent, and diabetes, and one calculation field that asks, "Can the patient be included?"
So here we have the end tokens or characters: so if H is greater or equal to 18, if informed consent is 1, and if diabetes is 1—so yes—then the patient can be included; otherwise, the patient should be excluded. So let's see how that looks like from a data entry point of view. And let's see: if I now enter here 17, then the patient is not 18 or older, then the condition will not be true because one of the conditions is not true.
So the patient did sign informed consent, the patient has diabetes mellitus, but because the age is 17, the patient should be excluded. So this is a very simple message that is shown to that, to the user, but what if you want to have like a clearer message shown to the user? For that, we can—we can use data validation messages. A—for example, I want to show a message if the patient should be excluded, because it's very important to, to, to show the user like, "This patient should be excluded, it's not part of the study."
So we go to the data validation tab. We add a message: if this field is equal to—and I now just paste the text over here, but it could be any value that you put over here—then show, for example, a warning and say, "This patient should not be included"—please do something like that. Let's save it, and let's see how it looks like. Now we get a warning message saying, "This patient should not be included." And if I say yes, I say 18, then the message will disappear. And if I now click no over here, then the message will appear again. So this is how you can create data validations with several conditions. If I, for example, want to show an additional question, then we would call that multiple dependencies. So le...
T's see. Let's go back to our example: H is greater or equal to 18, sign informed consent, and the patient has diabetes mellitus. And then, if all of these conditions are true, then we want to show a question—for example, confirm that the patient can be enrolled in a study. Maybe you want to have an additional check from the user. So, let's see, we want to add a question once these conditions are true. And, for example, I can use the same formula, but now I say one and zero as the actions to give the calculation field a value of one and zero. And now we can add a dependency to the question.
So, let's go back to the form structure, and and let's edit this calculation field. And let's say, if the patient can be included, then return one; if the patient should be excluded, we return zero. And then we can add the question, "Please confirm that the patient can be included"—something like that's yes or no. And we want this question to appear only if all conditions are true. So, if this calculation field is equal to one, then this field will appear. So let's again see how that looks like.
So now, if I say, "Did the patient sign informed consent?", put it to one, then the calculation will be equal to one. Then, if question appears, "Please confirm that the patient can be included", and we can enter any value over there. And we can also hide this calculation, because it looks a bit weird when it says one. So let's hide the field and see what happens. So now it looks a bit prettier without the calculation field, but it does still work. So this is how you can set up multiple dependencies in your study.
So, let's do a quick recap. Recap: an if-else statement executes a block of code if a specified condition is true—so, for example, if H is greater or equal to 18. If the condition is false, another block of code is executed. And we have comparative operators, which can be used to compare certain values to each other, and we have logical operators to include multiple conditions in your calculation. All right, let's move on to the next one.
The next topic is calculating scores, and there are so many scores that you can use in your study. So, let's go through a couple of simple ones to give you an insight of what you can do. So, a simple example would be the BMI, and the formula—you can see it here. And so we have two variables in our study, and how can we use these variables to calculate the BMI? You can very simply do that by just typing out the formula and filling in your cast of our variables. In calculation fields, you can use the mathematical operators very easily; it's it's very simple, straightforward, and mostly, yeah, close to what is normal.
So, let's see a different example: for example, the Apgar score, which is the sum of five variables—so activity, pools, etc. And every field has field options; for example, active has a value of two. And if you sum of all of these values, then you will have the up car score. So, let's see how that looks like in Castor: so, all the fields are here with the several options, and in the end, you want to show the Apgar score over here. So that is the sum of all options you have.
Let's see how we can do that. First, I create a variable, score, and then this court is the sum of all the variables, and then I return the score again. So, what is this far you see here? This means that a new variable is created within your calculation, so it's not in your study or something; it's just used in your calculation field, and you can use this as a sort of intermediary variable in your calculation field. And in the last line, we returned the variable to show the value of this variable. So let's do that in our in our form and see how that looks like.
So, we go to my app car step, and I copied the score over here so I don't have to type it again. So let's save it and let's see how that looks like. So if I now fill in active, which is 2, and all of these, then the sum will be calculated and the score is 10. If I now do this, then the score will be nine; the score is now eight; this course now seven—so it can calculate the the sum of all variab.
Les, if I now unselect one of the variables, then it will say, "Not all values for this calculation are available yet." So what does this mean? It means that this one is not filled in yet, and that's why the calculation will not be run. So, for example, if I want to calculate the score even though one of them has not been filled in, then how can I let this calculation run? Um, well, let's see on the help desk, because I'm not entirely sure how we can do that.
So if I have an empty Bible, and let's see: forcing Castor to calculate with empty fields. And our help desk is basically our manual, in which you can find everything about Castor if you need any information. So, forcing Castor to calculate with empty fields, and we need to use these texts. Let's just copy them. The "allow empty" tag tells Castor to allow for empty fields to be used in a calculation. So if we use this tag, then the calculation will be run even though not every field has been filled in.
What does this mean, "set MT to zero"? It means that if you have a field that has not been filled in yet, and you allow empty fields, then those empty fields will be set to zero; their value will be zero. So, for example, if I would add those tags to my calculation, which I will do in a minute, then this field will be set to zero. So let's check it out: allow empty and sets empty to zero. Now, that's safe this. Now I can see that the score has been calculated even though not all fields have been filled in. So if I now fill this in, then the calculation will be updated.
All right, if you need more advanced mathematical calculations, then check our help desk, which is help desk that Castor ECT. Um, yeah, let's just look for it instead of clicking on the link. So if I want to have more advanced mathematical calculate functions, then you can go to this calculation fields and syntax article. And let's see where we have all the mathematical functions which are listed over here. So, for example, you can use the absolute value of a variable, you can use the maximum value of several variables, you can have the square root of a variable. So these are mathematical functions which you can use for your own calculations.
And I won't go into detail to into a detail for those functions, but because of the time, let's move on to something else. So this is an example of mathematical functions used in in your calculation. Because of the time, let's go ahead with calculating with dates, because dates is—yeah, it can be complicated to calculate with, and that you can you can run into many problems when you calculate with dates. So let's see what kind of examples we have. For example, you want to calculate the difference between two dates—for example, the difference between the date of birth and visit date in gear. So that is basically your age at the visit date.
So let's see. Um, yeah, let's also look that up in our help desk, because we have many, many examples of that. So if you want to know the day difference between two dates, then it is already the first article that shows up. And there are several ways to calculate the difference between dates. For example, "gets they diff"—this is a function, so this is the function that you use, which will always be the same. And you can enter your variables over here: show your dates variable, the most recent one, so the visit date would be date two, and then our date of birth would be date one. And this means—Y means the unit in which the difference is returned; that is years in this case.
So what if I wanted to know the difference in months or days? Then in days, it would be replaced with D. If you want to know the day difference in months, then you can use moments objects. So let's see how that works. I—I can copy this piece of code, and I already entered it here in my presentation. So let's see what happens here: we create a new variable, admission, which is a moment. And moment is a kind of object that you can use in JavaScript, in our, in our, in our code, so that the system knows of this piece of code is actually a dates with a date instead of with the text b.
Ecause in Castor, dates are stored as text in a format DD - and M - bye bye bye, and we can transform this text into a moment object with, which happens over here, so that the system knows that it is a date. And so if you make it a date, you can easily create calculations with that. So we have our dates, we can enter that over here, and this one says that the date is in the format of how Castor sees a date. The same happens for the discharge, and then the difference is calculated. So diff is here our function, and it calculates the difference between admission and discharge in days.
So now I have my date of birth and my visit date. How can I calculate the difference between those dates? Oh, so we can replace our variables here. So I pasted the date of birth in here, my visit date, and the difference will be calculated in years, because I want to know that in years. So let's see how that looks like in Castor date differences. For example, I have my date of birth over here, which is 5 March 1995, my visit date, which is 15 of May 20 19, and I want to know the difference in years. So let's add a calculation for that.
Mmm, it's differences calculation. So let's see: the age adds visits updates. I want to use the visit dates, so I can just copy again my calculation over here from the help desk. And the date, the H will be calculated in years, and I will move this a bit upwards so that it's clear where it belongs to: difference in years. So let's see, we have my date of birth, the visit date, and the age at visit. So what if I change this one to 1991 March 5th? Then the H will be updated with current age with a new date of birth. Okay, that is how we calculate the difference of two dates.
Now let's say we have a third date: randomization date. And if the physic date is not known, we're empty, then I want to know the age at randomization. How do I do this? So there is a third date that comes into the calculation. And if this date is not known then, and we have a randomization date over here, and we do know the randomization date, then we want to know how old was this person at randomization. But if the visit date is known, then we want to know the age at visit. So let's see, we know that not all fields have to be filled in, so that is why we can allow empty values, and we set empty to zero.
Now this is a good example for an if statement. So if visit date is zero—so if it's not known, if it's not filled in, then it is set to zero, which we have done over here, because it's an empty field it will be set to zero. So if this is equal to zero, then we want to know that, the—the difference between the date of birth and a randomization date in years. Otherwise we want to know the date, the age at the visit, also in years. And you might have already seen it in the data entry; let's see how that looks like again.
So let's make this empty. Then this one will not work because we didn't edit that, but the age at the visit will be shown, which is 20—at the randomization will be shown, which is 27. If I enter a visit date again, then it should be 28, because that's one year later. Now if I make this empty, then the age add visit will be shown still. All right, so what did we do? We allowed empty values, we set empty values to 0. If the visit date is empty and thus set to 0, then we get the difference between the date of birth and a randomization date; otherwise we get the difference between date of birth and the visit date.
So on to the next example on date calculations: what if you want to check if a certain date is before a second date? So for example, you have a filled date of birth and date of diagnosis, and you want to check if the user has entered it correctly. So is the date of birth before the date of diagnosis? So let's check our help desk again. So we want to know if the date is for another date, so check if a dates before another date, and there is article about that. So first create your date fields, then create your calculation field. All right, and then what kind of formula do we use for that? We can just copy this, and let's review this form.
Ula, so this is the same formula from the help desk. Again, we create a variable which is the difference, and that is equal to get date difference between date one and day two in days. And if the date difference is less than 0, then we will output no, than a calculation field will be equal to no, otherwise we say yes. So let's see how that looks like in Castor. I have here two date fields: date of birth and the date of diagnosis. And let's make this one by accident 1998, which is before our date of birth, which is not possible. But now we want to check if the date of birth is before a date of diagnosis.
So let's add a calculation once again: check its date of birth is before diapers. So we create a variable which is called a fifth, so the day difference, and that one is to put it over here, and that one is able to get they diff between date of diagnosis, and that's called date underscore diagnosis, date of squirt diagnosis, and the OB. I think it's called like this. Let's check the OB you won, and I want to note the difference few years. Now if the dates dip is less than zero, so if there is no difference between the dates, then we say, we say yes. We can add a dependency or like a validation maybe over here.
So if this field is equal to, if they difference is no, then I want to go check, then I want to show. So let's see how that looks like: so the date of birth is not before the date of diagnosis, so then we say the date of birth is not before the diagnosis, and then we can show a warning, and we can enter any message over here. So then if we enter the right date over here, so 2018, 2015, okay 13 for example, then we see the date of birth is before the diagnosis, so that is correct. And in this way you can check if your data has been entered correctly.
If you want to add days to a date, for example, if you have a visit date you want to calculate the follow-up date, which can be 14 days after the visit date, then we can also do that. And let's see how we can do that: add these two dates, let's see if we can find something, add month to a date, so that is close enough, and let's see what happens. We have this formula which I will copy again, and let's find out what's in this formula.
So we have a variable which is the date, and we, it is a moment object that is made from our visit date. And because this is a moment object, we can add days to it very easily, which is done by date dot at 14:00 and days, and then we return the new dates in a format that is that Castor knows, so that is DD - mm - yyy. So let's see how that looks like in Castor again.
So I have my visit date over here, and we want to add 14 days to the visit date. So I create a new date which is an object moment, and that object moment is made out of my visit date. We create the new date which is our current date with 14 days added to it, and then our new data will be shown. So let's see: let's say 15th of May, and we have to add 14 days to the visit date, that will be 29 May. And let's see if we say 30 with me, then it will say 14 of June.
Now what if we want to add two months to the visit date? Let's go back to our formula. So we want to add two months, and right now it says 14 days. If we want to add two months, you can simply replace the number 14 with 2 and replace days with months. And let's save that and see what is the result. Let's zoom out a little bit. So now we have 30 of May, which now adds 2 months instead of 14 days, and it will say 30 of July 2019. So this is how you can add 2 months to a date, and you can do every, every kind of addition. Yeah, you can add days or months or years to your date; it doesn't really matter.
There are many options to to this formula. You can also test it in our calculation helper. So if I click on here, then our calculation field helper will be shown, and I will also put a link to the calculation field helper once we upload this webinar, so you can easily find all the links to our helpdesk into our field calculation helper. We are almost going to end, so I'm going to show you one more example that is two months, so what we did.
Was the same formula, but changing the number and the months. Now we want to add three years, for example; we can simply change this to three years so that our visit date is calculated correctly. So let's do a quick recap again: you can create new variables with far and then friable name, which can be any name you want. JavaScript has mathematical functions that can be used—for example, Math dot max, which shows you the maximum value of a set of values which have shown on the help desk. There are many different ways to calculate with dates; for example, you can calculate date differences with gets dates diff, and this needs to be a capital D.
You can check the valid, the validity of date with if-else statements—so, for example, if the date of birth is before visit date. And you can do date calculations with moment, with the moment object. So if you change a date that is—you Kaster, it is a text—if you change that to a moment object, then a system will know this is a date, so I can add days or at months to it, or subtract date days or years or months, whatever you want. And lastly, you can use allow empty and set empty to zero tags to use fields that have no value yet, that are not filled in.
All right, so we have come to the end of the webinar already, and I flew over all the examples that we have in calculation fields. If you have any questions, or if you—if you can't find anything on how you can do something, go to our helpdesk and type in your—your example. And it should be very, very high level, so not like, "Oh, I have my visit date and I want to know the difference between my visit date and my date of birth"—it is the difference between two dates that you actually want to know; it's not the age of a patient that you want to know, it's the difference between two dates. So those kind of things are very easy to—to find.
And we also have our calculations basics templates and calculations with date and time overviews over here. So if you go to the manual, and you will see many articles on our calculations which you can use—for example, also our general calculation templates: here is the BMI, for example, or you can calculate average of several variables. As I told you before, we also have our calculation field helper, which you can use; and let's just find that on our help desk: calculation field. Let's see: where—it's my calculation field helper over here. I can also use this to try out your variables in your calculation fields.
And last but not least, you can also go to our forums exchange, which is forums caster et Cie calm. Now you can find your calculations or calculations from your colleagues over here, and reuse those and import that into your study structure. Alright, well, thank you all for joining. If you have any questions, please send them to support at castor EDC comm. We are always available for your email, and otherwise via our chat in between business hours on Monday to Friday. So thank you all for coming, and have a nice day.

