Hello, I need to create a summed score of a set of questions each with yes / no (1/0) responses but am having difficulties doing this in some cases…Im sorry that this is very basic.
I’m am calculating the summed score in a rather long winded way (e.g. Q1 + Q2 + Q2 etc.) which at least works - sum(Q1,Q2, Q3…) does not work.
Anyway, some questions get skipped depending on the answer to the previous question (e.g. if Q1 = No (0) then do not ask Q2). In those cases, the summed score doesn’t not work. Does anyone know why, and how I might be able to fix this?
There are other cases where the question gets skipped completely e.g. if the health facility is in district x, don’t ask this question - the question being skipped is not dependent on the answer to a previous question, and it does not affect the summed score.
Does anyone know how I can correct this?
I believe that what you’re looking for isn’t happening like you expect is because of one little thing: CommCare functions and statements can sometimes get touchy if they are expecting a value and you don’t pass one to it. for example, you’re trying to add values that are numeric together, but, when you don’t have an actual value to sum, sometimes it causes commcare to not be able to recognize if it’s a “0” value, or a “Zero” value, or a “0.0” value, or any of the possible data objects that can carry a “Zero”.
So what do we do?
You have the following options:
1- use the function called coalesc. (https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-coalesce) so basically you can say : coalesce(Q2, 0) + coalesce(Q3, 0) + …
the coalesce function will try to find a value in Q2, if it finds one, it’ll use it, if it doesn’t, it’ll use the next argument, which in this example is “0”. this way, you know that you will always have a integer value to add up even if the questions aren’t answered.
2- use the function “Sum()” found here (https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-sum). The complication here is that the function “Sum” will only take a nodeset. a node set is essentially a collection of data structures linked together by a a parent data structure. But to avoid confusion, think of it as a series of the same question grouped together. so, if you change the way you’re asking the group of questions to something that will allow you to ask the same question a number of times and collect the answers (for example, “How do you make an income” and then “How much income do you make” repeated for X number of times) you can fund out how much everybody makes if you use the function "Sum(“How much income do you make”). for this example, depending on what Q1-X are, you may or may not be able to use it.
whatever happens, the first option i described should do the trick. but it won’t make the formula any more friendly to type.