Randomly Select 5 Cases from a dynamic valueset

Discussions about CSEntry
Post Reply
Yass
Posts: 103
Joined: November 11th, 2017, 1:26 am

Randomly Select 5 Cases from a dynamic valueset

Post by Yass »

Hi CSPro Team,

I would like tp restrict the number of dynamic value set to 5 from set of eligible cases, I have a listing of HHs that I want to dynamically
show 5 eligible cases out of the many from a lookup in a menu program.

Code: Select all

onfocus

valueset hhVS;

forcase  DVV_DICT where SELECT_CLUSTER=HCLUSTER  and  age  > 18 do
      //   if …..endif                            //Random selection of 5  cases 
   
   hhVS.add(maketext(HHEAD) +  " - "  + strip(ADDRESS),HHNO);

endfor;
setvalueset(SELECTED_HH, hhVS);

Please is it possible to restrict the number of HHs in the codes above or there is a better way to do it. Thanks for the support always.
Gregory Martin
Posts: 1792
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Randomly Select 5 Cases from a dynamic valueset

Post by Gregory Martin »

One thing you could do is add all of the eligible households to the value set and then do:
randomizevs(hhVS);
Then you could look at the first 5 entries in that value set. However, what's important with randomization is that you remember the settings from one run to the next. Once you've selected the five households, you may want to save those selections somewhere so that you don't get a different set of random households the next time you run your program.
Yass
Posts: 103
Joined: November 11th, 2017, 1:26 am

Re: Randomly Select 5 Cases from a dynamic valueset

Post by Yass »

Thanks Greg, Please can you help me with the line of codes in selecting the first 5 cases.

Thanks
Gregory Martin
Posts: 1792
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Randomly Select 5 Cases from a dynamic valueset

Post by Gregory Martin »

This loops through the first 5 (or fewer if there aren't 5) entries:
do numeric counter = 1 while counter <= low(5, hhVS.length())
    // do something with hhVS.codes(counter)
enddo;
Yass
Posts: 103
Joined: November 11th, 2017, 1:26 am

Re: Randomly Select 5 Cases from a dynamic valueset

Post by Yass »

Thanks Greg !!
Post Reply