Select 15 Cases randomly and write into external file

Discussions about creating CAPI applications to run on Android devices
Post Reply
Yass
Posts: 86
Joined: November 11th, 2017, 1:26 am

Select 15 Cases randomly and write into external file

Post by Yass » October 30th, 2020, 8:46 am

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.

aaronw
Posts: 374
Joined: June 9th, 2016, 9:38 am
Location: Washington, DC

Re: Select 15 Cases randomly and write into external file

Post by aaronw » October 30th, 2020, 12:42 pm

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;

Yass
Posts: 86
Joined: November 11th, 2017, 1:26 am

Re: Select 15 Cases randomly and write into external file

Post by Yass » October 30th, 2020, 1:57 pm

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

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

Re: Select 15 Cases randomly and write into external file

Post by Gregory Martin » November 2nd, 2020, 11:28 am

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);

josh
Posts: 2351
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Select 15 Cases randomly and write into external file

Post by josh » November 2nd, 2020, 11:59 am

You can see a full example here http://www.teleyah.com/cspro/RwandaNov2018/. Look at session 3.

Yass
Posts: 86
Joined: November 11th, 2017, 1:26 am

Re: Select 15 Cases randomly and write into external file

Post by Yass » November 2nd, 2020, 5:34 pm

Thanks Greg and Josh.

Post Reply