Have an idea?

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

See if a constructed list contains an item in free format question

Hi all,

I wonder if there is a way to do the following: I have a predefined list of combinations in a free format question, and in each question of the questionnaire I randomly choose and use one of them. The problem is, the combinations cannot be repeated, so I 'll have to check if they have been already used along the questions. What I'm thinking of doing is creating a constructed list and, if the combination isn't already in it, add it to that list, and then use it. So, each time I randomly pick up a combination, I would check if the constructed list already has it, and if it hasn't, use it and add it to the constructed list. The problem is that, after looking at the SSI Web Documentation, I can't seem to find a function that allows me to see if an item is present in a list or not.

I hope I'm putting this clear. Also, perhaps there is a much more easy and straight way to do this, but I'm not seeing how. Does anybody know how can I achieve this?

asked Apr 27, 2012 by João Fernandes Bronze (2,040 points)
retagged Sep 13, 2012 by Walter Williams
I would create the combinations in a constructed list to begin with. Remember, a constructed list removes duplicates if selected more than once. Not sure how many combinations you require but this method will work. You have a functions called ListLabel and ListValue that could come in handy here.
Thanks, Paul. From what I understood, I think I went your way. Though, I think I just didn't understand this one -> "Remember, a constructed list removes duplicates if selected more than once."

I'll post the full solution hereinafter as an answer.
Here's some simple rules to follow from the Sawtooth help (look up list building). Note point 4 which is what I was trying to say earlier.

Rules Governing List Operations

 1. All constructed lists are initially empty.  
 2. All members added to a constructed list must come from a common parent list.  
 3. You may add members from another constructed list to a constructed list, as long as both constructed lists share the same parent predefined list.  
 4. A member can only appear once in the constructed list (if you try to add a member after it already exists on a constructed list, it will not be added again).  
 5. List building commands are executed sequentially.  
 6. A question using an empty constructed list (as response options) is skipped.  

When you view questions involving constructed lists in Preview mode, all members from the parent list are displayed (since we don't know how the respondent has answered previous questions).
This is the kind of problem that appear when you're searching things in the documentation almost based only in CTRL + F :] Thanks for the enlightenment, Paul!

2 Answers

+1 vote
I'd suggest an alteration, don't create one item at a time. Rather create your random list with as many items as you have questions. Then call 1st item in 1st question, 2nd item in 2nd question and so on
answered Apr 27, 2012 by Bahadir Ozkurt Gold (16,980 points)
Couldn't you use ListHasParentMember for what you are trying to do?
That's a good idea, Bahadir. :] Although, if I want 10 items, I will have to still check if that item has been previously chosen (or not), so the scenario remains, doesn't? Of course that I can be missing something!
If I understand correctly, your situation is as follows:
you have a list of n items and then you have a constructed list selecting one item from this n items at random. You ask this item in a question and move on to a new question to get a new random, right?

Now I proposed to select your random items like this, assuming you have 7 questions to ask:

Now you have a list of 7 non-repeating items randomly chosen. Now in your 1st question use this:
2nd question --> Add(Randomlist,2)
this way you can achieve what I perceive you want to achieve :)
Now I got it :] I wasn't fully understanding the ADD function, I thought I had to explicitly indicate to which list I wanted to add the items, instead of knowing that it adds them to the current list, where the function is being used (duh!).

I'll post now a full solution as an answer, since my case is a little bit more complex.
I think yes, Jay. Thanks for your answer! I already had a look at the LISTHASPARENTMEMBER function, but I didn't got it fully. With your answer, I took another look and got it. Tough, I used another path, which I think is more straightforward (I'll post the solution as an answer hereinafter).
+1 vote
So, thank you all for your kind answers.

To summarize, in my case I have 2 predefined lists: one containing the items and another containing the possible combinations. My list of items is something like:

Item 1
Item 2
Item 3
Item 4

My combinations list has the combinations of items possible. It goes like this:

1 2
1 3
1 4
2 3
2 4
3 4

So, I created a general "RandomQuestions" constructed list. If I want to display 4 questions in my questionnaire, it would be like this:

Begin Unverified Perl
    my $number_questions = 4;

End Unverified

Then, for each question I created another constructed list. For instance, for question 1, it is:

Begin Unverified Perl
    my $tmpString = "";
    my @tmpItems = ();

    $tmpString = LISTLABEL("RandomQuestions", 1); # in the 2nd question it would be LISTLABEL("RandomQuestions", 2), etc
    @tmpItems = split(" ", $tmpString);
End Unverified

Just a final question: although I think this is correctly working, does anybody know a way of debugging this, other than running the questionnaire several times and empirically see if the combinations are being repeated?

Thank you all again!
answered Apr 30, 2012 by João Fernandes Bronze (2,040 points)
Its impossible to get duplicates unless you make a mistake in coding.
One way to test that I can think of is the fillowong;

Use the data generatoe and fill a gew hundred surveys. Che k their data to ease yoir mind
I'll do it. Danke! :]