Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Array (numbers) question where all values for each row have to add up to 100

More
9 months 1 week ago #270433 by care4limesurvey
Please help us help you and fill where relevant:
Your LimeSurvey version: 6.5.3+240415
Own server or LimeSurvey hosting: hosted by my university
Survey theme/template:
==================
I want to create a survey questions where participants enter which type of food they dispose of through different channels. For this I used an array (numbers) with each of the food types in the rows and the different disposal routes in the columns. Participants shall then enter the percentage of each food type they dispose of through the respective disposal route as a numerical value. 
 
 

So far so good. Now i want to add the condition that fo reach row/food type all the values must add up to 100, because you can only dispose of 100% of food waste but not for example 120%. And I can't for the life of me figure out how to do it. I don't have any programming/coding skills so I read through the manuals. I tried this code in the question validation equation field: sum(Q5_SQ1.NAOK) == 100 and it does not work at all (I only tried it for the first subquestion as a test). I also always get the "undefined variable" notification.  

How do I get this to work?

Please Log in to join the conversation.

More
9 months 1 week ago #270435 by Joffm
Hi,
the first - obvious - error is:
You have y-axis codes "SQ001", "SQ002", ... and also x-axis codes "SQ001", "SQ002", ...
But in your validation equation you adress a subquestion code "SQ1".

Next: Your array is a two dimensional array; to adress a cell you need the y-axis code and the x-axis code
As you see here in the manual
[url] www.limesurvey.org/manual/ExpressionScri...code_variable_naming [/url]
If you are not sure, just activate the survey, and have a look at the responses table.

Now the solution that comes to mind at first is: "Sum all cells of a row" - for all rows
sum(Q1_SQ001_SQ001.NAOK,Q1_SQ001_SQ002.NAOK,Q1_SQ001_SQ003.NAOK,Q1_SQ001_SQ004.NAOK,Q1_SQ001_SQ005.NAOK,Q1_SQ001_SQ006.NAOK)==100 AND
sum(Q1_SQ002_SQ001.NAOK,Q1_SQ002_SQ002.NAOK,Q1_SQ002_SQ003.NAOK,Q1_SQ002_SQ004.NAOK,Q1_SQ002_SQ005.NAOK,Q1_SQ002_SQ006.NAOK)==100 AND
...



But if you read the manual here
[url] www.limesurvey.org/manual/ExpressionScri...%22that%22_variables [/url]
you can shorten this.
But only if you do the coding a bit different.
Instead of  "SQ00x" in both directions you may use "Y01", "Y02",... for the y-axis, and "X01", "X02",... for the x-axis.
This way you won't get confused later (what was y, what was x) when you import the data into your analysis tool.

Then you can validare by
sum(self.sq_Y01)==100 and sum(self.sq_Y02)==100 and sum(self.sq_Y03)==100 and ...

But beware: You will get an initial error.
When the question is displayed all sums are equal 0 (<>100). So there is an error.

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: care4limesurvey

Please Log in to join the conversation.

More
9 months 4 days ago #270548 by care4limesurvey
So I just realized that it still doesn't work like I want it to. 

This is what my settings look like: 
   

and when I test the survey and enter 100 for each row I get this window:
 
and it looks like this:  

