Have an idea?

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

Using a hidden variable in skip logic


My question is whether I can use a hidden or constructed variable in skip logic. For example:
I have a select question (visible)from which  I would like to group the answers - answers "a" to "d" will be coded in the hidden variable as 1, answers "e" to "g" will be coded as 2, etc.
Will I then be able to use skip logic with the hidden variable? For example, if the hidden variable is "2" skip to Question3.

Where should I use perl to code the hidden variable? Does it need to be after the answers to the visible variable?

Thank you!
asked Sep 8, 2017 by Adam
retagged Sep 8, 2017 by Walter Williams

1 Answer

0 votes
Hidden fields can be used everywhere non-hidden fields can, including skips.  There are two ways hidden fields are often added to surveys: as a pass-in field, or as a hidden variable in a free format question.

For a pass-in field, you can set the value at any point in the survey with Sawtooth Script, but you may need to wait until some questions are answered before setting the pass-in field.  Consider this example:

[% SetValue(Qsum, Q1 + Q2) %]

That code will set the pass-in field "Qsum" to the sum of numeric questions "Q1" and "Q2."  That script will work on any page AFTER Q1 and Q2 - it won't be able to sum up the numbers correctly if it is ran before or on the same page as those two questions.

All of that applies to hidden variables in free formats as well.  If you add a Qsum free format question anywhere in the survey, you can use the same Sawtooth Script as before to set the hidden variable:

[% SetValue(Qsum_sum, Q1 + Q2) %]

The one additional feature offered by hidden variables is that you can set them in the HTML on the page as well as with Sawtooth Script / Perl.  If you were to put the Qsum free format question after Q1 and Q2, you could put this code in the free format:

<input type="hidden" name="Qsum_sum" id="Qsum_sum" value="[% Q1 + Q2 %]"/>

Does that help clear things up?  I'd be happy to explain further or help you code up a specific hidden variable you are working on.
answered Sep 8, 2017 by Zachary Platinum Sawtooth Software, Inc. (151,050 points)
This is extremely helpful, thank you for this.
Just to clarify, a possible solution would be to have Question 1, a page break and then a hidden Question 2 where I would insert the code:
[% if Label(Question1) = "Label 1" OR Label(Question1) = "Label 2"
SetValue(Question2," Label A")
else if Label(Question1) = "Label 3" OR Label(Question1) = "Label 4"
SetValue(Question2," Label b")  
end if %]
And then use skip logic as needed with Question2 after the page with the hidden variable (Question2)

Thank you again!!
You're close.  One thing I forgot to mention was that you probably don't want to run code like this:

[% SetValue(Qsum, Q1 + Q2) %]

on Qsum's page.  The reason is that the Qsum value is already going to be getting set by the Qsum page when the respondent submits the page (that's just normal survey page behavior).  So the value that Qsum is set to by the Sawtooth Script will get overwritten by the normal page behavior when the respondent submits the page.

There are two options I could suggest:

First, if you use a pass-in field rather than a free format question, there's no default page behavior to worry about.  You could make question 2 be a text question and run Sawtooth Script on question 2 to set the pass-in field.

[% Begin Unverified Perl
if (LABEL('Question1') == 'Label 1' || LABEL('Question1') == 'Label 2') {
    SETVALUE('PassInField', 'Label A');
elsif (LABEL('Question1') == 'Label 3' || LABEL('Question1') == 'Label 4') {
    SETVALUE('PassInField', 'Label b');
End Unverified %]

Second, if you want to use a free format question for question 2, set the value with HTML / JavaScript instead of Perl, similar to the example HTML I included above.  This way when the respondent submits the page, the value that is stored from question 2 is the value you want stored.

<input type="hidden" name="Question2_sum" id="Question2_sum" value="[% Begin Unverified Perl
if (LABEL('Question1') == 'Label 1' || LABEL('Question1') == 'Label 2') {
    return 'Label A';
elsif (LABEL('Question1') == 'Label 3' || LABEL('Question1') == 'Label 4') {
    return 'Label b';
End Unverified %]"/>
This is extremely helpful, I used option 1, and it works like a charm!