Select 15 Cases randomly and write into external file

Discussions about creating CAPI applications to run on Android devices
Forum rules
New release: CSPro 8.0
Post Reply
Yass
Posts: 103
Joined: November 11th, 2017, 1:26 am

Select 15 Cases randomly and write into external file

Post 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.
aaronw
Posts: 561
Joined: June 9th, 2016, 9:38 am
Location: Washington, DC

Re: Select 15 Cases randomly and write into external file

Post 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;
Yass
Posts: 103
Joined: November 11th, 2017, 1:26 am

Re: Select 15 Cases randomly and write into external file

Post 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
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Select 15 Cases randomly and write into external file

Post 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);
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Select 15 Cases randomly and write into external file

Post by josh »

You can see a full example here http://www.teleyah.com/cspro/RwandaNov2018/. Look at session 3.
Yass
Posts: 103
Joined: November 11th, 2017, 1:26 am

Re: Select 15 Cases randomly and write into external file

Post by Yass »

Thanks Greg and Josh.
Post Reply