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.
Select 15 Cases randomly and write into external file
Re: Select 15 Cases randomly and write into external file
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;
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
Thanks Aaron,
Please at the end , i added before enddo
how do i write the case to the sample dicts.
Thanks
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
-
- Posts: 1796
- Joined: December 5th, 2011, 11:27 pm
- Location: Washington, DC
Re: Select 15 Cases randomly and write into external file
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);
// set some values
writecase(MY_SAMPLE_DICT);
Re: Select 15 Cases randomly and write into external file
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
Thanks Greg and Josh.