Have an idea?

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

Look up value based on zip code

I need to pipe in a numeric value based on a zip code that the respondent types in. The problem is that I have ~33K zip codes, and the corresponding numeric values (distance in miles from a certain location) is different for each zip code.

I would like to ask an open-end zip code question, and then insert a text question in which I pipe in the numeric value corresponding to the zip code respondents type in on the first question. These two questions would be separated by a page break.

In Excel terms, this is basically a VLOOKUP, but I don't how to do this  in Sawtooth.

Any suggestions?
asked Jun 11, 2021 by ALegend (140 points)

1 Answer

0 votes
I can point you to a post from 2018 that Zachary kindly assisted with ...


This is similar to what you are seeking.

The Perl Script contains hashes - which are paired variables (e.g. zip code and distance from a certain location). From this script, you would just need something like this to insert in to your question script ...
The distance is: [%Begin Unverified Perl
my $zipCodeQuestion = 'ZipCodeQ';
my %zipCodes = (
    12345 => 1,
    23456 => 1,
    34567 => 2,
    45678 => 2,
    56789 => 3
return $zipCodes{GETVALUE($zipCodeQuestion)};
End Unverified%]

Note I have removed the "exists" function as this is just checking to see if the zip code exists within the array (it returns a 1 if true and a 0 if false). As Zachary has mentioned, replace 'ZipCodeQ' with your zip code question, and also insert (using a suitable text editor), all the zip codes with their corresponding distances in the format of zip code => distance.

One matter I cannot determine is the speed of this on your server. The hash array approach is meant to be quick, but I can't say for sure how well performed this will be for your situation on your server. Zachary does mention this as a warning to test the speed / performance.

May I suggest you test this approach out with maybe 50-100 zip codes, to ensure it is working. Once you are confident it works, add your full list of zip codes in to the script and test the speed / performance.

If you feel the speed / performance is lacking, I can only suggest you reach out to Sawtooth Software directly at Support@SawtoothSoftware.com for an alternative.
answered Jun 12, 2021 by Paul Moon Platinum (99,385 points)
edited Jun 12, 2021 by Paul Moon