Have an idea?

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

How to group the age internally?

Hi Team,

I am creating a survey in which the respondents age needs to captured as numeric and group them in the background. so I want to have a hidden variable to group the age under different age group categories, say for example, we have the below categories
Age 18-25
Age 26-40
Age 41 and above

I the respondent's age is 32 then has to be classified under group 2. please check and let me know the possibilities.

As I am new to this platform please explain detail.

asked Jun 11, 2019 by raaamuz (360 points)

1 Answer

+1 vote
You can create a Pass-In variable (Questionnaire Access and Passwords -> Pass-In Fields).  Let's say you name it AgeCategory.  I'm going to assume your open numeric age question is called 'Age' for the example below.  After your age question, on a  question that all respondents will hit, add this script in the footer:

[% Begin Unverified Perl
if (VALUE("Age")<26)
{SETVALUE("AgeCategory", 1)}
elsif (VALUE("Age")>25 && VALUE("Age")<41)
{SETVALUE("AgeCategory", 2)}
elsif (VALUE("Age")>40)
{SETVALUE("AgeCategory", 3)}
End Unverified %]

This will set AgeCategory to a 1, 2 or 3 based on the response to Age.
answered Jun 11, 2019 by Jay Rutherford Gold (36,215 points)
Is it possible to add labels to that pass-in field (AgeCategory) Where 1 is "18-25 years", 2 is "26-40 years" and 3 is "Above 40 years"?
No, you can't add labels there.  However, another option would be to create a select question with those 3 categories and add a preskip to always skip that question.  Then use the same script but setting the value for the name of your select question instead of using a pass-in variable.  Works the same and I've done it on many projects.
Is it possible to display a message, like message box in excel?
I'm not sure if I understand what you are asking.  If you are referring to having a reference as to what the codes mean for a pass-in variable, there is a comments column and you make notes there.
I want to display messages for debugging purpose, say for example I want to display a welcome message in the survey like, "Hello gentleman" when the gender is 'Male', hope this is possible, please check and confirm.
You can use unverified perl to return specific text based on responses to other questions.  Similar to the script I provided.  For example, if you had a question called 'Gender' you could use this script later in the survey to insert the word man or woman:
[% Begin Unverified Perl
if (VALUE("Gender")==1)
{return "man";}
elsif (VALUE("Gender")==2)
{return "woman";}
End Unverified %]
Thanks a lot, this is working fine.