Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Use the result of one variable into another in the same page

Gentlemen, good afternoon.

Currently using Sawtooth Software, version 8.4.8.

I have two variables in a Free Format page. One of them captures the sum of responses on different pages (say Page_Sum) and the other is supposed to categorize the result of Page_Sum (say Page_SumCategory). How can I use the updated results from Page_Sum into Page_SumCategory? How hard is JavaScript coding in this case?

Thanks a lot for your time!


asked Aug 28, 2019 by Mauricio Belleza Bronze (860 points)
What do you mean by "to categorize the result"?  What is Page_Sum and Page_SumCategory and how do you want them to interact?

Page_Sum, for example, is the result of the sum of response values in a previous page, say SocioEconomicClass. Page_SumCategory is the categorization of Page_Sum results (for example, the range between 0 and 20 is 'Category 1', between 21 and 40 is 'Category 2', and so on...).
Does this description help?

Thanks again for your attention!
Are either of these questions the respondent actually sees?  It sounds like these are just calculations that need to be ran behind-the-scenes based on responses to other questions.

Page_Sum is not seen, it is a hidden input field, which is a "behind-the-scenes" calculation. However, the fieldwork provider, who will run the survey, will need to see the result from Page_SumCategory. I tried putting them in the same page, but Page_SumCategory does not update according to the results from Page_Sum, that's the point.

1 Answer

0 votes
Best answer
While it is possible to have fields on the same page interact with each other using JavaScript, I don't think this is necessary for the behavior you are after.

Instead of fields in a free format question, you could add these two variables as pass-in fields to your questionnaire.  Then, on the page where you want to run the calculations and display the category, place code like this:

[% Begin Unverified Perl
my $sum = GETVALUE('Q1_1') + GETVALUE('Q1_2') + GETVALUE('Q1_3');
SETVALUE('pif1', $sum);

my $category;
if ($sum <= 20) {
    $category = 1;
elsif ($sum <= 40) {
    $category = 2;
SETVALUE('pif2', $category);

return $category;
End Unverified %]

"pif1" and "pif2" must be replaced with the names of your two pass-in fields.  Line 2 can be updated to reflect the names of the fields you want to sum together.  Lines 7 through 12 can be updated and repeated to reflect the rules of your categorization.

Does that suit your needs?
answered Aug 28, 2019 by Zachary Platinum Sawtooth Software, Inc. (205,575 points)
selected Aug 28, 2019 by Mauricio Belleza

I have tested this alternative, and it has worked nicely! Thanks a lot for the patience!

Best Regards,