A subset of a Multiple response Category

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
lmoriba
Posts: 56
Joined: June 10th, 2018, 6:21 pm

A subset of a Multiple response Category

Post by lmoriba »

Dear CSPRo Community,

Can any one help to find a solution to this problem, I have two multiple response category of questions, so that the items picked/selected in the first category is exactly the same item list(s) I want to appear in the second category. From the second category I can then choose or select some or all response categories already selected in the first one. Is like the second one is a subset of the first one in that only items checked in the first one will be the only items to be selected or checked in the second category.

Attached is the sample am trying to work on.

2Multiple response categories.zip
(7.14 KiB) Downloaded 237 times
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: A subset of a Multiple response Category

Post by josh »

To change the responses for the second multiple response question you will need to use setvalueset() and pass it two arrays: an array of codes ("A", "B", "C"...) and an array of labels ("Manager", "Professional",...). You only want to put the codes and labels that were picked in the first question into the codes and labels arrays you pass to setvalueset for the second question. This is a technique called "dynamic value sets". You can see more info and examples on dynamic value sets starting on page 6 of this document: http://teleyah.com/cspro/DCJune2015/04-CAPI/04-CAPI.pdf including a similar example with a dynamic check box.

First, you need to declare the arrays of codes and labels in your PROC GLOBAL. The arrays need to be long enough to hold all the values in your value set plus one to hold an additional blank row. In your application the arrays would be length 12 since you have 11 items in your value set. There is no harm in making them longer. If I have an application where I use setvalueset in multiple questions I will make these arrays length 100 which is usually big enough for any value set I have.
PROC GLOBAL
array string labels(100);
array string codes(100);
You want to call setvaleuset from the onfocus proc of the item whose value set you are modifying rather than preproc or postproc. Onfocus is called just before the responses are displayed for a question when moving either forwards or backwards into the question. There you need to check each code from the previous question to see if it was selected. If a code was selected, add the code to the array of codes and add the label corresponding to that code from the original value set to the array of labels. You can get the label for a code using the function getlabel().
PROC EM15
onfocus

// Create value set from only options selected in previous question

// Variable to hold position in array in which to insert next item
numeric nextEntryValueSet = 1;

// If "A" is selected in previous question, add it to arrays for value set
if pos("A", EM14) > 0 then
    codes(nextEntryValueSet) =
"A";
    labels(nextEntryValueSet) =
getlabel(EM14_VS1, "A");
    nextEntryValueSet = nextEntryValueSet +
1;
endif;

// If "B" is selected in previous question, add it to arrays for value set
if pos("B", EM14) > 0 then
    codes(nextEntryValueSet) =
"B";
    labels(nextEntryValueSet) =
getlabel(EM14_VS1, "B");
    nextEntryValueSet = nextEntryValueSet +
1;
endif;

// If "C" is selected in previous question, add it to arrays for value set
if pos("C", EM14) > 0 then
    codes(nextEntryValueSet) =
"C";
    labels(nextEntryValueSet) =
getlabel(EM14_VS1, "C");
    nextEntryValueSet = nextEntryValueSet +
1;
endif;

// Do same for "D", "E", "F", ....

// Mark end of array with a blank code
codes(nextEntryValueSet) = "";

// Update the value set
setvalueset(EM15, codes, labels);
To do this with a bit less code you can use a loop instead of 11 separate if then statements. The tricky part about using a loop is that you will need to loop from 1 to 11 but the codes are the letters "A","B","C"... so you need to convert from a number to the corresponding letter in the alphabet. You can do this using a string of the characters in the alphabet "ABCDEFGHIJK" and the [] operator which lets you extract a substring at a position in the array.
PROC EM15
onfocus

// Create value set from only options selected in previous question

// Variable to hold position in array in which to insert next item
numeric nextEntryValueSet = 1;

// Used to convert from number (1,2,3...) to corresponding letter ("A", "B", "C"...)
string numbersToLetters = "ABCDEFGHIJK";

// Loop through codes and add the selected ones to value set
do numeric i = 1 while i <= 11

    
// Get letter code ("A","B","C"...) from number i.
    // the [] get the substring at position 1 of length 1 from the string "ABCDEFGHIJK"
    // First time through loop i will be 1 so codeLetter will be "A"
    // Second time through loop i will be 2 so codeLetter will be "B"
    // Third time through loop i will be 3 so codeLetter will be "C"...
    string codeLetter = numbersToLetters[i:1];
    
    
if pos(codeLetter, EM14) > 0 then
        codes(nextEntryValueSet) = codeLetter;
        labels(nextEntryValueSet) =
getlabel(EM14_VS1, codeLetter);
        nextEntryValueSet = nextEntryValueSet +
1;
    
endif;
enddo;

// Mark end of array with a blank code
codes(nextEntryValueSet) = "";

// Update the value set
setvalueset(EM15, codes, labels);
lmoriba
Posts: 56
Joined: June 10th, 2018, 6:21 pm

Re: A subset of a Multiple response Category

Post by lmoriba »

Dear Josh,

Many thanks, indeed, this is exactly the solution, I was looking for. Thanks for all the wonderful support.
abass
Posts: 12
Joined: December 4th, 2018, 6:45 pm

Re: A subset of a Multiple response Category

Post by abass »

Hi, precisely I would be very happy to have a help, an example, if possible a tutorial on how multiselection works, that is to say, to turn the checkboxes into YES / NO!
cordially!!!!!!!
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: A subset of a Multiple response Category

Post by Gregory Martin »

Take a look at the discussion here: http://www.csprousers.org/forum/viewtop ... f=1&t=2114

Search the referenced PDF, http://teleyah.com/cspro/DCJune2015/04-CAPI/04-CAPI.pdf, for "Converting checkboxes to yes/no."
Post Reply