- Posts: 6
- Thank you received: 1
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
- Topic Author
- Offline
- New Member
Less
More
3 years 6 months ago #206820
by Capitole
Using equation to create values to be used for tailoring questions was created 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).
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 <, '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
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).
cvm | value |
(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 <, '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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10099
- Thank you received: 3589
3 years 6 months ago #206825
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using equation to create values to be used for tailoring questions
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.
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
- Topic Author
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 1
3 years 6 months ago #206829
by Capitole
Replied by Capitole on topic Using equation to create values to be used for tailoring questions
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
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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11639
- Thank you received: 2737
3 years 6 months ago #206857
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic Using equation to create values to be used for tailoring questions
In my opinion your equation is wrong. You are not using the "if" clause correctly.
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
I hope I have added enought ))) at the end...
The other option is a bit easier to read:
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...
Code:
{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')}
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 && 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 hope I have added enought ))) at the end...
The other option is a bit easier to read:
Code:
{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')}
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
- Away
- LimeSurvey Community Team
Less
More
- Posts: 12886
- Thank you received: 3965
3 years 5 months ago #206864
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Using equation to create values to be used for tailoring questions
And you can simplify the equation to
{if(cvm <, '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
{if(cvm <, '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
- Topic Author
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 1
3 years 5 months ago #206866
by Capitole
Replied by Capitole on topic Using equation to create values to be used for tailoring questions
Thanks Holch!
I used your first suggestion and it worked. My bad not understanding the if in LimeSurvey.
Thanks again!
Henrik
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
- Topic Author
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 1
3 years 5 months ago #206867
by Capitole
Replied by Capitole on topic Using equation to create values to be used for tailoring questions
Thanks Joffm,
that indeed makes the coding more efficient.
Henrik
that indeed makes the coding more efficient.
Henrik
The topic has been locked.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11639
- Thank you received: 2737
3 years 5 months ago #206868
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic Using equation to create values to be used for tailoring questions
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.