rank_cmd (struct dataset *ds, const struct case_ordering *sc,
const struct rank_spec *rank_specs, int n_rank_specs)
{
- struct case_ordering *base_ordering;
+ struct dictionary *d = dataset_dict (ds);
bool ok = true;
int i;
- const int n_splits = dict_get_split_cnt (dataset_dict (ds));
- base_ordering = case_ordering_create (dataset_dict (ds));
- for (i = 0; i < n_splits ; i++)
- case_ordering_add_var (base_ordering,
- dict_get_split_vars (dataset_dict (ds))[i],
- SRT_ASCEND);
-
- for (i = 0; i < n_group_vars; i++)
- case_ordering_add_var (base_ordering, group_vars[i], SRT_ASCEND);
for (i = 0 ; i < case_ordering_get_var_cnt (sc) ; ++i )
{
- struct case_ordering *ordering;
- struct casegrouper *grouper;
- struct casereader *group;
+ /* Rank variable at index I in SC. */
+ struct casegrouper *split_grouper;
+ struct casereader *split_group;
struct casewriter *output;
- struct casereader *ranked_file;
-
- ordering = case_ordering_clone (base_ordering);
- case_ordering_add_var (ordering,
- case_ordering_get_var (sc, i),
- case_ordering_get_direction (sc, i));
proc_discard_output (ds);
- grouper = casegrouper_create_case_ordering (sort_execute (proc_open (ds),
- ordering),
- base_ordering);
- output = autopaging_writer_create (dict_get_next_value_idx (
- dataset_dict (ds)));
- while (casegrouper_get_next_group (grouper, &group))
- rank_sorted_file (group, output, dataset_dict (ds),
- rank_specs, n_rank_specs,
- i, src_vars[i]);
- ok = casegrouper_destroy (grouper);
+ 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 (d);
+ 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. */
+
+ rank_sorted_file (by_group, output, d, rank_specs, n_rank_specs,
+ i, src_vars[i]);
+ }
+ ok = casegrouper_destroy (by_grouper) && ok;
+ }
+ ok = casegrouper_destroy (split_grouper);
ok = proc_commit (ds) && ok;
- ranked_file = casewriter_make_reader (output);
- ok = proc_set_active_file_data (ds, ranked_file) && ok;
+ ok = (proc_set_active_file_data (ds, casewriter_make_reader (output))
+ && ok);
if (!ok)
break;
}
- case_ordering_destroy (base_ordering);
return ok;
}
NEW FILE.
DATA LIST LIST NOTABLE /a * g1 g2 *.
BEGIN DATA.
+2 1 2
+2 1 2
+3 1 2
+4 1 2
+5 1 2
1 0 2
2 0 2
3 0 2
6 0 2
7 0 2
8 0 2
-2 1 2
-2 1 2
-3 1 2
-4 1 2
-5 1 2
6 1 2
7 1 2
7 1 2
/NORMAL
.
+SPLIT FILE BY g1.
+
+RANK a (D) BY g2
+ /PRINT=YES
+ /TIES=LOW
+ /MISSING=INCLUDE
+ /FRACTION=RANKIT
+ /RANK
+ /NORMAL
+ .
+
+SPLIT FILE OFF.
+
LIST.
Variables Created By RANK
a into Ra(RANK of a BY g2 g1)
a into Na(NORMAL of a using RANKIT BY g2 g1)
- a g1 g2 Ra Na
--------- -------- -------- --------- ------
- 1.00 .00 2.00 8.000 1.5341
- 2.00 .00 2.00 7.000 .8871
- 3.00 .00 2.00 6.000 .4888
- 4.00 .00 2.00 5.000 .1573
- 5.00 .00 2.00 4.000 -.1573
- 6.00 .00 2.00 3.000 -.4888
- 7.00 .00 2.00 2.000 -.8871
- 8.00 .00 2.00 1.000 -1.534
- 2.00 1.00 2.00 8.000 .9674
- 2.00 1.00 2.00 8.000 .9674
- 3.00 1.00 2.00 7.000 .5895
- 4.00 1.00 2.00 6.000 .2822
- 5.00 1.00 2.00 5.000 .0000
- 6.00 1.00 2.00 4.000 -.2822
- 7.00 1.00 2.00 2.000 -.9674
- 7.00 1.00 2.00 2.000 -.9674
- 8.00 1.00 2.00 1.000 -1.593
- 9.00 1.00 1.00 1.000 .0000
+Variables Created By RANK
+a into RAN001(RANK of a BY g2)
+a into NOR001(NORMAL of a using RANKIT BY g2)
+ a g1 g2 Ra Na RAN001 NOR001
+-------- -------- -------- --------- ------ --------- ------
+ 2.00 1.00 2.00 8.000 .9674 4.000 .5244
+ 2.00 1.00 2.00 8.000 .9674 4.000 .5244
+ 3.00 1.00 2.00 7.000 .5895 3.000 .0000
+ 4.00 1.00 2.00 6.000 .2822 2.000 -.5244
+ 5.00 1.00 2.00 5.000 .0000 1.000 -1.282
+ 1.00 .00 2.00 8.000 1.5341 8.000 1.5341
+ 2.00 .00 2.00 7.000 .8871 7.000 .8871
+ 3.00 .00 2.00 6.000 .4888 6.000 .4888
+ 4.00 .00 2.00 5.000 .1573 5.000 .1573
+ 5.00 .00 2.00 4.000 -.1573 4.000 -.1573
+ 6.00 .00 2.00 3.000 -.4888 3.000 -.4888
+ 7.00 .00 2.00 2.000 -.8871 2.000 -.8871
+ 8.00 .00 2.00 1.000 -1.534 1.000 -1.534
+ 6.00 1.00 2.00 4.000 -.2822 4.000 1.1503
+ 7.00 1.00 2.00 2.000 -.9674 2.000 -.3186
+ 7.00 1.00 2.00 2.000 -.9674 2.000 -.3186
+ 8.00 1.00 2.00 1.000 -1.593 1.000 -1.150
+ 9.00 1.00 1.00 1.000 .0000 1.000 .0000
fractional ranks ( including small ones for special case of SAVAGE ranks)
Variables Created By RANK
a into Pa(PROPORTION of a using TUKEY)