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/stats/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/string-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] = {
145 enum rank_func rfunc;
146 const char **dest_names;
147 const char **dest_labels;
150 /* If NEW_NAME exists in DICT or NEW_NAMES, returns NULL without changing
151 anything. Otherwise, inserts NEW_NAME in NEW_NAMES and returns the copy of
152 NEW_NAME now in NEW_NAMES. */
154 try_new_name (const char *new_name,
155 const struct dictionary *dict, struct string_set *new_names)
157 return (!dict_lookup_var (dict, new_name)
158 && string_set_insert (new_names, new_name)
159 ? string_set_find_node (new_names, new_name)->string
163 /* Returns a variable name for storing ranks of a variable named SRC_NAME
164 according to the rank function F. The name chosen will not be one already in
167 If successful, adds the new name to NEW_NAMES and returns the name added.
168 If no name can be generated, returns NULL. */
170 rank_choose_dest_name (struct dictionary *dict, struct string_set *new_names,
171 enum rank_func f, const char *src_name)
178 /* Try the first character of the ranking function followed by the first 7
179 bytes of the srcinal variable name. */
180 src_name_7 = utf8_encoding_trunc (src_name, dict_get_encoding (dict), 7);
181 snprintf (name, sizeof name, "%c%s", function_name[f][0], src_name_7);
183 s = try_new_name (name, dict, new_names);
188 for (i = 1; i <= 999; i++)
190 sprintf (name, "%.3s%03d", function_name[f], i);
191 s = try_new_name (name, dict, new_names);
197 for (i = 1; i <= 99; i++)
199 sprintf (name, "RNK%.2s%02d", function_name[f], i);
200 s = try_new_name (name, dict, new_names);
205 msg (ME, _("Cannot generate variable name for ranking %s with %s. "
206 "All candidates in use."),
207 src_name, function_name[f]);
213 struct dictionary *dict;
217 const struct variable **vars;
220 const struct variable **group_vars;
224 enum mv_class exclude;
226 struct rank_spec *rs;
231 enum fraction fraction;
236 /* Pool on which cell functions may allocate data */
242 destroy_rank (struct rank *rank)
245 free (rank->group_vars);
246 subcase_destroy (&rank->sc);
247 pool_destroy (rank->pool);
251 parse_into (struct lexer *lexer, struct rank *cmd,
252 struct string_set *new_names)
255 struct rank_spec *rs = NULL;
257 cmd->rs = pool_realloc (cmd->pool, cmd->rs, sizeof (*cmd->rs) * (cmd->n_rs + 1));
258 rs = &cmd->rs[cmd->n_rs];
260 if (lex_match_id (lexer, "RANK"))
264 else if (lex_match_id (lexer, "NORMAL"))
268 else if (lex_match_id (lexer, "RFRACTION"))
270 rs->rfunc = RFRACTION;
272 else if (lex_match_id (lexer, "N"))
276 else if (lex_match_id (lexer, "SAVAGE"))
280 else if (lex_match_id (lexer, "PERCENT"))
284 else if (lex_match_id (lexer, "PROPORTION"))
286 rs->rfunc = PROPORTION;
288 else if (lex_match_id (lexer, "NTILES"))
290 if (!lex_force_match (lexer, T_LPAREN))
293 if (! lex_force_int_range (lexer, "NTILES", 1, INT_MAX))
296 cmd->k_ntiles = lex_integer (lexer);
299 if (!lex_force_match (lexer, T_RPAREN))
306 lex_error (lexer, NULL);
311 rs->dest_names = pool_calloc (cmd->pool, cmd->n_vars,
312 sizeof *rs->dest_names);
314 if (lex_match_id (lexer, "INTO"))
316 while(lex_token (lexer) == T_ID)
318 const char *name = lex_tokcstr (lexer);
320 if (var_count >= subcase_get_n_fields (&cmd->sc))
321 msg (SE, _("Too many variables in %s clause."), "INTO");
322 else if (dict_lookup_var (cmd->dict, name) != NULL)
323 msg (SE, _("Variable %s already exists."), name);
324 else if (string_set_contains (new_names, name))
325 msg (SE, _("Duplicate variable name %s."), name);
328 string_set_insert (new_names, name);
329 rs->dest_names[var_count++] = pool_strdup (cmd->pool, name);
342 /* Hardly a rank function !! */
344 rank_n (const struct rank *cmd UNUSED, double c UNUSED, double cc UNUSED, double cc_1 UNUSED,
345 int i UNUSED, double w)
352 rank_rank (const struct rank *cmd, double c, double cc, double cc_1,
353 int i, double w UNUSED)
368 rank = cc_1 + (c + 1.0)/ 2.0;
388 rank = cc_1 + c / 2.0 ;
403 rank_rfraction (const struct rank *cmd, double c, double cc, double cc_1,
406 return rank_rank (cmd, c, cc, cc_1, i, w) / w ;
411 rank_percent (const struct rank *cmd, double c, double cc, double cc_1,
414 return rank_rank (cmd, c, cc, cc_1, i, w) * 100.0 / w ;
419 rank_proportion (const struct rank *cmd, double c, double cc, double cc_1,
422 const double r = rank_rank (cmd, c, cc, cc_1, i, w) ;
426 switch (cmd->fraction)
429 f = (r - 3.0/8.0) / (w + 0.25);
435 f = (r - 1.0/3.0) / (w + 1.0/3.0);
445 return (f > 0) ? f : SYSMIS;
449 rank_normal (const struct rank *cmd, double c, double cc, double cc_1,
452 double f = rank_proportion (cmd, c, cc, cc_1, i, w);
454 return gsl_cdf_ugaussian_Pinv (f);
458 rank_ntiles (const struct rank *cmd, double c, double cc, double cc_1,
461 double r = rank_rank (cmd, c, cc, cc_1, i, w);
464 return (floor ((r * cmd->k_ntiles) / (w + 1)) + 1);
467 /* Expected value of the order statistics from an exponential distribution */
469 ee (int j, double w_star)
474 for (k = 1 ; k <= j; k++)
475 sum += 1.0 / (w_star + 1 - k);
482 rank_savage (const struct rank *cmd UNUSED, double c, double cc, double cc_1,
483 int i UNUSED, double w)
486 const int i_1 = floor (cc_1);
487 const int i_2 = floor (cc);
489 const double w_star = (modf (w, &int_part) == 0) ? w : floor (w) + 1;
491 const double g_1 = cc_1 - i_1;
492 const double g_2 = cc - i_2;
494 /* The second factor is infinite, when the first is zero.
495 Therefore, evaluate the second, only when the first is non-zero */
496 const double expr1 = (1 - g_1) ? (1 - g_1) * ee(i_1+1, w_star) : (1 - g_1);
497 const double expr2 = g_2 ? g_2 * ee (i_2+1, w_star) : g_2 ;
500 return ee (i_1 + 1, w_star) - 1;
503 return ((expr1 + expr2)/c) - 1;
509 for (j = i_1 + 2 ; j <= i_2; ++j)
510 sigma += ee (j, w_star);
511 return ((expr1 + expr2 + sigma) / c) -1;
518 sum_weights (const struct casereader *input, int weight_idx)
520 if (weight_idx == -1)
521 return casereader_count_cases (input);
524 struct casereader *pass;
529 pass = casereader_clone (input);
530 for (; (c = casereader_read (pass)) != NULL; case_unref (c))
531 w += case_num_idx (c, weight_idx);
532 casereader_destroy (pass);
539 rank_sorted_file (struct casereader *input,
540 struct casewriter *output,
542 const struct rank *cmd)
544 struct casegrouper *tie_grouper;
545 struct casereader *tied_cases;
546 struct subcase input_var;
552 /* Get total group weight. */
553 w = sum_weights (input, weight_idx);
556 subcase_init (&input_var, 0, 0, SC_ASCEND);
557 tie_grouper = casegrouper_create_subcase (input, &input_var);
558 subcase_destroy (&input_var);
559 for (; casegrouper_get_next_group (tie_grouper, &tied_cases);
560 casereader_destroy (tied_cases))
562 double tw = sum_weights (tied_cases, weight_idx);
566 taint_propagate (casereader_get_taint (tied_cases),
567 casewriter_get_taint (output));
569 /* Rank tied cases. */
570 for (; (c = casereader_read (tied_cases)) != NULL; case_unref (c))
572 struct ccase *out_case;
575 out_case = case_create (casewriter_get_proto (output));
576 *case_num_rw_idx (out_case, 0) = case_num_idx (c, 1);
577 for (i = 0; i < cmd->n_rs; ++i)
579 rank_function_t func = rank_func[cmd->rs[i].rfunc];
580 double rank = func (cmd, tw, cc, cc_1, tie_group, w);
581 *case_num_rw_idx (out_case, i + 1) = rank;
584 casewriter_write (output, out_case);
588 casegrouper_destroy (tie_grouper);
593 rank_cmd (struct dataset *ds, const struct rank *cmd);
596 fraction_name (const struct rank *cmd)
598 switch (cmd->fraction)
600 case FRAC_BLOM: return "BLOM";
601 case FRAC_RANKIT: return "RANKIT";
602 case FRAC_TUKEY: return "TUKEY";
603 case FRAC_VW: return "VW";
604 default: NOT_REACHED ();
608 /* Returns a label for a variable derived from SRC_VAR with function F. */
610 create_var_label (struct rank *cmd, const struct variable *src_var,
614 const char *pool_label;
616 ds_init_empty (&label);
618 if (cmd->n_group_vars > 0)
620 struct string group_var_str;
623 ds_init_empty (&group_var_str);
625 for (g = 0 ; g < cmd->n_group_vars ; ++g)
627 if (g > 0) ds_put_cstr (&group_var_str, " ");
628 ds_put_cstr (&group_var_str, var_get_name (cmd->group_vars[g]));
631 ds_put_format (&label, _("%s of %s by %s"), function_name[f],
632 var_get_name (src_var), ds_cstr (&group_var_str));
633 ds_destroy (&group_var_str);
636 ds_put_format (&label, _("%s of %s"),
637 function_name[f], var_get_name (src_var));
639 pool_label = pool_strdup (cmd->pool, ds_cstr (&label));
647 cmd_rank (struct lexer *lexer, struct dataset *ds)
649 struct string_set new_names;
651 struct rank_spec *rs;
653 subcase_init_empty (&rank.sc);
657 rank.exclude = MV_ANY;
658 rank.n_group_vars = 0;
659 rank.group_vars = NULL;
660 rank.dict = dataset_dict (ds);
661 rank.ties = TIES_MEAN;
662 rank.fraction = FRAC_BLOM;
665 rank.pool = pool_create ();
667 string_set_init (&new_names);
669 if (lex_match_id (lexer, "VARIABLES"))
670 if (! lex_force_match (lexer, T_EQUALS))
673 if (!parse_sort_criteria (lexer, rank.dict,
678 rank.n_vars = rank.sc.n_fields;
680 if (lex_match (lexer, T_BY))
682 if (! parse_variables_const (lexer, rank.dict,
683 &rank.group_vars, &rank.n_group_vars,
684 PV_NO_DUPLICATE | PV_NO_SCRATCH))
689 while (lex_token (lexer) != T_ENDCMD)
691 if (! lex_force_match (lexer, T_SLASH))
693 if (lex_match_id (lexer, "TIES"))
695 if (! lex_force_match (lexer, T_EQUALS))
697 if (lex_match_id (lexer, "MEAN"))
699 rank.ties = TIES_MEAN;
701 else if (lex_match_id (lexer, "LOW"))
703 rank.ties = TIES_LOW;
705 else if (lex_match_id (lexer, "HIGH"))
707 rank.ties = TIES_HIGH;
709 else if (lex_match_id (lexer, "CONDENSE"))
711 rank.ties = TIES_CONDENSE;
715 lex_error (lexer, NULL);
719 else if (lex_match_id (lexer, "FRACTION"))
721 if (! lex_force_match (lexer, T_EQUALS))
723 if (lex_match_id (lexer, "BLOM"))
725 rank.fraction = FRAC_BLOM;
727 else if (lex_match_id (lexer, "TUKEY"))
729 rank.fraction = FRAC_TUKEY;
731 else if (lex_match_id (lexer, "VW"))
733 rank.fraction = FRAC_VW;
735 else if (lex_match_id (lexer, "RANKIT"))
737 rank.fraction = FRAC_RANKIT;
741 lex_error (lexer, NULL);
745 else if (lex_match_id (lexer, "PRINT"))
747 if (! lex_force_match (lexer, T_EQUALS))
749 if (lex_match_id (lexer, "YES"))
753 else if (lex_match_id (lexer, "NO"))
759 lex_error (lexer, NULL);
763 else if (lex_match_id (lexer, "MISSING"))
765 if (! lex_force_match (lexer, T_EQUALS))
767 if (lex_match_id (lexer, "INCLUDE"))
769 rank.exclude = MV_SYSTEM;
771 else if (lex_match_id (lexer, "EXCLUDE"))
773 rank.exclude = MV_ANY;
777 lex_error (lexer, NULL);
781 else if (! parse_into (lexer, &rank, &new_names))
786 /* If no rank specs are given, then apply a default */
789 struct rank_spec *rs;
791 rs = pool_calloc (rank.pool, 1, sizeof *rs);
793 rs->dest_names = pool_calloc (rank.pool, rank.n_vars,
794 sizeof *rs->dest_names);
800 /* Choose variable names for all rank destinations which haven't already been
801 created with INTO. */
802 for (rs = rank.rs; rs < &rank.rs[rank.n_rs]; rs++)
804 rs->dest_labels = pool_calloc (rank.pool, rank.n_vars,
805 sizeof *rs->dest_labels);
806 for (int v = 0 ; v < rank.n_vars ; v ++)
808 const char **dst_name = &rs->dest_names[v];
809 if (*dst_name == NULL)
811 *dst_name = rank_choose_dest_name (rank.dict, &new_names,
813 var_get_name (rank.vars[v]));
814 if (*dst_name == NULL)
818 rs->dest_labels[v] = create_var_label (&rank, rank.vars[v],
825 struct pivot_table *table = pivot_table_create (
826 N_("Variables Created by RANK"));
828 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("New Variable"),
829 N_("New Variable"), N_("Function"),
830 N_("Fraction"), N_("Grouping Variables"));
832 struct pivot_dimension *variables = pivot_dimension_create (
833 table, PIVOT_AXIS_ROW, N_("Existing Variable"),
834 N_("Existing Variable"));
835 variables->root->show_label = true;
837 for (size_t i = 0 ; i < rank.n_rs ; ++i)
839 for (size_t v = 0 ; v < rank.n_vars ; v ++)
841 int row_idx = pivot_category_create_leaf (
842 variables->root, pivot_value_new_variable (rank.vars[v]));
844 struct string group_vars = DS_EMPTY_INITIALIZER;
845 for (int g = 0 ; g < rank.n_group_vars ; ++g)
848 ds_put_byte (&group_vars, ' ');
849 ds_put_cstr (&group_vars, var_get_name (rank.group_vars[g]));
852 enum rank_func rfunc = rank.rs[i].rfunc;
853 bool has_fraction = rfunc == NORMAL || rfunc == PROPORTION;
854 const char *entries[] =
856 rank.rs[i].dest_names[v],
857 function_name[rank.rs[i].rfunc],
858 has_fraction ? fraction_name (&rank) : NULL,
859 rank.n_group_vars ? ds_cstr (&group_vars) : NULL,
861 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
863 const char *entry = entries[j];
865 pivot_table_put2 (table, j, row_idx,
866 pivot_value_new_user_text (entry, -1));
868 ds_destroy (&group_vars);
872 pivot_table_submit (table);
876 rank_cmd (ds, &rank);
878 destroy_rank (&rank);
879 string_set_destroy (&new_names);
884 destroy_rank (&rank);
885 string_set_destroy (&new_names);
889 /* RANK transformation. */
894 struct rank_trns_input_var *input_vars;
900 struct rank_trns_input_var
902 struct casereader *input;
903 struct ccase *current;
905 struct variable **output_vars;
909 advance_ranking (struct rank_trns_input_var *iv)
911 case_unref (iv->current);
912 iv->current = casereader_read (iv->input);
915 static enum trns_result
916 rank_trns_proc (void *trns_, struct ccase **c, casenumber case_idx UNUSED)
918 struct rank_trns *trns = trns_;
919 double order = case_num_idx (*c, trns->order_case_idx);
920 struct rank_trns_input_var *iv;
922 *c = case_unshare (*c);
923 for (iv = trns->input_vars; iv < &trns->input_vars[trns->n_input_vars]; iv++)
924 while (iv->current != NULL)
926 double iv_order = case_num_idx (iv->current, 0);
927 if (iv_order == order)
931 for (i = 0; i < trns->n_funcs; i++)
932 *case_num_rw (*c, iv->output_vars[i])
933 = case_num_idx (iv->current, i + 1);
934 advance_ranking (iv);
937 else if (iv_order > order)
940 advance_ranking (iv);
942 return TRNS_CONTINUE;
946 rank_trns_free (void *trns_)
948 struct rank_trns *trns = trns_;
949 struct rank_trns_input_var *iv;
951 for (iv = trns->input_vars; iv < &trns->input_vars[trns->n_input_vars]; iv++)
953 casereader_destroy (iv->input);
954 case_unref (iv->current);
956 free (iv->output_vars);
958 free (trns->input_vars);
964 static const struct trns_class rank_trns_class = {
966 .execute = rank_trns_proc,
967 .destroy = rank_trns_free,
971 rank_cmd (struct dataset *ds, const struct rank *cmd)
973 struct dictionary *d = dataset_dict (ds);
974 struct variable *weight_var = dict_get_weight (d);
975 struct casewriter **outputs;
976 struct variable *order_var;
977 struct casereader *input;
978 struct rank_trns *trns;
982 order_var = add_permanent_ordering_transformation (ds);
984 /* Create output files. */
986 struct caseproto *output_proto;
987 struct subcase by_order;
989 output_proto = caseproto_create ();
990 for (i = 0; i < cmd->n_rs + 1; i++)
991 output_proto = caseproto_add_width (output_proto, 0);
993 subcase_init (&by_order, 0, 0, SC_ASCEND);
995 outputs = xnmalloc (cmd->n_vars, sizeof *outputs);
996 for (i = 0; i < cmd->n_vars; i++)
997 outputs[i] = sort_create_writer (&by_order, output_proto);
999 subcase_destroy (&by_order);
1000 caseproto_unref (output_proto);
1003 /* Open the active file and make one pass per input variable. */
1004 input = proc_open (ds);
1005 input = casereader_create_filter_weight (input, d, NULL, NULL);
1006 for (i = 0 ; i < cmd->n_vars ; ++i)
1008 const struct variable *input_var = cmd->vars[i];
1009 struct casereader *input_pass;
1010 struct casegrouper *split_grouper;
1011 struct casereader *split_group;
1012 struct subcase rank_ordering;
1013 struct subcase projection;
1014 struct subcase split_vars;
1015 struct subcase group_vars;
1019 /* Discard cases that have missing values of input variable. */
1020 input_pass = i == cmd->n_vars - 1 ? input : casereader_clone (input);
1021 input_pass = casereader_create_filter_missing (input_pass, &input_var, 1,
1022 cmd->exclude, NULL, NULL);
1024 /* Keep only the columns we really need, to save time and space when we
1025 sort them just below.
1027 After this projection, the input_pass case indexes look like:
1031 - 2 and up: cmd->n_group_vars group variables
1032 - 2 + cmd->n_group_vars and up: split variables
1033 - 2 + cmd->n_group_vars + n_split_vars: weight var
1035 subcase_init_empty (&projection);
1036 subcase_add_var_always (&projection, input_var, SC_ASCEND);
1037 subcase_add_var_always (&projection, order_var, SC_ASCEND);
1038 subcase_add_vars_always (&projection,
1039 cmd->group_vars, cmd->n_group_vars);
1040 subcase_add_vars_always (&projection, dict_get_split_vars (d),
1041 dict_get_n_splits (d));
1042 if (weight_var != NULL)
1044 subcase_add_var_always (&projection, weight_var, SC_ASCEND);
1045 weight_idx = 2 + cmd->n_group_vars + dict_get_n_splits (d);
1049 input_pass = casereader_project (input_pass, &projection);
1050 subcase_destroy (&projection);
1052 /* Prepare 'group_vars' as the set of grouping variables. */
1053 subcase_init_empty (&group_vars);
1054 for (j = 0; j < cmd->n_group_vars; j++)
1055 subcase_add_always (&group_vars,
1056 j + 2, var_get_width (cmd->group_vars[j]),
1059 /* Prepare 'rank_ordering' for sorting with the group variables as
1060 primary key and the input variable as secondary key. */
1061 subcase_clone (&rank_ordering, &group_vars);
1062 subcase_add (&rank_ordering, 0, 0, subcase_get_direction (&cmd->sc, i));
1064 /* Group by split variables */
1065 subcase_init_empty (&split_vars);
1066 for (j = 0; j < dict_get_n_splits (d); j++)
1067 subcase_add_always (&split_vars, 2 + j + cmd->n_group_vars,
1068 var_get_width (dict_get_split_vars (d)[j]),
1070 split_grouper = casegrouper_create_subcase (input_pass, &split_vars);
1071 subcase_destroy (&split_vars);
1072 while (casegrouper_get_next_group (split_grouper, &split_group))
1074 struct casereader *ordered;
1075 struct casegrouper *by_grouper;
1076 struct casereader *by_group;
1078 ordered = sort_execute (split_group, &rank_ordering);
1079 by_grouper = casegrouper_create_subcase (ordered, &group_vars);
1080 while (casegrouper_get_next_group (by_grouper, &by_group))
1081 rank_sorted_file (by_group, outputs[i], weight_idx, cmd);
1082 ok = casegrouper_destroy (by_grouper) && ok;
1084 subcase_destroy (&group_vars);
1085 subcase_destroy (&rank_ordering);
1087 ok = casegrouper_destroy (split_grouper) && ok;
1089 ok = proc_commit (ds) && ok;
1091 /* Re-fetch the dictionary and order variable, because if TEMPORARY was in
1092 effect then there's a new dictionary. */
1093 d = dataset_dict (ds);
1094 order_var = dict_lookup_var_assert (d, "$ORDER");
1096 /* Merge the original data set with the ranks (which we already sorted on
1098 trns = xmalloc (sizeof *trns);
1099 trns->order_case_idx = var_get_case_index (order_var);
1100 trns->input_vars = xnmalloc (cmd->n_vars, sizeof *trns->input_vars);
1101 trns->n_input_vars = cmd->n_vars;
1102 trns->n_funcs = cmd->n_rs;
1103 for (i = 0; i < trns->n_input_vars; i++)
1105 struct rank_trns_input_var *iv = &trns->input_vars[i];
1108 iv->input = casewriter_make_reader (outputs[i]);
1109 iv->current = casereader_read (iv->input);
1110 iv->output_vars = xnmalloc (trns->n_funcs, sizeof *iv->output_vars);
1111 for (j = 0; j < trns->n_funcs; j++)
1113 struct rank_spec *rs = &cmd->rs[j];
1114 struct variable *var;
1116 var = dict_create_var_assert (d, rs->dest_names[i], 0);
1117 var_set_both_formats (var, &dest_format[rs->rfunc]);
1118 var_set_label (var, rs->dest_labels[i]);
1120 iv->output_vars[j] = var;
1125 add_transformation (ds, &rank_trns_class, trns);
1127 /* Delete our sort key, which we don't need anymore. */
1128 dict_delete_var (d, order_var);