1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Free Software Foundation, Inc
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <gsl/gsl_cdf.h>
22 #include "data/case.h"
23 #include "data/casegrouper.h"
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/format.h"
28 #include "data/variable.h"
29 #include "data/subcase.h"
30 #include "data/casewriter.h"
31 #include "data/short-names.h"
32 #include "language/command.h"
33 #include "language/lexer/lexer.h"
34 #include "language/lexer/variable-parser.h"
35 #include "language/commands/sort-criteria.h"
36 #include "math/sort.h"
37 #include "libpspp/assertion.h"
38 #include "libpspp/i18n.h"
39 #include "libpspp/message.h"
40 #include "libpspp/misc.h"
41 #include "libpspp/pool.h"
42 #include "libpspp/stringi-set.h"
43 #include "libpspp/taint.h"
44 #include "output/pivot-table.h"
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) (msgid)
52 typedef double (*rank_function_t) (const struct rank*, double c, double cc, double cc_1,
55 static double rank_proportion (const struct rank *, double c, double cc, double cc_1,
58 static double rank_normal (const struct rank *, double c, double cc, double cc_1,
61 static double rank_percent (const struct rank *, double c, double cc, double cc_1,
64 static double rank_rfraction (const struct rank *, double c, double cc, double cc_1,
67 static double rank_rank (const struct rank *, double c, double cc, double cc_1,
70 static double rank_n (const struct rank *, double c, double cc, double cc_1,
73 static double rank_savage (const struct rank *, double c, double cc, double cc_1,
76 static double rank_ntiles (const struct rank *, double c, double cc, double cc_1,
93 static const struct fmt_spec dest_format[n_RANK_FUNCS] = {
94 [RANK] = { .type = FMT_F, .w = 9, .d = 3 },
95 [NORMAL] = { .type = FMT_F, .w = 6, .d = 4 },
96 [PERCENT] = { .type = FMT_F, .w = 6, .d = 2 },
97 [RFRACTION] = { .type = FMT_F, .w = 6, .d = 4 },
98 [PROPORTION] = { .type = FMT_F, .w = 6, .d = 4 },
99 [N] = { .type = FMT_F, .w = 6, .d = 0 },
100 [NTILES] = { .type = FMT_F, .w = 3, .d = 0 },
101 [SAVAGE] = { .type = FMT_F, .w = 8, .d = 4 }
104 static const char * const function_name[n_RANK_FUNCS] = {
115 static const rank_function_t rank_func[n_RANK_FUNCS] = {
126 static enum measure rank_measures[n_RANK_FUNCS] = {
127 [RANK] = MEASURE_ORDINAL,
128 [NORMAL] = MEASURE_ORDINAL,
129 [PERCENT] = MEASURE_ORDINAL,
130 [RFRACTION] = MEASURE_ORDINAL,
131 [PROPORTION] = MEASURE_ORDINAL,
133 [NTILES] = MEASURE_ORDINAL,
134 [SAVAGE] = MEASURE_ORDINAL,
155 enum rank_func rfunc;
156 const char **dest_names;
157 const char **dest_labels;
160 /* If NEW_NAME exists in DICT or NEW_NAMES, returns NULL without changing
161 anything. Otherwise, inserts NEW_NAME in NEW_NAMES and returns the copy of
162 NEW_NAME now in NEW_NAMES. In any case, frees NEW_NAME. */
164 try_new_name (char *new_name,
165 const struct dictionary *dict, struct stringi_set *new_names)
167 const char *retval = (!dict_lookup_var (dict, new_name)
168 && stringi_set_insert (new_names, new_name)
169 ? stringi_set_find_node (new_names, new_name)->string
175 /* Returns a variable name for storing ranks of a variable named SRC_NAME
176 according to the rank function F. The name chosen will not be one already in
179 If successful, adds the new name to NEW_NAMES and returns the name added.
180 If no name can be generated, returns NULL. */
182 rank_choose_dest_name (struct dictionary *dict, struct stringi_set *new_names,
183 enum rank_func f, const char *src_name)
185 /* Try the first character of the ranking function followed by the first 7
186 bytes of the srcinal variable name. */
187 char *src_name_7 = utf8_encoding_trunc (src_name, dict_get_encoding (dict),
189 const char *s = try_new_name (
190 xasprintf ("%c%s", function_name[f][0], src_name_7), dict, new_names);
196 for (int i = 1; i <= 999; i++)
198 s = try_new_name (xasprintf ("%.3s%03d", function_name[f], i),
205 for (int i = 1; i <= 99; i++)
207 s = try_new_name (xasprintf ("RNK%.2s%02d", function_name[f], i),
213 msg (ME, _("Cannot generate variable name for ranking %s with %s. "
214 "All candidates in use."),
215 src_name, function_name[f]);
221 struct dictionary *dict;
225 const struct variable **vars;
228 const struct variable **group_vars;
232 enum mv_class exclude;
234 struct rank_spec *rs;
239 enum fraction fraction;
244 /* Pool on which cell functions may allocate data */
250 destroy_rank (struct rank *rank)
253 free (rank->group_vars);
254 subcase_uninit (&rank->sc);
255 pool_destroy (rank->pool);
259 parse_into (struct lexer *lexer, struct rank *cmd,
260 struct stringi_set *new_names)
262 enum rank_func rfunc;
263 if (lex_match_id (lexer, "RANK"))
265 else if (lex_match_id (lexer, "NORMAL"))
267 else if (lex_match_id (lexer, "RFRACTION"))
269 else if (lex_match_id (lexer, "N"))
271 else if (lex_match_id (lexer, "SAVAGE"))
273 else if (lex_match_id (lexer, "PERCENT"))
275 else if (lex_match_id (lexer, "PROPORTION"))
277 else if (lex_match_id (lexer, "NTILES"))
279 if (!lex_force_match (lexer, T_LPAREN)
280 || !lex_force_int_range (lexer, "NTILES", 1, INT_MAX))
283 cmd->k_ntiles = lex_integer (lexer);
286 if (!lex_force_match (lexer, T_RPAREN))
293 lex_error_expecting (lexer, "RANK", "NORMAL", "RFRACTION", "N",
294 "SAVAGE", "PERCENT", "PROPORTION", "NTILES");
298 cmd->rs = pool_realloc (cmd->pool, cmd->rs, sizeof (*cmd->rs) * (cmd->n_rs + 1));
299 struct rank_spec *rs = &cmd->rs[cmd->n_rs++];
300 *rs = (struct rank_spec) {
302 .dest_names = pool_calloc (cmd->pool, cmd->n_vars,
303 sizeof *rs->dest_names),
306 if (lex_match_id (lexer, "INTO"))
308 int vars_start = lex_ofs (lexer);
309 size_t var_count = 0;
310 while (lex_token (lexer) == T_ID)
312 const char *name = lex_tokcstr (lexer);
314 if (var_count >= subcase_get_n_fields (&cmd->sc))
315 lex_ofs_error (lexer, vars_start, lex_ofs (lexer),
316 _("Too many variables in %s clause."), "INTO");
317 else if (dict_lookup_var (cmd->dict, name) != NULL)
318 lex_error (lexer, _("Variable %s already exists."), name);
319 else if (stringi_set_contains (new_names, name))
320 lex_error (lexer, _("Duplicate variable name %s."), name);
323 stringi_set_insert (new_names, name);
324 rs->dest_names[var_count++] = pool_strdup (cmd->pool, name);
337 /* Hardly a rank function. */
339 rank_n (const struct rank *cmd UNUSED, double c UNUSED, double cc UNUSED, double cc_1 UNUSED,
340 int i UNUSED, double w)
346 rank_rank (const struct rank *cmd, double c, double cc, double cc_1,
347 int i, double w UNUSED)
362 rank = cc_1 + (c + 1.0)/ 2.0;
382 rank = cc_1 + c / 2.0;
397 rank_rfraction (const struct rank *cmd, double c, double cc, double cc_1,
400 return rank_rank (cmd, c, cc, cc_1, i, w) / w;
405 rank_percent (const struct rank *cmd, double c, double cc, double cc_1,
408 return rank_rank (cmd, c, cc, cc_1, i, w) * 100.0 / w;
413 rank_proportion (const struct rank *cmd, double c, double cc, double cc_1,
416 const double r = rank_rank (cmd, c, cc, cc_1, i, w);
420 switch (cmd->fraction)
423 f = (r - 3.0/8.0) / (w + 0.25);
429 f = (r - 1.0/3.0) / (w + 1.0/3.0);
439 return (f > 0) ? f : SYSMIS;
443 rank_normal (const struct rank *cmd, double c, double cc, double cc_1,
446 double f = rank_proportion (cmd, c, cc, cc_1, i, w);
448 return gsl_cdf_ugaussian_Pinv (f);
452 rank_ntiles (const struct rank *cmd, double c, double cc, double cc_1,
455 double r = rank_rank (cmd, c, cc, cc_1, i, w);
458 return (floor ((r * cmd->k_ntiles) / (w + 1)) + 1);
461 /* Expected value of the order statistics from an exponential distribution */
463 ee (int j, double w_star)
467 for (int k = 1; k <= j; k++)
468 sum += 1.0 / (w_star + 1 - k);
475 rank_savage (const struct rank *cmd UNUSED, double c, double cc, double cc_1,
476 int i UNUSED, double w)
479 const int i_1 = floor (cc_1);
480 const int i_2 = floor (cc);
482 const double w_star = (modf (w, &int_part) == 0) ? w : floor (w) + 1;
484 const double g_1 = cc_1 - i_1;
485 const double g_2 = cc - i_2;
487 /* The second factor is infinite, when the first is zero.
488 Therefore, evaluate the second, only when the first is non-zero */
489 const double expr1 = (1 - g_1) ? (1 - g_1) * ee(i_1+1, w_star) : (1 - g_1);
490 const double expr2 = g_2 ? g_2 * ee (i_2+1, w_star) : g_2;
493 return ee (i_1 + 1, w_star) - 1;
496 return ((expr1 + expr2)/c) - 1;
501 for (int j = i_1 + 2; j <= i_2; ++j)
502 sigma += ee (j, w_star);
503 return ((expr1 + expr2 + sigma) / c) -1;
510 sum_weights (const struct casereader *input, int weight_idx)
512 if (weight_idx == -1)
513 return casereader_count_cases (input);
517 struct casereader *pass = casereader_clone (input);
519 for (; (c = casereader_read (pass)) != NULL; case_unref (c))
520 w += case_num_idx (c, weight_idx);
521 casereader_destroy (pass);
527 rank_sorted_file (struct casereader *input,
528 struct casewriter *output,
530 const struct rank *cmd)
535 /* Get total group weight. */
536 double w = sum_weights (input, weight_idx);
539 struct subcase input_var = SUBCASE_EMPTY_INITIALIZER;
540 subcase_add (&input_var, 0, 0, SC_ASCEND);
541 struct casegrouper *tie_grouper = casegrouper_create_subcase (input, &input_var);
542 subcase_uninit (&input_var);
544 struct casereader *tied_cases;
545 for (; casegrouper_get_next_group (tie_grouper, &tied_cases);
546 casereader_destroy (tied_cases))
548 double tw = sum_weights (tied_cases, weight_idx);
552 taint_propagate (casereader_get_taint (tied_cases),
553 casewriter_get_taint (output));
555 /* Rank tied cases. */
557 for (; (c = casereader_read (tied_cases)) != NULL; case_unref (c))
559 struct ccase *out_case = case_create (casewriter_get_proto (output));
560 *case_num_rw_idx (out_case, 0) = case_num_idx (c, 1);
561 for (size_t i = 0; i < cmd->n_rs; ++i)
563 rank_function_t func = rank_func[cmd->rs[i].rfunc];
564 double rank = func (cmd, tw, cc, cc_1, tie_group, w);
565 *case_num_rw_idx (out_case, i + 1) = rank;
568 casewriter_write (output, out_case);
572 casegrouper_destroy (tie_grouper);
577 rank_cmd (struct dataset *ds, const struct rank *cmd);
580 fraction_name (const struct rank *cmd)
582 switch (cmd->fraction)
584 case FRAC_BLOM: return "BLOM";
585 case FRAC_RANKIT: return "RANKIT";
586 case FRAC_TUKEY: return "TUKEY";
587 case FRAC_VW: return "VW";
588 default: NOT_REACHED ();
592 /* Returns a label for a variable derived from SRC_VAR with function F. */
594 create_var_label (struct rank *cmd, const struct variable *src_var,
597 if (cmd->n_group_vars > 0)
599 struct string group_var_str = DS_EMPTY_INITIALIZER;
600 for (size_t g = 0; g < cmd->n_group_vars; ++g)
603 ds_put_cstr (&group_var_str, " ");
604 ds_put_cstr (&group_var_str, var_get_name (cmd->group_vars[g]));
607 const char *label = pool_asprintf (
608 cmd->pool, _("%s of %s by %s"), function_name[f],
609 var_get_name (src_var), ds_cstr (&group_var_str));
610 ds_destroy (&group_var_str);
614 return pool_asprintf (cmd->pool, _("%s of %s"),
615 function_name[f], var_get_name (src_var));
619 cmd_rank (struct lexer *lexer, struct dataset *ds)
621 struct stringi_set new_names = STRINGI_SET_INITIALIZER (new_names);
623 .sc = SUBCASE_EMPTY_INITIALIZER,
625 .dict = dataset_dict (ds),
627 .fraction = FRAC_BLOM,
629 .pool = pool_create (),
632 if (lex_match_id (lexer, "VARIABLES") && !lex_force_match (lexer, T_EQUALS))
635 if (!parse_sort_criteria (lexer, rank.dict, &rank.sc, &rank.vars, NULL))
637 rank.n_vars = rank.sc.n_fields;
639 if (lex_match (lexer, T_BY)
640 && !parse_variables_const (lexer, rank.dict,
641 &rank.group_vars, &rank.n_group_vars,
642 PV_NO_DUPLICATE | PV_NO_SCRATCH))
645 while (lex_token (lexer) != T_ENDCMD)
647 if (!lex_force_match (lexer, T_SLASH))
649 if (lex_match_id (lexer, "TIES"))
651 if (!lex_force_match (lexer, T_EQUALS))
653 if (lex_match_id (lexer, "MEAN"))
654 rank.ties = TIES_MEAN;
655 else if (lex_match_id (lexer, "LOW"))
656 rank.ties = TIES_LOW;
657 else if (lex_match_id (lexer, "HIGH"))
658 rank.ties = TIES_HIGH;
659 else if (lex_match_id (lexer, "CONDENSE"))
660 rank.ties = TIES_CONDENSE;
663 lex_error_expecting (lexer, "MEAN", "LOW", "HIGH", "CONDENSE");
667 else if (lex_match_id (lexer, "FRACTION"))
669 if (!lex_force_match (lexer, T_EQUALS))
671 if (lex_match_id (lexer, "BLOM"))
672 rank.fraction = FRAC_BLOM;
673 else if (lex_match_id (lexer, "TUKEY"))
674 rank.fraction = FRAC_TUKEY;
675 else if (lex_match_id (lexer, "VW"))
676 rank.fraction = FRAC_VW;
677 else if (lex_match_id (lexer, "RANKIT"))
678 rank.fraction = FRAC_RANKIT;
681 lex_error_expecting (lexer, "BLOM", "TUKEY", "VW", "RANKIT");
685 else if (lex_match_id (lexer, "PRINT"))
687 if (!lex_force_match (lexer, T_EQUALS))
689 if (lex_match_id (lexer, "YES"))
691 else if (lex_match_id (lexer, "NO"))
695 lex_error_expecting (lexer, "YES", "NO");
699 else if (lex_match_id (lexer, "MISSING"))
701 if (!lex_force_match (lexer, T_EQUALS))
703 if (lex_match_id (lexer, "INCLUDE"))
704 rank.exclude = MV_SYSTEM;
705 else if (lex_match_id (lexer, "EXCLUDE"))
706 rank.exclude = MV_ANY;
709 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
713 else if (!parse_into (lexer, &rank, &new_names))
718 /* If no rank specs are given, then apply a default */
721 struct rank_spec *rs = pool_malloc (rank.pool, sizeof *rs);
722 *rs = (struct rank_spec) {
724 .dest_names = pool_calloc (rank.pool, rank.n_vars,
725 sizeof *rs->dest_names),
732 /* Choose variable names for all rank destinations which haven't already been
733 created with INTO. */
734 for (struct rank_spec *rs = rank.rs; rs < &rank.rs[rank.n_rs]; rs++)
736 rs->dest_labels = pool_calloc (rank.pool, rank.n_vars,
737 sizeof *rs->dest_labels);
738 for (int v = 0; v < rank.n_vars; v ++)
740 const char **dst_name = &rs->dest_names[v];
741 if (*dst_name == NULL)
743 *dst_name = rank_choose_dest_name (rank.dict, &new_names,
745 var_get_name (rank.vars[v]));
746 if (*dst_name == NULL)
750 rs->dest_labels[v] = create_var_label (&rank, rank.vars[v],
757 struct pivot_table *table = pivot_table_create (
758 N_("Variables Created by RANK"));
760 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("New Variable"),
761 N_("New Variable"), N_("Function"),
762 N_("Fraction"), N_("Grouping Variables"));
764 struct pivot_dimension *variables = pivot_dimension_create (
765 table, PIVOT_AXIS_ROW, N_("Existing Variable"),
766 N_("Existing Variable"));
767 variables->root->show_label = true;
769 for (size_t i = 0; i < rank.n_rs; ++i)
771 for (size_t v = 0; v < rank.n_vars; v ++)
773 int row_idx = pivot_category_create_leaf (
774 variables->root, pivot_value_new_variable (rank.vars[v]));
776 struct string group_vars = DS_EMPTY_INITIALIZER;
777 for (int g = 0; g < rank.n_group_vars; ++g)
780 ds_put_byte (&group_vars, ' ');
781 ds_put_cstr (&group_vars, var_get_name (rank.group_vars[g]));
784 enum rank_func rfunc = rank.rs[i].rfunc;
785 bool has_fraction = rfunc == NORMAL || rfunc == PROPORTION;
786 const char *entries[] =
788 rank.rs[i].dest_names[v],
789 function_name[rank.rs[i].rfunc],
790 has_fraction ? fraction_name (&rank) : NULL,
791 rank.n_group_vars ? ds_cstr (&group_vars) : NULL,
793 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
795 const char *entry = entries[j];
797 pivot_table_put2 (table, j, row_idx,
798 pivot_value_new_user_text (entry, -1));
800 ds_destroy (&group_vars);
804 pivot_table_submit (table);
808 rank_cmd (ds, &rank);
810 destroy_rank (&rank);
811 stringi_set_destroy (&new_names);
815 destroy_rank (&rank);
816 stringi_set_destroy (&new_names);
820 /* RANK transformation. */
823 struct caseproto *proto;
827 struct rank_trns_input_var *input_vars;
833 struct rank_trns_input_var
835 struct casereader *input;
836 struct ccase *current;
838 size_t *output_var_indexes;
842 advance_ranking (struct rank_trns_input_var *iv)
844 case_unref (iv->current);
845 iv->current = casereader_read (iv->input);
848 static struct ccase *
849 rank_translate (struct ccase *c, void *trns_)
851 struct rank_trns *trns = trns_;
853 c = case_unshare_and_resize (c, trns->proto);
854 double order = case_num_idx (c, trns->order_case_idx);
855 for (struct rank_trns_input_var *iv = trns->input_vars;
856 iv < &trns->input_vars[trns->n_input_vars]; iv++)
858 for (size_t i = 0; i < trns->n_funcs; i++)
859 *case_num_rw_idx (c, iv->output_var_indexes[i]) = SYSMIS;
861 while (iv->current != NULL)
863 double iv_order = case_num_idx (iv->current, 0);
864 if (iv_order == order)
866 for (size_t i = 0; i < trns->n_funcs; i++)
867 *case_num_rw_idx (c, iv->output_var_indexes[i])
868 = case_num_idx (iv->current, i + 1);
869 advance_ranking (iv);
872 else if (iv_order > order)
875 advance_ranking (iv);
882 rank_trns_free (void *trns_)
884 struct rank_trns *trns = trns_;
886 caseproto_unref (trns->proto);
887 for (struct rank_trns_input_var *iv = trns->input_vars;
888 iv < &trns->input_vars[trns->n_input_vars]; iv++)
890 casereader_destroy (iv->input);
891 case_unref (iv->current);
893 free (iv->output_var_indexes);
895 free (trns->input_vars);
901 static const struct casereader_translator_class rank_trns_class = {
902 .translate = rank_translate,
903 .destroy = rank_trns_free,
907 rank_cmd (struct dataset *ds, const struct rank *cmd)
909 struct dictionary *d = dataset_dict (ds);
910 struct variable *weight_var = dict_get_weight (d);
913 struct variable *order_var = add_permanent_ordering_transformation (ds);
915 /* Create output files. */
916 struct caseproto *output_proto = caseproto_create ();
917 for (size_t i = 0; i < cmd->n_rs + 1; i++)
918 output_proto = caseproto_add_width (output_proto, 0);
920 struct subcase by_order;
921 subcase_init (&by_order, 0, 0, SC_ASCEND);
923 struct casewriter **outputs = xnmalloc (cmd->n_vars, sizeof *outputs);
924 for (size_t i = 0; i < cmd->n_vars; i++)
925 outputs[i] = sort_create_writer (&by_order, output_proto);
927 subcase_uninit (&by_order);
928 caseproto_unref (output_proto);
930 /* Open the active file and make one pass per input variable. */
931 struct casereader *input = proc_open (ds);
932 input = casereader_create_filter_weight (input, d, NULL, NULL);
933 for (size_t i = 0; i < cmd->n_vars; ++i)
935 const struct variable *input_var = cmd->vars[i];
937 /* Discard cases that have missing values of input variable. */
938 struct casereader *input_pass
939 = i == cmd->n_vars - 1 ? input : casereader_clone (input);
940 input_pass = casereader_create_filter_missing (input_pass, &input_var, 1,
941 cmd->exclude, NULL, NULL);
943 /* Keep only the columns we really need, to save time and space when we
944 sort them just below.
946 After this projection, the input_pass case indexes look like:
950 - 2 and up: cmd->n_group_vars group variables
951 - 2 + cmd->n_group_vars and up: split variables
952 - 2 + cmd->n_group_vars + n_split_vars: weight var
954 struct subcase projection = SUBCASE_EMPTY_INITIALIZER;
955 subcase_add_var_always (&projection, input_var, SC_ASCEND);
956 subcase_add_var_always (&projection, order_var, SC_ASCEND);
957 subcase_add_vars_always (&projection,
958 cmd->group_vars, cmd->n_group_vars);
959 subcase_add_vars_always (&projection, dict_get_split_vars (d),
960 dict_get_n_splits (d));
962 if (weight_var != NULL)
964 subcase_add_var_always (&projection, weight_var, SC_ASCEND);
965 weight_idx = 2 + cmd->n_group_vars + dict_get_n_splits (d);
969 input_pass = casereader_project (input_pass, &projection);
970 subcase_uninit (&projection);
972 /* Prepare 'group_vars' as the set of grouping variables. */
973 struct subcase group_vars = SUBCASE_EMPTY_INITIALIZER;
974 for (size_t j = 0; j < cmd->n_group_vars; j++)
975 subcase_add_always (&group_vars,
976 j + 2, var_get_width (cmd->group_vars[j]),
979 /* Prepare 'rank_ordering' for sorting with the group variables as
980 primary key and the input variable as secondary key. */
981 struct subcase rank_ordering;
982 subcase_clone (&rank_ordering, &group_vars);
983 subcase_add (&rank_ordering, 0, 0, subcase_get_direction (&cmd->sc, i));
985 /* Group by split variables */
986 struct subcase split_vars = SUBCASE_EMPTY_INITIALIZER;
987 for (size_t j = 0; j < dict_get_n_splits (d); j++)
988 subcase_add_always (&split_vars, 2 + j + cmd->n_group_vars,
989 var_get_width (dict_get_split_vars (d)[j]),
992 struct casegrouper *split_grouper
993 = casegrouper_create_subcase (input_pass, &split_vars);
994 subcase_uninit (&split_vars);
996 struct casereader *split_group;
997 while (casegrouper_get_next_group (split_grouper, &split_group))
999 struct casereader *ordered;
1000 struct casegrouper *by_grouper;
1001 struct casereader *by_group;
1003 ordered = sort_execute (split_group, &rank_ordering);
1004 by_grouper = casegrouper_create_subcase (ordered, &group_vars);
1005 while (casegrouper_get_next_group (by_grouper, &by_group))
1006 rank_sorted_file (by_group, outputs[i], weight_idx, cmd);
1007 ok = casegrouper_destroy (by_grouper) && ok;
1009 subcase_uninit (&group_vars);
1010 subcase_uninit (&rank_ordering);
1012 ok = casegrouper_destroy (split_grouper) && ok;
1014 ok = proc_commit (ds) && ok;
1016 /* Re-fetch the dictionary and order variable, because if TEMPORARY was in
1017 effect then there's a new dictionary. */
1018 d = dataset_dict (ds);
1019 order_var = dict_lookup_var_assert (d, "$ORDER");
1021 /* Merge the original data set with the ranks (which we already sorted on
1023 struct rank_trns *trns = xmalloc (sizeof *trns);
1024 *trns = (struct rank_trns) {
1025 .order_case_idx = var_get_dict_index (order_var),
1026 .input_vars = xnmalloc (cmd->n_vars, sizeof *trns->input_vars),
1027 .n_input_vars = cmd->n_vars,
1028 .n_funcs = cmd->n_rs,
1030 for (size_t i = 0; i < trns->n_input_vars; i++)
1032 struct rank_trns_input_var *iv = &trns->input_vars[i];
1034 iv->input = casewriter_make_reader (outputs[i]);
1035 iv->current = casereader_read (iv->input);
1036 iv->output_var_indexes = xnmalloc (trns->n_funcs,
1037 sizeof *iv->output_var_indexes);
1038 for (size_t j = 0; j < trns->n_funcs; j++)
1040 struct rank_spec *rs = &cmd->rs[j];
1041 struct variable *var;
1043 var = dict_create_var_assert (d, rs->dest_names[i], 0);
1044 var_set_both_formats (var, dest_format[rs->rfunc]);
1045 var_set_label (var, rs->dest_labels[i]);
1046 var_set_measure (var, rank_measures[rs->rfunc]);
1048 iv->output_var_indexes[j] = var_get_dict_index (var);
1053 trns->proto = caseproto_ref (dict_get_proto (d)),
1054 dataset_set_source (ds, casereader_translate_stateless (
1055 dataset_steal_source (ds), trns->proto,
1056 &rank_trns_class, trns));
1058 /* Delete our sort key, which we don't need anymore. */
1059 dataset_delete_vars (ds, &order_var, 1);