This is the code for the conditions I used: sum(G14Q52_SQ001_SQ001.NAOK, G14Q52_SQ001_SQ002.NAOK, G14Q52_SQ001_SQ003.NAOK, G14Q52_SQ001_SQ004.NAOK, G14Q52_SQ001_SQ005.NAOK, G14Q52_SQ001_SQ006.NAOK) == 100 AND sum(G14Q52_SQ002_SQ001.NAOK, G14Q52_SQ002_SQ002.NAOK, G14Q52_SQ002_SQ003.NAOK, G14Q52_SQ002_SQ004.NAOK, G14Q52_SQ002_SQ005.NAOK, G14Q52_SQ002_SQ006.NAOK) == 100 AND sum(G14Q52_SQ003_SQ001.NAOK, G14Q52_SQ003_SQ002.NAOK, G14Q52_SQ003_SQ003.NAOK, G14Q52_SQ003_SQ004.NAOK, G14Q52_SQ003_SQ005.NAOK, G14Q52_SQ003_SQ006.NAOK) == 100 AND sum(G14Q52_SQ004_SQ001.NAOK, G14Q52_SQ004_SQ002.NAOK, G14Q52_SQ004_SQ003.NAOK, G14Q52_SQ004_SQ004.NAOK, G14Q52_SQ004_SQ005.NAOK, G14Q52_SQ004_SQ006.NAOK) == 100 AND sum(G14Q52_SQ005_SQ001.NAOK, G14Q52_SQ005_SQ002.NAOK, G14Q52_SQ005_SQ003.NAOK, G14Q52_SQ005_SQ004.NAOK, G14Q52_SQ005_SQ005.NAOK, G14Q52_SQ005_SQ006.NAOK) == 100 AND sum(G14Q52_SQ006_SQ001.NAOK, G14Q52_SQ006_SQ002.NAOK, G14Q52_SQ006_SQ003.NAOK, G14Q52_SQ006_SQ004.NAOK, G14Q52_SQ006_SQ005.NAOK, G14Q52_SQ006_SQ006.NAOK) == 100 AND sum(G14Q52_SQ007_SQ001.NAOK, G14Q52_SQ007_SQ002.NAOK, G14Q52_SQ007_SQ003.NAOK, G14Q52_SQ007_SQ004.NAOK, G14Q52_SQ007_SQ005.NAOK, G14Q52_SQ007_SQ006.NAOK) == 100 AND ... and so on

Is there any way to do this so the participants don't have to enter 0 so they can get to the next question?

Please Log in to join the conversation.

More
9 months 4 days ago #270549 by Joffm
You set the question to "mandatory".
So this is obvious.
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

More
1 week 1 day ago - 1 week 1 day ago #273878 by glennkoyal

Please help us help you and fill where relevant:
Your LimeSurvey version: 6.5.3+240415
Own server or LimeSurvey hosting: hosted by my university
Survey theme/template:
==================
I want to create a survey questions where participants enter which type of food they dispose of through different channels. For this I used an array (numbers) with each of the food types in the rows and the different disposal routes in the columns. Participants shall then enter the percentage of each food type they dispose of through the respective disposal route as a numerical value. 
 
 

So far so good. Now i want to add the condition that fo reach row/food type all the values must add up to 100, because you can only dispose of 100% of food waste but not for example 120%.  culver menu food waste but not for example 120%. And I can't for the life of me figure out how to do it. I don't have any programming/coding skills so I read through the manuals. I tried this code in the question validation equation field: sum(Q5_SQ1.NAOK) == 100 and it does not work at all (I only tried it for the first subquestion as a test). I also always get the "undefined variable" notification.  

How do I get this to work?

 
I actually have the same question because I’m trying to set up a similar survey where participants enter percentages for different food types across disposal channels. I want each row to sum to exactly 100% as well, but I’m running into the same problem with the validation equation. Even when I use sum(Q5_SQ1.NAOK) == 100, I keep getting errors like “undefined variable,” and it doesn’t seem to recognize the array of subquestions properly. It’s frustrating because I don’t have programming experience, so I’m trying to rely on the built-in validation options and example code from the manuals.

I’m also wondering whether the solution involves referencing each subquestion individually rather than using a generic sum() function, or if there’s a specific syntax needed for percentage arrays in the survey software. It would be really helpful to see a working example or step-by-step guidance, because having the rows add up to 100% is essential for the survey to make sense, and I don’t want to let participants submit invalid data. If anyone has managed to implement this correctly, I’d love to know exactly how.
Last edit: 1 week 1 day ago by glennkoyal. Reason: Spelling

Please Log in to join the conversation.

More
1 week 23 hours ago - 1 week 23 hours ago #273879 by Joffm
Hi,

Even when I use sum(Q5_SQ1.NAOK) == 100, I keep getting errors like “undefined variable,” and it doesn’t seem to recognize the array of subquestions properly

This is not surprising; Q5_SQ1 is not a valid QCode for an array(numbers).
An array(numbers) has subquestions in two dimensions (y-axis and x-axis)
 
See the manual: [url] www.limesurvey.org/manual/ExpressionScri...code_variable_naming [/url]
And I always recommend to activate the survey and have a look at the response table. Here you see exactly how to access variables.
  

The full validation you already see in care4limesurvey's post.
You can shorten it considering the "self" variable
[url] www.limesurvey.org/manual/ExpressionScri...#%22Self%22_variable [/url]
Using "self" results in this: sum(self.sq_Y01.NAOK)==100 and ... for each y-scale subquestion (Y01, Y02,...).

Joffm



 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 week 23 hours ago by Joffm.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose