Page 1 of 1

Select 15 Cases randomly and write into external file

Posted: October 30th, 2020, 8:46 am
by Yass
Dear Cspro Team,

I have this listing application that , i want assistance on how to randomly select 15 cases into an external file then load it into main application, i have
seen an example on the forum in batch by Greg!

Assistance pls.

Re: Select 15 Cases randomly and write into external file

Posted: October 30th, 2020, 12:42 pm
by aaronw
This snippet will pick two cases at random:
seed(systime());

list all_cases;

forcase QUESTIONNAIRE_DICT do
   
all_cases.add(QUESTIONNAIRE_ID);
endfor;


numeric random_picks = 2;

if random_picks <= length(all_cases) then

    do numeric
i = 1 while i <= random_picks

       
numeric case_count = length(all_cases);
       
numeric index = random(1, case_count);
       
errmsg("Random case selected: %d", all_cases(index));
        all_cases.remove(index);
        case_count = case_count -
1;

   
enddo;

else

    errmsg
("Error: not enough cases");

endif;

Re: Select 15 Cases randomly and write into external file

Posted: October 30th, 2020, 1:57 pm
by Yass
Thanks Aaron,

Please at the end , i added before enddo

Code: Select all

writecase(sampled_15)


how do i write the case to the sample dicts.

Thanks

Re: Select 15 Cases randomly and write into external file

Posted: November 2nd, 2020, 11:28 am
by Gregory Martin
It's not possible to answer your question without knowing what the rest of your code does. However, in that code, all_cases(index), contains the "questionnaire ID" of the selected case, so you will do something with it.
MY_SAMPLE_ID = all_cases(index);
// set some values
writecase(MY_SAMPLE_DICT);

Re: Select 15 Cases randomly and write into external file

Posted: November 2nd, 2020, 11:59 am
by josh
You can see a full example here http://www.teleyah.com/cspro/RwandaNov2018/. Look at session 3.

Re: Select 15 Cases randomly and write into external file

Posted: November 2nd, 2020, 5:34 pm
by Yass
Thanks Greg and Josh.