Start a conversation

Comparing variables with calculation fields

Comparing two variables

In calculations, you sometimes need to compare two variables against each other. Check out the article Using the "if-else" logic to see how this works.

For example, you want to check if one value is larger than the other and then show an error message if the condition is true. 

Below is an example that allows you to check if the diastolic blood pressure (diasbp) is larger than the systolic blood pressure (sysbp). 

var dbp = {diasbp};
var sbp = {sysbp};

if(dbp > sbp){
 1;
} else {
 0;
}

You can also use the following operators to compare two variables (instead of '>'):

  <   smaller than
  >   larger than
  ==  equal to
  !=  not equal to
 >=   greater than or equal to
 <=   less than or equal to

The calculation returns 1 if the dpb is larger than the sbp and 0 if not.  To set up a validation message message, use this setup:

  • If this field is equal to 1 
  • Show an error (or other type of) message. 


You can find a similar calculation here.


Assessing multiple comparisons

Sometimes comparing two variables is not enough and more conditions need to be assessed within an if-else statement. This can be done via two operators:
    &&   (AND)
    ||        (OR)

In the following examples you can see the use of these operators.

&& (AND)

var age = {part_age}
var trip = {part_travel}
if(age >= 18 && trip <= 6){
   1;
   }else{
   0;
   }

The result will be 1 only if both conditions are met. In this case, the patient is 18 or older, and has travelled within the previous 6 months.


|| (OR)

var ptrip = {past_trip}
var ftrip = {future_trip}
var tdis = {tropical_disease}
if(ptrip <= 6 || ftrip == 1 || tdis == 1){
   1;
   }else {
   0;
   }

Now, if just one of those conditions occur we will obtain 1. In the example, we obtain 1 if the patient has traveled within the previous 6 months, OR is planning to go on a trip in the future, OR has suffered a tropical disease.


&& and || can be combined but we will need parentheses to organize the operations.

For example:

var age = {part_age}
var ptrip = {past_trip}
var ftrip = {future_trip}
if((age >= 18 && ptrip <= 6) || (age >= 50 && ftrip == 1)) {
1;
}else{
0;
}

The evaluation of the code will happen in two steps: (age >= 18 && ptrip <= 6), (age >= 50 && ftrip == 1) and then ||.

In this example we obtain 1 if one of the blocks in parentheses is true: older than 18 AND traveled in the last 6 months, OR older than 50 AND planning a future trip.


Note

When using < and > with numbers, the value specified is not included. Thus, > 25 is equivalent to >= 26

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Castor Support Team

  2. Posted
  3. Updated