Have an idea?

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

Hidden variable to assign states to regions

I have a single select type variable S1 with values 1-51 coded for each state (including DC).  I want to create a hidden variable called region that groups the 51 states into one of 4 regions.  I've tried multiple ways to do this, but I don't have a good grasp of either pass-in fields or the use of free format variables to do this.  Can anyone guide me on the best way to approach?
asked Dec 12, 2019 by dpatek (290 points)

1 Answer

+1 vote
We can solve this by defining a pass-in field and using the SetValue SSI Script function.

Start by going to Access | Pass-In Fields. Define your "Region" variable here and make it a whole number.

Now pop this Perl script into a question footer where that question is not skipped. Select a question beyond S1.
[%Begin Unverified Perl 

 if (VALUE("S1")==1234 || VALUE("S1")==4321)
  {
   SETVALUE("Region",1);  
  }

 elsif (VALUE("S1")==5678 || VALUE("S1")==8765)
  {
   SETVALUE("Region",2);  
  }

 elsif (VALUE("S1")==6789 || VALUE("S1")==9876)
  {
   SETVALUE("Region",3);  
  }

 else
  {
   SETVALUE("Region",4);  
  }

End Unverified%]

Now you have a variable called Region that constructs 4 separate regions based on your conditions at S1.

Change the conditions in my example for each region and you're done. Note the all responses not coded in Regions 1-3 will be coded as Region=4. That should help lighten the scripting.
answered Dec 12, 2019 by Paul Moon Platinum (84,255 points)
edited Dec 12, 2019 by Paul Moon
Thank you so much for your help.  I created a pass-in field called region, set it as a whole number, and then entered the following in the footer on the next question.  However, I keep getting an error #132:  

Do you see an issue with the way it's coded?  See pasted below.

Thanks, again.


[%Begin Unverified Perl

if (VALUE("S1")==4 || VALUE("S1")==14 || VALUE("S1")==15 || VALUE("S1")==16 || VALUE("S1")==17 || VALUE("S1")==23 || VALUE("S1")==24 || VALUE("S1")==26 || VALUE("S1")==28 || VALUE("S1")==35 || VALUE("S1")==36 || VALUE("S1")==37 || VALUE("S1")==42 || VALUE("S1")==44 || VALUE("S1")==50)
 {
   SETVALUE("Region",1);  
  }

elsif (VALUE("S1")==7 || VALUE("S1")==8 || VALUE("S1")==9 || VALUE("S1")==20 || VALUE("S1")==21 || VALUE("S1")==22 || VALUE("S1")==30 || VALUE("S1")==31 || VALUE("S1")==33 || VALUE("S1")==39 || VALUE("S1")==40 || VALUE("S1")==46 || VALUE("S1")==47 || VALUE("S1")==49)
  {
   SETVALUE("Region",2);  
  }

elsif (VALUE("S1")==1 || VALUE("S1")==10 || VALUE("S1")==11 || VALUE("S1")==18 || VALUE("S1")==19 || VALUE("S1")==25 ||
VALUE("S1")==34 || VALUE("S1")==41 || VALUE("S1")==43)
  {
   SETVALUE("Region",3);  
  }

 else
  {
   SETVALUE("Region",4);  
  }

End Unverified%]
The Perl Script looks fine regarding syntax.

Is "S1" and "Region" defined exactly as is? Perl is case sensitive and needs to be defined correctly.

So ensure S1 is S1 and Region is Region.
And have you placed this Perl script in a question footer after S1?
Eureka!  The problem was that I used "region" in the pass-in field and Region in the syntax.  As soon as I fixed it, the syntax worked!  Thank you so much, Paul!  I appreciate all of the help you provide everyon on this forum.
Well done. Good to hear you now have it working.

You can use this trick going forward in many more of your surveys.

And I'm just a small part of the help of this wonderful forum.
I just use a quota variable called Region.  If S1 = 1 or S1 = 3 or S1 = 5 Northeast = 1, etc.
That way you can track region in the admin page.
You can use Region for many things including skip logic and in other scripting (Perl, SSI, JavaScript, etc). You can also export it as if it was a captured survey question.
...