Have an idea?

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

How do I add the two greatest values entered from a previous question into a new constructed list?

Hello,

I have a question that asks what type of fish do you regularly catch and it is a multiple response select question. Using constructed lists, I pulled the answer if chosen from that question into a grid-style question asking how many times do you regularly catch these fish?

Then, I want to ask follow-up questions about only the top 2 fish they catch, so it is more than just pulling in the AIC from the initial multiple response select question. If they catch 5 different fish, but catfish 10 times and tuna 9 times, but the other 3 types only once, I only want to ask the follow-up questions about catfish and then about tuna. How do I pull the largest value from the grid-style question into the follow-up questions? Then I want to copy and paste those same follow-up questions but change it to the second largest value (tuna).

Please help! Thanks!
asked Dec 27, 2017 by Rick

1 Answer

0 votes
This post should give you what you need.  It constructs a list of values from highest to lowest (which is what you want) using the AddSorted command.  It then further selects an item (randomly) from the top two which would account for any 'ties'.

https://sawtoothsoftware.com/forum/9318/select-top-2-from-a-constant-sum?show=9320#a9320
answered Dec 27, 2017 by Jay Rutherford Gold (26,185 points)
Thanks for this, however is there any that are a bit more explanatory? My apologies I need a bit of dumbed down version as I am more of a Sawtooth novice. My initial questions are in a grid, not a constant sum format, so that makes the post a bit harder for me to utilize in my lists. Thanks!
Sorry about that Rick.  Same principal applies since it's a grid question collecting a numeric response.  In the script below I am assuming it is a column based grid in which the fish they catch is put in a constructed list as the rows and the column is set as numeric.  With that, each fish has a response entered for how many they catch.  So, let's assume your grid is set up as I described and is named Q1.

This follows the same instructions as the link I provided, but I adjusted the code for a grid question.

Q1SortedList
AddSorted(Q1_c1,0)


In the script below the '10' is assuming up to 10 items in your list.  You can replace that number with how many total fish items are in your parent list.
Q1High1List
Begin Unverified Perl
    
my $i=1;
    
for($i=1; $i<=10; $i++)
 {
  if (VALUE("Q1_r".$i."_c1")==VALUE("Q1_r".LISTVALUE("Q1SortedList",1)."_c1"))
   {  
    ADD("Q1List",$i);
   }  
 }
  
RANDOMIZE();
 
End Unverified


Q1High2List
Begin Unverified Perl
    
my $i=1;
    
for($i=1; $i<=10; $i++)
 {
  if (VALUE("Q1_r".$i."_c1")==VALUE("Q1_r".LISTVALUE("Q1SortedList",2)."_c1"))
   {  
    ADD("Q1List",$i);
   }  
 }
  
RANDOMIZE();
 
End Unverified


Q1Top2List
ADD(Q1High1List)
ADD(Q1High2List)
ListMax(2)


Q1Top2List would be what you would use in the followup questions and will have 2 items that gave the largest numbers in Q1.  If you had 3 or more items with the same 'high' number, then this script would have randomly selected 2 of them instead of just taking the first two in the list.  With the explanations Paul provided in the other post and this modified script, I hope it gets you what you are looking for.  If you have problems or need more detail on a specific point please let us know and provide any necessary info (such as question numbers, etc.)
I forgot to point out that where this script references 'Q1List' you would use the name of whatever your parent list is.
Thanks so much. You assumed correctly as to the design. Sorry, but taking an even further step back where do I enter this code? In the constructed list instructions?
That is correct, these are all constructed list instructions.  All of these constructed lists should be using the same Parent list.
Thanks Jay. I'm getting there thanks to your patience and repeated assistance! I am able to pipe in responses from the grid into future questions, however there must be an issue because it doesn't seem to follow the desired logic.

When I ask the follow up question including the code "[%ListLabel(Q1High1List, 1)%]" to get the top response from the grid to appear, it seems to be more random. It is pulling in a row from the grid, but not the one with the highest numeric value. Please see below the Q1High1List code I used (9 items in my list, DyeProcedures is the numeric grid question with responses piped in via the AIC constructed list from a previous multi-select question which has the parent list DocsNursesProceduresL1):

Begin Unverified Perl
     
my $i=1;
     
for($i=1; $i<=9; $i++)
 {
  if (VALUE("DyeProcedures_c".$i."_r1")==VALUE("DyeProcedures_c".LISTVALUE("Q1SortedList",1)."_r1"))
   {  
    ADD("DocsNursesProceduresL1",$i);
   }  
 }
   
RANDOMIZE();
  
End Unverified
Sorry Rick, I was out of the office yesterday so only seeing your post now.  One thing right off is that you have reversed the row and column so I am assuming your grid is row based as a number with only one row and the items from the previous multiple select are your columns (where my example was the opposite where it was a column based grid and the items were the rows).  If that is correct did you also change the logic of the Q1Sorted List to refer to the row instead of the column?  So the problem may lie somewhere in there.  If you still are stuck you are also welcome to email me a sample of what you have and I can take a look.  My email is jruth@isgmn.com
Thanks Jay! I just sent you an e-mail with a sample as requested.
It looks like one problem is the parent list you are using.  I sent you an email back.
...