Have an idea?

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

perform simple algebra on a variable


I am using a free format to collect some data and I am saving the collected answer (one value per question) in a certain variable.
I wanted to perform simple algebra on the value (subtraction from 10 and dividing by 10) and then save it in a new variable to use it in the analysis.
Note that I am displaying the value while the respondent is answering, so I can't perform this equation on the same variable. So I believe I need to save it to a new variable.

1- Is that correct?
2- how is that done?
3-how could I check if the equation was performed correctly?

Thanks a lot
asked Apr 18 by AMYN Bronze (560 points)

1 Answer

0 votes
You can use the SetValue function to save to a new or existing variable.

Let's assume you have a variable called "Q1" and you want to apply the arithmetic you mentioned.

Go to the Pass-In Fields and define a variable as a Decimal Number. Call it "Q1NEW".

Pop this script into an appropriate question footer (one that is not being skipped).

This will take Q1 and minus 10, then divide that answer by 10, and then save it to a new variable called Q1NEW.

Replace Q1 and Q1NEW with your variable names.

You can check within the admin module the values of Q1 and Q1NEW as you are testing the survey.

And it is good practice to keep the original variable and the calculated variable separately.
answered Apr 18 by Paul Moon Platinum (70,315 points)
Ahmed, I reached out to Zachary Anderson at Sawtooth Software for this solution. He is a very generous and helpful forum member and has wonderful skills in the areas of JavaScript, CSS, Perl, HTML, etc.

I've tested this solution and it works nicely on a single response select question. This will work on any question of this type without editing the script. You don't even need to change the question number.

You can change the calculation / arithmetic for the new variable by changing the script. This version will minus 10 from the original variable.

Place this where you want the updated variable to appear ...
Updated Variable: <span class="UpdatedVar"></span>

Now place this JavaScript into the footer of the same question ...

function SSI_CustomGraphicalRadiobox() {
    var ov = SSI_GetValue('[%QuestionName()%]');
    var nv = ov - 10;

If you need any further variations of this, you can reach out via the forum or contact Zachary at zachary@sawtoothsoftware.com.

Hope this gets you sorted out.
thank you very much, Paul & Zachary. Zachary gave me valuable help previously on several problems too and I am very sorry for giving you some hard time thinking about a solution and even reaching out for somebody else for the solution; That is very generous of you.

I see that I did not clearly write the type of question I am using in the free format. The question I am using is a slider question, thus I believe I need to slightly modify :
to the proper function for the slider type questions, am I correct?

2-then this script will save the values in a new variable (updatedVar) or just change the value in the old variable??

and finally, do I need to pass the new variable beforehand and name it UpdatedVar?

Thank you again and have a great day.
If you are using this multiple times. you would have to change the UpdateVar to another name each time (e.g. UpdateVar1, UpdateVar2 or something more appropriate).

I would also contact Zachary to ask him to convert this script to the slider option. I'm sure it is a quick change for someone of his knowledge of JavaScript. He's a good guy who is very helpful.
Thank you for the prompt reply, I have just sent him a private message.
If you are using a custom UI control, you'll have to look into that control's documentation to find a relevant event related to the slider changing state.

If this happens to be jQuery UI slider control, then you probably want to use the change, slide, or stop events: http://api.jqueryui.com/slider/