Have an idea?

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

perform simple algebra on a variable

Hi,

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
Regards;
AMYN
asked Apr 18 by AMYN Bronze (1,470 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).
[%SetValue(Q1NEW,((Q1-10)/10))%]

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 (77,000 points)
Thank you for your prompt reply,
I have just tried the code you shared in the question (so I added it to Q1) then just to test that the code is working properly I requested the value of Q1 and Q1NEW in the next question to be displayed on the question page (call it Q2) but unfortunately only the value of Q1 appeared and noting for Q1NEW, so I assume the equation was not executed and the value was not passed to the variable?!!
The Q1NEW variable script I provided needs to be on the next page or a future page.

Otherwise you will need to write some JavaScript if you want it on the same page
Thank you for your explanation.

I have tried your suggestion and it worked yet I have a further request.

Since I am going to use this script on a lot of questions, is it possible to incorporate the [% QuestionName ()%] function to automatically get the question name instead of just manually rewriting the name of the question every time?

It is not a must to have the value calculated on the same page, but if the JS written on the same page would make it easier to copy and past in other questions then could someone suggest the appropriate code?

Regards'
You can certainly use the system function QuestionName() in any of your questions to display the question name without manually typing it out.

Insert [%QuestionName()%] to display the question name.

The SSI Script I gave you to display your calculated variable is slightly less work than the JavaScript version. Either way, they do the same job and they are just as easy to copy / paste in selected questions.
I tried but it seems that I can't put a system function within another system function.

I will manually write them if I did not find any other solutions, thanks.
You should not use [%%] within another set of [%%].

All your calculations and script need to be in one set of [%%] or multiple sets of [%%], but no overlapping of them.

You haven't mentioned what other task you are trying to do. If you pass the details on, I might be able to assist you?
Thank you very much for offering to help and the following I hope would give a wider picture of my current situation.

 I have several "bundles" of questions were every bundle composed of 3 questions, the respondent will always answer the first question then based on the answer he will answer either question 2 or 3 in the bundle.

for question 2 I want to calculate the result by just dividing the answer by 10, and the name of the new variable will be Q_R, the name of the old variable is Q_ans. I tried to do that using the following script:


[% SetValue(QuestionName()_R,(QuestionName()_ans/10)) %]



but it returns an error message the "_" is an invalid character

in question 3, I want to calculate it as a negative difference from 10 then divide it by 10, as in the original question. The name of the new variable is also Q_R & the original is Q_ans; tried the following script :


[% SetValue(QuestionName()_R,((QuestionName()_ans-10)/10)) %]



the same error message.

I found that if there was no response from the respondent, a 0 value is stored and there is a result for the new variable, but then I can just ignore these results as I know the range of possible results, 0 is not one of them.

In the end, I wanted to say that not all bundles are answered by all respondents and some bundles are answered by all respondents, I believe again the unanswered once will just give me the "out-of-range" values which I can later exclude from the analysis.

thank you very much
The problem with using this SetValue solution with the QuestionName() function is that you are placing this script on a page after the question, so when running the script say on the next page, the question name has changed away from the question name you want to calculate on.

So I understand correctly, you want to write one script that caters for all questions you need to perform the same calculation on? Is that correct?

Please confirm. I'll try to come up with a solution a little later.

Thanks.
Yes you are right, how did I miss this point about the script.

Yes I want either a "universal" script that I can use for all the questions by copying and pasting multiple times or better a script that can recognize which questions where answered and calculate the view variables just for them.

Appreciate your help
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 ...
<script>
$(document).ready(function(){
    SSI_CustomGraphicalRadiobox();
})

function SSI_CustomGraphicalRadiobox() {
    var ov = SSI_GetValue('[%QuestionName()%]');
    var nv = ov - 10;
     
   $('.UpdatedVar').text(nv);
}
</script>

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 :
                                  SSI_CustomGraphicalRadiobox()
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/
...