# Calculating the average value of a 5-item grid.

Hi,

Im currently designing my survey and I have a grid question where respondents need to select how much they agree/disagree to 5 statements (1-strongly disagree, 5-strongly agree). I want to calculate a numeric variable that computes the average value for each respondent since I want to include this continuous variable as covariate later in my analysis.

I searched in the forum and I found a possible way to calculate such variable by creating a new variable within a Free Format question, and make it a "hidden" type variable.  Then in the html box add [% (Q1 + Q2 + Q3 + Q4 + Q5)/5 %] this script. I followed these instructions but instead of having different questions within the brackets I had the different rows as followed:

[% (IndScale_r1 + IndScale_r2 + IndScale_r3 + IndScale_r4 + IndScale_r5) /5 %]

The problem is that this doesnt seem to work because when I filled in a questionnaire and downloaded the data the column refers to the new variable is empty. So no value was calculated. In addition, when I filled in the survey I noticed that in the place where the new variable was added a "0" appeared. That is kind of weird since I clicked at the "hidden" format.

Am I doing something wrong? Is there another way to calculate the average value per respondent for a grid?

Thank you :)

+1 vote
You could create a pass-in variable and use setvalue.  Say you create a pass-n variable called GRIDAVG.  Then, in the header or footer of a questions after those 5 that all respondents will see, use this script.

[% SetValue(GRIDAVG,(Q1 + Q2 + Q3 + Q4 + Q5)/5) %]

If you are doing this from answers on a grid question, make sure that you use the correct variable names for your rows.
answered Apr 12, 2019 by Gold (36,215 points)
Sorry mate. You slipped in just before.

At least we're on the same track!
Thank you very much for the answers!! I just tried what you wrote me and although the variable is created, it doesnt seem to work cause it calculated the average as "0".
If you want to share your script we can see if there is any issue.  Otherwise check the following:
- make sure that you encountered the page the script is on
- check the names of the variables are correct and case sensitive
[% SetValue(IndAverage,(IndScale_r1 + IndScale_r2 + IndScale_r3 + IndScale_r4 + IndScale_r5 + IndScale_r6)/6) %]

I wrote this script, the IndScale_r1... refer to the different rows of the list I have in grid. Maybe has something to do with the fact that the values given to these rows are categorical and hence the script cannot compute the average?
It worked! At first I had included the script at the footer of the same question with the grid thats maybe the reason it didnt work. The only thing I realized is that although I didnt not include the ROUND function, the average is rounded up. I calculated myself the value and it should be 2,8 but in the variable the value is 3. Is there a way to fix this?
Place this script we provided on a page after the grid question. Let the grid be answered first.
Use this script ...

[%SetValue(GridAverage,ROUND((Q1+Q2+Q3+Q4+Q5)/5),1)%]

This will round your average to 1 decimal point.

You won't need to go beyond one decimal point if you are dividing by 5.
Hmmm, I just tested and it worked.  So your grid question is named IndScale correct?  I set up a row based grid question called IndScale with 6 rows and 5 columns and pasted your script in and showed that variable on text screen after that and it gives an average.  I'd be happy to look at your program if you want to send the .ssi file to me.
Glad you got it working.  Yes you always need that script on a page AFTER the questions as they haven't been submitted and don't have any values yet (it looks at the script when the page loads).  Change your pass in variable to a decimal number instead of whole number and that should do the trick.
I forgot about the decimal at the pass-in fields?

Now we have 2 solutions.

Thanks Jay.
Thank you both so much for replying so fast.

I wrote the script exactly how you've written it Paul,

[% SetValue(IndAverage,Round((IndScale_r1 + IndScale_r2 + IndScale_r3 + IndScale_r4 + IndScale_r5 + IndScale_r6)/6),1)%]
But it gives me a wrong message :
The function call "SetValue" contains an invalid number of arguments or an argument with an incorrect type.

Can you see a mistake in my script because I think is the same with yours?
I believe it needs to be:

[% SetValue(IndAverage,Round((IndScale_r1 + IndScale_r2 + IndScale_r3 + IndScale_r4 + IndScale_r5 + IndScale_r6)/6,1))%]

The ,1 should be inside the ROUND() tag.
Thanks Jay for correcting my syntax.
It worked!! Thank you both so much
You can use the SetValue function which caters for this very need.

Define a variable called GridAverage in the Pass-In fields and define it as a whole number.

Pop this script into the footer of an appropriate question (one that is not skipped).

[%SetValue(GridAverage,(Q1+Q2+Q3+Q4+Q5)/5)%]

Replace the above example questions with you question numbers.

If you want to get a little fancy, you can use the ROUND function to clean up the decimal places.

ROUND (NUMBER,#DECIMALS)
answered Apr 12, 2019 by Platinum (82,475 points)