- struct casefile *out ;
- struct casefile *cf ;
- struct casereader *reader ;
- struct casefile *sorted_cf ;
-
- /* Obtain active file in CF. */
- if (!procedure (current_dataset, NULL, NULL))
- goto error;
-
- cf = proc_capture_output (current_dataset);
-
- /* Sort CF into SORTED_CF. */
- reader = casefile_get_destructive_reader (cf) ;
- criteria.crits[criteria.crit_cnt - 1] = sc->crits[i];
- assert ( sc->crits[i].fv == src_vars[i]->fv );
- sorted_cf = sort_execute (reader, &criteria);
- casefile_destroy (cf);
-
- out = rank_sorted_casefile (sorted_cf, &criteria,
- rank_specs, n_rank_specs,
- i, &src_vars[i]->miss) ;
- if ( NULL == out )
- {
- result = false ;
- continue ;
- }
-
- proc_set_source (current_dataset, storage_source_create (out));
- }
+ /* Rank variable at index I in SC. */
+ struct casegrouper *split_grouper;
+ struct casereader *split_group;
+ struct casewriter *output;
+
+ proc_discard_output (ds);
+ split_grouper = casegrouper_create_splits (proc_open (ds), d);
+ output = autopaging_writer_create (dict_get_next_value_idx (d));
+
+ while (casegrouper_get_next_group (split_grouper, &split_group))
+ {
+ struct case_ordering *ordering;
+ struct casereader *ordered;
+ struct casegrouper *by_grouper;
+ struct casereader *by_group;
+ int j;
+
+ /* Sort this split group by the BY variables as primary
+ keys and the rank variable as secondary key. */
+ ordering = case_ordering_create ();
+ for (j = 0; j < n_group_vars; j++)
+ case_ordering_add_var (ordering, group_vars[j], SRT_ASCEND);
+ case_ordering_add_var (ordering,
+ case_ordering_get_var (sc, i),
+ case_ordering_get_direction (sc, i));
+ ordered = sort_execute (split_group, ordering);
+
+ /* Rank the rank variable within this split group. */
+ by_grouper = casegrouper_create_vars (ordered,
+ group_vars, n_group_vars);
+ while (casegrouper_get_next_group (by_grouper, &by_group))
+ {
+ /* Rank the rank variable within this BY group
+ within the split group. */