- struct casefile *out ;
- struct casefile *cf ;
- struct casereader *reader ;
- struct casefile *sorted_cf ;
-
- /* Obtain active file in CF. */
- if (!procedure (NULL, NULL))
- goto error;
-
- cf = proc_capture_output ();
-
- /* 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 (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_proto (d));
+
+ while (casegrouper_get_next_group (split_grouper, &split_group))
+ {
+ struct subcase ordering;
+ struct casereader *ordered;
+ struct casegrouper *by_grouper;
+ struct casereader *by_group;
+
+ /* Sort this split group by the BY variables as primary
+ keys and the rank variable as secondary key. */
+ subcase_init_vars (&ordering, group_vars, n_group_vars);
+ subcase_add_var (&ordering, src_vars[i],
+ subcase_get_direction (sc, i));
+ ordered = sort_execute (split_group, &ordering);
+ subcase_destroy (&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. */