Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Using equation to create values to be used for tailoring questions

  • Capitole
  • Capitole's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #206820 by Capitole
Hi,

I am creating a survey with 18 different subsamples. I randomly allocate respondents using the following equation in an early question group (with question code = cvm),

{rand(1,18)}

The different subsamples will be asked to answer yes or no to a question where values will differ. To simplify, here I exemplify with 6 values according to below (cvm(1,3) corresponds to samples were cvm equal 1, 2, and 3).

cvmvalue
(1,3)35
(4,6)50
(7,9)68
(10,12)10
(13,15)14
(16,18)18


I tried to use the equation command (after having read about equations in the LimeSurvey manual ) and included in the following equation in a question group after the one that included "cvm" (with question code = cvmdp),

{if(cvm >= 1 && cvm <=3, '35'),if(cvm >= 4 && cvm <=6, '50'),if(cvm >= 7 && cvm <=9, '68'),if(cvm >= 10 && cvm <=12, '10'),if(cvm >= 13 && cvm <=15, '14'),if(cvm >= 16 && cvm <=18, '18')}

I then used the tailored question approach by calling on the value in a later question (and later question group than cvmdp) by including {cvmdp} in that question. The space remains empty, though. I would be very grateful for any help on my coding, or hints on how to call on values in questions.

I have used the tailored question approach to call on answers in the survey with success, so that part should work.

Thank you in advance,

Henrik
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #206825 by tpartner
What LimeSurvey version?

Do you see any errors in the logic file?

What do you see if you leave cvmdp visible?

You should put a test in cvm to check if it's already set. This will prevent resetting on page submission.

Code:
{if(is_empty(cvm), rand(1,18), cvm)}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Capitole
  • Capitole's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #206829 by Capitole
I am using Version 3.24.2.

I do not see any errors in the logic file. Both for cvmdp and for the question where I call upon {cvmdp}.

Leaving cvmdp results in an empty question in the question group where cvmpd is include, but the space is still empty in the question were I try to call upon {cvmdp}.

Regarding the test of cvm. Should I do that in the cvm question itself, or in the equation containing cvm in cvmdp question?

Thanks

Henrik
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #206857 by holch
In my opinion your equation is wrong. You are not using the "if" clause correctly.
Code:
{if(cvm >= 1 &amp;&amp; cvm <=3, '35'),if(cvm >= 4 &amp;&amp; cvm <=6, '50'),if(cvm >= 7 &amp;&amp; cvm <=9, '68'),if(cvm >= 10 &amp;&amp; cvm <=12, '10'),if(cvm >= 13 &amp;&amp; cvm <=15, '14'),if(cvm >= 16 &amp;&amp; cvm <=18, '18')}

in Limesurvey if works like this:
{if(condition,then,else)}

So either you create one equation for each case or you need to nest the if conditions. the second if needs to be within the "else" of the first one. You can not just add one if after the other in the same {}

But what could work would be something like this:
Aproach ine
Code:
{if(cvm >= 1 &amp;&amp; cvm <=3, '35', if(cvm >= 4 &amp;&amp; cvm <=6, '50', if(cvm >= 7 &amp;&amp; cvm <=9, '68', if(cvm >= 10 &amp;&amp; cvm <=12, '10', if(cvm >= 13 &amp;&amp; cvm <=15, '14', if(cvm >= 16 &amp;&amp; cvm <=18, '18'))))))}

I hope I have added enought ))) at the end... ;-)

The other option is a bit easier to read:
Code:
{if(cvm >= 1 &amp;&amp; cvm <=3, '35')}
{if(cvm >= 4 &amp;&amp; cvm <=6, '50')}
{if(cvm >= 7 &amp;&amp; cvm <=9, '68')}
{if(cvm >= 10 &amp;&amp; cvm <=12, '10')}
{if(cvm >= 13 &amp;&amp; cvm <=15, '14')}
{if(cvm >= 16 &amp;&amp; cvm <=18, '18')}

Not sure if you need to add an empty "else" for each. But I think in the newer versions this is not required anymore.

What worries me a bit, that Tpartner didn't see this. Either he really didn't spot it or I am not up to date... ;-)

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The following user(s) said Thank You: DenisChenu
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #206864 by Joffm
And you can simplify the equation to
{if(cvm <=3, '35',if(cvm <=6, '50',if(cvm <=9, '68',if(cvm <=12, '10',if(cvm <=15, '14','18')))))}
Because of the syntax of the IF-statement
{if(condition,then,else)}
it is not necessary to check again the already checked parts.

If the first condition fails, the value of cvm IS greater or equal 4; so nop need to check again.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: holch
The topic has been locked.
  • Capitole
  • Capitole's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 5 months ago #206866 by Capitole
Thanks Holch!

I used your first suggestion and it worked. My bad not understanding the if in LimeSurvey.

Thanks again!

Henrik
The topic has been locked.
  • Capitole
  • Capitole's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 5 months ago #206867 by Capitole
Thanks Joffm,

that indeed makes the coding more efficient.

Henrik
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #206868 by holch
of course Joffm's version is the cleaner and simpler one. I would go with his approach.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose