OpenClinica User Manual/Calculations
Calculations and functions in a CRFEdit
In your XL for the CRF you can choose in column N, RESPONSE_TYPE, the type "calculation". In other programs this is sometimes called a Derived Field. You can use it for example to add the values of two items, Item1 and Item2, both integers.
The way to do this is make an item SumOfItem1AndItem2, type calculation. Now, we will start with column Q RESPONSE_VALUES_OR_CALCULATIONS. Here we put our calculation in format func: Item1 + Item2. Because of the fact that we fill in something in this column, we must also complete the two preceding columns, RESPONSE_OPTIONS_TEXT and RESPONSE_LABEL. To keep things easy, we just fill in "calc" for both.
Using the pow-functions in your calculationsEdit
In the example above we added the values of two items and likewise we can subtract them, but what if we want the difference between the two items? And not just Item1 - Item2: we want the absolute difference between Item1 and Item2.
In our calculations, we can use two functions, POW and DECODE.
POW stands of course for Power and it works with two parameters: a number and the power (surprise!) For example pow(2,3) is 8. We can use 1/2 for the square-root, so pow(25, 0.5) is 5.
We set this to work in our quest for the absolute, by typing in column Q: func: pow(pow(Item1-Item2,2),1/2). This functions first takes the power of 2 of the difference and then takes the square-root, resulting in the absolute difference.
Using the decode-functionEdit
Another example of using a function in your CRF can be when you want your users to enter temperature in both Fahrenheit and Celsius. You design your CRF with a field Temperature and a field TempUnit and also a calculated field TempInCelsius. In TempUnit the user chooses between Fahrenheit or Celsius and the result is F or C.
In our calculation to degrees Celsius, we will use the decode-function. The syntax of this function is
- a variable to evaluate
- one or more key-value pairs
- a default
In our case we will test item TempUnit. If this is "C", then we will take the value of Item Temperature. In the other case, so the default, it will be Fahrenheit, so we will subtract 32 and then multiply it with 5/9. The function then looks like func: decode(TempUnit,C,Temperature,((Temperature-32)*5/9)).