Creating a multiple response based on the non-selected response

Discussions about CSEntry
Post Reply
sham
Posts: 69
Joined: February 3rd, 2022, 7:30 pm

Creating a multiple response based on the non-selected response

Post by sham »

Hi Family,
I have a variable(Q1) with multiple selection and want to create another variable(Q2) but which should contain only the once that were not selected in Q1. what do I do?

Q1(Select multiple)
A. YAM
B. Cassava...............selected
C. Maize,.................selected
D.. Millet
E. NONE
So I want in Q2 to have a preloaded options for those that were not selected in Q1
I tried all the magic and went through documentation but still not getting it.
Q2(Multiple select)
A. YAM
B. Millet


I will appreciate your support family.
justinlakier
Posts: 152
Joined: November 21st, 2022, 4:41 pm

Re: Creating a multiple response based on the non-selected response

Post by justinlakier »

Hi sham,

For this, you want a dynamic valueset using the valueset object and the valueset.remove function. Start out with Q1 and Q2 having the same valueset. After Q1 is selected, use Q2valueset.remove(value) for each Q1 value, and set that result as Q2's value set. This use of dynamic value sets to remove the previously selected option can be found in the documentation and on the forums, and modified to remove multiple options for multiple choice.

This similar post in the forums includes use of a loop to remove multiple previous values from a valueset:
viewtopic.php?t=3825

Hope that helps,
Justin
sham
Posts: 69
Joined: February 3rd, 2022, 7:30 pm

Re: Creating a multiple response based on the non-selected response

Post by sham »

Hi Justin,
I am most grateful for your attention.
I went through the document and guide but still not getting the results.
I used the logic below;

PROC Q2
preproc

valueset vs = Q1_VS1;

do numeric i = 1 while i < curocc(Q1)
vs.remove(MAINLANGUAGE(i))
enddo;

setvalueset(SECONDARY_LANG, vs);

And getting errors;
ERROR(10): You can only add or assign a value set of the same type (numeric) as vs
ERROR(12): Record, group or multiple item name expected

Like I mentioned earlier on. At Q2 I want to see only options that were not selected in Q1

With your permission will like tp attach the file for your perusal.

AMidu
Attachments
Remove_multi response.zip
(47.18 KiB) Downloaded 80 times
justinlakier
Posts: 152
Joined: November 21st, 2022, 4:41 pm

Re: Creating a multiple response based on the non-selected response

Post by justinlakier »

Hi Sham,

First, you need to declare the alpha valueset as "valueset string vs", or else it will default to a numeric valueset.
Second, looping through curocc(Q1) doesn't make sense for your specific example. Instead, I changed it to loop through the Q1 string, removing each substring from the valueset.
I changed the Preproc of Q2 to only read the following, and it worked to limit Q2 to what was not selected in Q1:

valueset string vs1 = Q1_VS1;
do numeric i=1 while i<=length(Q1)
vs1.remove(Q1[i:1]);
enddo;
setvalueset(Q2, vs1);

You can modify this as needed for your own valuesets, such as using i<length instead of <= if you want the final option g, None, to be selectable twice.
Hope that helps,
Justin
sham
Posts: 69
Joined: February 3rd, 2022, 7:30 pm

Re: Creating a multiple response based on the non-selected response

Post by sham »

Hi Justin,
I am soo gratiful for your swift feedback.
It is working.
In fact that was going to be my last question. I had wanted to asked "what if I want to maintain the None? but your guide actually help. I came out with a logic to deal with the none and will like to share if its ok.

valueset string vs1 = Q1_VS1;
do numeric i=1 while i<=length(Q1)
if not pos("G", Q1)=1 then
vs1.remove(Q1[i:1]);
endif;
enddo;
setvalueset(Q2, vs1);

sham. it is working well.

I am most grateful for you and team
Post Reply