Get rid of vestigial mrset support.
[pspp] / src / language / stats / ctables.c
index 418496df928711a23b063690ff2c0cf876fffaf5..68239b85b9685f7495ca8a8c39f2038e82066126 100644 (file)
@@ -115,8 +115,8 @@ enum ctables_vlabel
     S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
     S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
     S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
-                                                                        \
-    /* Multiple response sets. */                                       \
+
+#if 0         /* Multiple response sets not yet implemented. */
   S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
     S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
     S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
@@ -139,6 +139,7 @@ enum ctables_vlabel
     S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
     S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
     S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
+#endif
 
 enum ctables_summary_function
   {
@@ -179,9 +180,11 @@ struct ctables_domain
     const struct ctables_cell *example;
 
     double d_valid;             /* Dictionary weight. */
-    double d_missing;
+    double d_count;
+    double d_total;
     double e_valid;             /* Effective weight */
-    double e_missing;
+    double e_count;
+    double e_total;
   };
 
 enum ctables_summary_variant
@@ -198,10 +201,11 @@ struct ctables_cell
     struct hmap_node node;
 
     /* The domains that contain this cell. */
-    bool contributes_to_domains;
+    uint32_t omit_domains;
     struct ctables_domain *domains[N_CTDTS];
 
     bool hide;
+
     bool postcompute;
     enum ctables_summary_variant sv;
 
@@ -218,6 +222,8 @@ struct ctables_cell
     axes[PIVOT_N_AXES];
 
     union ctables_summary *summaries;
+
+    //char *name;
   };
 
 struct ctables
@@ -336,7 +342,21 @@ struct ctables_summary_spec_set
     size_t n;
     size_t allocated;
 
+    /* The variable to which the summary specs are applied. */
     struct variable *var;
+
+    /* Whether the variable to which the summary specs are applied is a scale
+       variable for the purpose of summarization.
+
+       (VALIDN and TOTALN act differently for summarizing scale and categorical
+       variables.) */
+    bool is_scale;
+
+    /* If any of these optional additional scale variables are missing, then
+       treat 'var' as if it's missing too.  This is for implementing
+       SMISSING=LISTWISE. */
+    struct variable **listwise_vars;
+    size_t n_listwise_vars;
   };
 
 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
@@ -351,6 +371,7 @@ struct ctables_nest
     size_t scale_idx;
     size_t *domains[N_CTDTS];
     size_t n_domains[N_CTDTS];
+    size_t group_head;
 
     struct ctables_summary_spec_set specs[N_CSVS];
   };
@@ -428,30 +449,6 @@ struct ctables_table
     struct ctables_pairwise *pairwise;
   };
 
-struct ctables_var
-  {
-    bool is_mrset;
-    union
-      {
-        struct variable *var;
-        const struct mrset *mrset;
-      };
-  };
-
-static const struct fmt_spec *
-ctables_var_get_print_format (const struct ctables_var *var)
-{
-  return (var->is_mrset
-          ? var_get_print_format (var->mrset->vars[0])
-          : var_get_print_format (var->var));
-}
-
-static const char *
-ctables_var_name (const struct ctables_var *var)
-{
-  return var->is_mrset ? var->mrset->name : var_get_name (var->var);
-}
-
 struct ctables_categories
   {
     size_t n_refs;
@@ -480,6 +477,9 @@ struct ctables_category
         CCT_VALUE,
         CCT_LABEL,
         CCT_FUNCTION,
+
+        /* For contributing to TOTALN. */
+        CCT_EXCLUDED_MISSING,
       }
     type;
 
@@ -490,7 +490,7 @@ struct ctables_category
     union
       {
         double number;          /* CCT_NUMBER. */
-        char *string;           /* CCT_STRING. */
+        char *string;           /* CCT_STRING.  In dictionary encoding. */
         double range[2];        /* CCT_RANGE. */
 
         struct
@@ -515,7 +515,7 @@ struct ctables_category
       };
 
     /* Source location.  This is null for CCT_TOTAL, CCT_VALUE, CCT_LABEL,
-       CCT_FUNCTION. */
+       CCT_FUNCTION, CCT_EXCLUDED_MISSING. */
     struct msg_location *location;
   };
 
@@ -547,6 +547,9 @@ ctables_category_uninit (struct ctables_category *cat)
     case CCT_LABEL:
     case CCT_FUNCTION:
       break;
+
+    case CCT_EXCLUDED_MISSING:
+      break;
     }
 }
 
@@ -587,6 +590,9 @@ ctables_category_equal (const struct ctables_category *a,
               && a->sort_function == b->sort_function
               && a->sort_var == b->sort_var
               && a->percentile == b->percentile);
+
+    case CCT_EXCLUDED_MISSING:
+      return true;
     }
 
   NOT_REACHED ();
@@ -662,7 +668,7 @@ struct ctables_axis
         /* Terminals. */
         struct
           {
-            struct ctables_var var;
+            struct variable *var;
             bool scale;
             struct ctables_summary_spec_set specs[N_CSVS];
           };
@@ -729,7 +735,8 @@ ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
     .specs = specs,
     .n = src->n,
     .allocated = src->n,
-    .var = src->var
+    .var = src->var,
+    .is_scale = src->is_scale,
   };
 }
 
@@ -788,32 +795,59 @@ ctables_function_availability (enum ctables_summary_function f)
 static bool
 ctables_summary_function_is_count (enum ctables_summary_function f)
 {
-  static const bool is_count[N_CTSF_FUNCTIONS] = {
-    [CTSF_COUNT] = true,
-    [CTSF_ECOUNT] = true,
-    [CTSF_ROWPCT_COUNT] = true,
-    [CTSF_COLPCT_COUNT] = true,
-    [CTSF_TABLEPCT_COUNT] = true,
-    [CTSF_SUBTABLEPCT_COUNT] = true,
-    [CTSF_LAYERPCT_COUNT] = true,
-    [CTSF_LAYERROWPCT_COUNT] = true,
-    [CTSF_LAYERCOLPCT_COUNT] = true,
-    [CTSF_ROWPCT_RESPONSES_COUNT] = true,
-    [CTSF_COLPCT_RESPONSES_COUNT] = true,
-    [CTSF_TABLEPCT_RESPONSES_COUNT] = true,
-    [CTSF_SUBTABLEPCT_RESPONSES_COUNT] = true,
-    [CTSF_LAYERPCT_RESPONSES_COUNT] = true,
-    [CTSF_LAYERROWPCT_RESPONSES_COUNT] = true,
-    [CTSF_LAYERCOLPCT_RESPONSES_COUNT] = true,
-    [CTSF_ROWPCT_COUNT_RESPONSES] = true,
-    [CTSF_COLPCT_COUNT_RESPONSES] = true,
-    [CTSF_TABLEPCT_COUNT_RESPONSES] = true,
-    [CTSF_SUBTABLEPCT_COUNT_RESPONSES] = true,
-    [CTSF_LAYERPCT_COUNT_RESPONSES] = true,
-    [CTSF_LAYERROWPCT_COUNT_RESPONSES] = true,
-    [CTSF_LAYERCOLPCT_COUNT_RESPONSES] = true,
-  };
-  return is_count[f];
+  switch (f)
+    {
+    case CTSF_COUNT:
+    case CTSF_ECOUNT:
+    case CTSF_ROWPCT_COUNT:
+    case CTSF_COLPCT_COUNT:
+    case CTSF_TABLEPCT_COUNT:
+    case CTSF_SUBTABLEPCT_COUNT:
+    case CTSF_LAYERPCT_COUNT:
+    case CTSF_LAYERROWPCT_COUNT:
+    case CTSF_LAYERCOLPCT_COUNT:
+      return true;
+
+    case CTSF_ROWPCT_VALIDN:
+    case CTSF_COLPCT_VALIDN:
+    case CTSF_TABLEPCT_VALIDN:
+    case CTSF_SUBTABLEPCT_VALIDN:
+    case CTSF_LAYERPCT_VALIDN:
+    case CTSF_LAYERROWPCT_VALIDN:
+    case CTSF_LAYERCOLPCT_VALIDN:
+    case CTSF_ROWPCT_TOTALN:
+    case CTSF_COLPCT_TOTALN:
+    case CTSF_TABLEPCT_TOTALN:
+    case CTSF_SUBTABLEPCT_TOTALN:
+    case CTSF_LAYERPCT_TOTALN:
+    case CTSF_LAYERROWPCT_TOTALN:
+    case CTSF_LAYERCOLPCT_TOTALN:
+    case CTSF_MAXIMUM:
+    case CTSF_MEAN:
+    case CTSF_MEDIAN:
+    case CTSF_MINIMUM:
+    case CTSF_MISSING:
+    case CTSF_MODE:
+    case CTSF_PTILE:
+    case CTSF_RANGE:
+    case CTSF_SEMEAN:
+    case CTSF_STDDEV:
+    case CTSF_SUM:
+    case CSTF_TOTALN:
+    case CTSF_ETOTALN:
+    case CTSF_VALIDN:
+    case CTSF_EVALIDN:
+    case CTSF_VARIANCE:
+    case CTSF_ROWPCT_SUM:
+    case CTSF_COLPCT_SUM:
+    case CTSF_TABLEPCT_SUM:
+    case CTSF_SUBTABLEPCT_SUM:
+    case CTSF_LAYERPCT_SUM:
+    case CTSF_LAYERROWPCT_SUM:
+    case CTSF_LAYERCOLPCT_SUM:
+      return false;
+  }
+  NOT_REACHED ();
 }
 
 
@@ -905,7 +939,7 @@ struct ctables_axis_parse_ctx
 
 static struct fmt_spec
 ctables_summary_default_format (enum ctables_summary_function function,
-                                const struct ctables_var *var)
+                                const struct variable *var)
 {
   static const enum ctables_format default_formats[] = {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
@@ -921,7 +955,7 @@ ctables_summary_default_format (enum ctables_summary_function function,
       return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
 
     case CTF_GENERAL:
-      return *ctables_var_get_print_format (var);
+      return *var_get_print_format (var);
 
     default:
       NOT_REACHED ();
@@ -964,21 +998,18 @@ add_summary_spec (struct ctables_axis *axis,
   if (axis->op == CTAO_VAR)
     {
       const char *function_name = ctables_summary_function_name (function);
-      const char *var_name = ctables_var_name (&axis->var);
+      const char *var_name = var_get_name (axis->var);
       switch (ctables_function_availability (function))
         {
         case CTFA_MRSETS:
-          if (!axis->var.is_mrset)
-            {
-              msg_at (SE, loc, _("Summary function %s applies only to multiple "
-                                 "response sets."), function_name);
-              msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
-                      var_name);
-              return false;
-            }
-          break;
+          msg_at (SE, loc, _("Summary function %s applies only to multiple "
+                             "response sets."), function_name);
+          msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
+                  var_name);
+          return false;
 
         case CTFA_SCALE:
+#if 0
           if (!axis->scale)
             {
               msg_at (SE, loc,
@@ -988,6 +1019,7 @@ add_summary_spec (struct ctables_axis *axis,
                       var_name);
               return false;
             }
+#endif
           break;
 
         case CTFA_ALL:
@@ -1005,7 +1037,7 @@ add_summary_spec (struct ctables_axis *axis,
         .percentile = percentile,
         .label = xstrdup (label),
         .format = (format ? *format
-                   : ctables_summary_default_format (function, &axis->var)),
+                   : ctables_summary_default_format (function, axis->var)),
         .is_ctables_format = is_ctables_format,
       };
       return true;
@@ -1023,35 +1055,6 @@ add_summary_spec (struct ctables_axis *axis,
 static struct ctables_axis *ctables_axis_parse_stack (
   struct ctables_axis_parse_ctx *);
 
-static bool
-ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
-                   struct ctables_var *var)
-{
-  if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
-    {
-      *var = (struct ctables_var) {
-        .is_mrset = true,
-        .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
-      };
-      if (!var->mrset)
-        {
-          lex_error (lexer, _("'%s' does not name a multiple-response set "
-                              "in the active file dictionary."),
-                     lex_tokcstr (lexer));
-          return false;
-        }
-      lex_get (lexer);
-      return true;
-    }
-  else
-    {
-      *var = (struct ctables_var) {
-        .is_mrset = false,
-        .var = parse_variable (lexer, dict),
-      };
-      return var->var != NULL;
-    }
-}
 
 static struct ctables_axis *
 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
@@ -1071,20 +1074,28 @@ ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
     return NULL;
 
   int start_ofs = lex_ofs (ctx->lexer);
-  struct ctables_var var;
-  if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
+  struct variable *var = parse_variable (ctx->lexer, ctx->dict);
+  if (!var)
     return NULL;
 
   struct ctables_axis *axis = xmalloc (sizeof *axis);
   *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
 
   /* XXX should figure out default measures by reading data */
-  axis->scale = (var.is_mrset ? false
-                 : lex_match_phrase (ctx->lexer, "[S]") ? true
+  axis->scale = (lex_match_phrase (ctx->lexer, "[S]") ? true
                  : lex_match_phrase (ctx->lexer, "[C]") ? false
-                 : var_get_measure (var.var) == MEASURE_SCALE);
+                 : var_get_measure (var) == MEASURE_SCALE);
   axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
                                 lex_ofs (ctx->lexer) - 1);
+  if (axis->scale && var_is_alpha (var))
+    {
+      msg_at (SE, axis->loc, _("Cannot use string variable %s as a scale "
+                               "variable."),
+              var_get_name (var));
+      ctables_axis_destroy (axis);
+      return NULL;
+    }
+
   return axis;
 }
 
@@ -1224,15 +1235,7 @@ find_scale (const struct ctables_axis *axis)
   if (!axis)
     return NULL;
   else if (axis->op == CTAO_VAR)
-    {
-      if (axis->scale)
-        {
-          assert (!axis->var.is_mrset);
-          return axis;
-        }
-      else
-        return NULL;
-    }
+    return axis->scale ? axis : NULL;
   else
     {
       for (size_t i = 0; i < 2; i++)
@@ -1896,6 +1899,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
         case CCT_VALUE:
         case CCT_LABEL:
         case CCT_FUNCTION:
+        case CCT_EXCLUDED_MISSING:
           break;
         }
     }
@@ -1977,13 +1981,39 @@ stack_fts (struct ctables_stack s0, struct ctables_stack s1)
   for (size_t i = 0; i < s0.n; i++)
     stack.nests[stack.n++] = s0.nests[i];
   for (size_t i = 0; i < s1.n; i++)
-    stack.nests[stack.n++] = s1.nests[i];
+    {
+      stack.nests[stack.n] = s1.nests[i];
+      stack.nests[stack.n].group_head += s0.n;
+      stack.n++;
+    }
   assert (stack.n == s0.n + s1.n);
   free (s0.nests);
   free (s1.nests);
   return stack;
 }
 
+static struct ctables_stack
+var_fts (const struct ctables_axis *a)
+{
+  struct variable **vars = xmalloc (sizeof *vars);
+  *vars = a->var;
+
+  struct ctables_nest *nest = xmalloc (sizeof *nest);
+  *nest = (struct ctables_nest) {
+    .vars = vars,
+    .n = 1,
+    .scale_idx = a->scale ? 0 : SIZE_MAX,
+  };
+  if (a->specs[CSV_CELL].n || a->scale)
+    for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
+      {
+        ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
+        nest->specs[sv].var = a->var;
+        nest->specs[sv].is_scale = a->scale;
+      }
+  return (struct ctables_stack) { .nests = nest, .n = 1 };
+}
+
 static struct ctables_stack
 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
 {
@@ -1993,30 +2023,15 @@ enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
   switch (a->op)
     {
     case CTAO_VAR:
-      assert (!a->var.is_mrset);
-
-      struct variable **vars = xmalloc (sizeof *vars);
-      *vars = a->var.var;
-
-      struct ctables_nest *nest = xmalloc (sizeof *nest);
-      *nest = (struct ctables_nest) {
-        .vars = vars,
-        .n = 1,
-        .scale_idx = a->scale ? 0 : SIZE_MAX,
-      };
-      if (a->specs[CSV_CELL].n || a->scale)
-        for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
-          {
-            ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
-            nest->specs[sv].var = a->var.var;
-          }
-      return (struct ctables_stack) { .nests = nest, .n = 1 };
+      return var_fts (a);
 
     case CTAO_STACK:
       return stack_fts (enumerate_fts (axis_type, a->subs[0]),
                         enumerate_fts (axis_type, a->subs[1]));
 
     case CTAO_NEST:
+      /* This should consider any of the scale variables found in the result to
+         be linked to each other listwise for SMISSING=LISTWISE. */
       return nest_fts (enumerate_fts (axis_type, a->subs[0]),
                        enumerate_fts (axis_type, a->subs[1]));
     }
@@ -2027,11 +2042,7 @@ enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
 union ctables_summary
   {
     /* COUNT, VALIDN, TOTALN. */
-    struct
-      {
-        double valid;
-        double missing;
-      };
+    double count;
 
     /* MINIMUM, MAXIMUM, RANGE. */
     struct
@@ -2088,7 +2099,7 @@ ctables_summary_init (union ctables_summary *s,
     case CTSF_ETOTALN:
     case CTSF_VALIDN:
     case CTSF_EVALIDN:
-      s->missing = s->valid = 0;
+      s->count = 0;
       break;
 
     case CTSF_MAXIMUM:
@@ -2130,30 +2141,6 @@ ctables_summary_init (union ctables_summary *s,
         s->ovalue = SYSMIS;
       }
       break;
-
-    case CTSF_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES:
-    case CTSF_COLPCT_RESPONSES:
-    case CTSF_TABLEPCT_RESPONSES:
-    case CTSF_SUBTABLEPCT_RESPONSES:
-    case CTSF_LAYERPCT_RESPONSES:
-    case CTSF_LAYERROWPCT_RESPONSES:
-    case CTSF_LAYERCOLPCT_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES_COUNT:
-    case CTSF_COLPCT_RESPONSES_COUNT:
-    case CTSF_TABLEPCT_RESPONSES_COUNT:
-    case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
-    case CTSF_LAYERPCT_RESPONSES_COUNT:
-    case CTSF_LAYERROWPCT_RESPONSES_COUNT:
-    case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
-    case CTSF_ROWPCT_COUNT_RESPONSES:
-    case CTSF_COLPCT_COUNT_RESPONSES:
-    case CTSF_TABLEPCT_COUNT_RESPONSES:
-    case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
-    case CTSF_LAYERPCT_COUNT_RESPONSES:
-    case CTSF_LAYERROWPCT_COUNT_RESPONSES:
-    case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
-      NOT_REACHED ();
     }
 }
 
@@ -2218,30 +2205,6 @@ ctables_summary_uninit (union ctables_summary *s,
     case CTSF_PTILE:
       casewriter_destroy (s->writer);
       break;
-
-    case CTSF_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES:
-    case CTSF_COLPCT_RESPONSES:
-    case CTSF_TABLEPCT_RESPONSES:
-    case CTSF_SUBTABLEPCT_RESPONSES:
-    case CTSF_LAYERPCT_RESPONSES:
-    case CTSF_LAYERROWPCT_RESPONSES:
-    case CTSF_LAYERCOLPCT_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES_COUNT:
-    case CTSF_COLPCT_RESPONSES_COUNT:
-    case CTSF_TABLEPCT_RESPONSES_COUNT:
-    case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
-    case CTSF_LAYERPCT_RESPONSES_COUNT:
-    case CTSF_LAYERROWPCT_RESPONSES_COUNT:
-    case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
-    case CTSF_ROWPCT_COUNT_RESPONSES:
-    case CTSF_COLPCT_COUNT_RESPONSES:
-    case CTSF_TABLEPCT_COUNT_RESPONSES:
-    case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
-    case CTSF_LAYERPCT_COUNT_RESPONSES:
-    case CTSF_LAYERROWPCT_COUNT_RESPONSES:
-    case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
-      NOT_REACHED ();
     }
 }
 
@@ -2249,20 +2212,42 @@ static void
 ctables_summary_add (union ctables_summary *s,
                      const struct ctables_summary_spec *ss,
                      const struct variable *var, const union value *value,
+                     bool is_scale, bool is_scale_missing,
+                     bool is_missing, bool excluded_missing,
                      double d_weight, double e_weight)
 {
+  /* To determine whether a case is included in a given table for a particular
+     kind of summary, consider the following charts for each variable in the
+     table.  Only if "yes" appears for every variable for the summary is the
+     case counted.
+
+     Categorical variables:                    VALIDN   COUNT   TOTALN
+       Valid values in included categories       yes     yes      yes
+       Missing values in included categories     ---     yes      yes
+       Missing values in excluded categories     ---     ---      yes
+       Valid values in excluded categories       ---     ---      ---
+
+     Scale variables:                          VALIDN   COUNT   TOTALN
+       Valid value                               yes     yes      yes
+       Missing value                             ---     yes      yes
+
+     Missing values include both user- and system-missing.  (The system-missing
+     value is always in an excluded category.)
+  */
   switch (ss->function)
     {
-    case CTSF_COUNT:
     case CSTF_TOTALN:
-    case CTSF_VALIDN:
-      if (var_is_value_missing (var, value))
-        s->missing += d_weight;
-      else
-        s->valid += d_weight;
+    case CTSF_ROWPCT_TOTALN:
+    case CTSF_COLPCT_TOTALN:
+    case CTSF_TABLEPCT_TOTALN:
+    case CTSF_SUBTABLEPCT_TOTALN:
+    case CTSF_LAYERPCT_TOTALN:
+    case CTSF_LAYERROWPCT_TOTALN:
+    case CTSF_LAYERCOLPCT_TOTALN:
+      s->count += d_weight;
       break;
 
-    case CTSF_ECOUNT:
+    case CTSF_COUNT:
     case CTSF_ROWPCT_COUNT:
     case CTSF_COLPCT_COUNT:
     case CTSF_TABLEPCT_COUNT:
@@ -2270,6 +2255,11 @@ ctables_summary_add (union ctables_summary *s,
     case CTSF_LAYERPCT_COUNT:
     case CTSF_LAYERROWPCT_COUNT:
     case CTSF_LAYERCOLPCT_COUNT:
+      if (is_scale || !excluded_missing)
+        s->count += d_weight;
+      break;
+
+    case CTSF_VALIDN:
     case CTSF_ROWPCT_VALIDN:
     case CTSF_COLPCT_VALIDN:
     case CTSF_TABLEPCT_VALIDN:
@@ -2277,26 +2267,37 @@ ctables_summary_add (union ctables_summary *s,
     case CTSF_LAYERPCT_VALIDN:
     case CTSF_LAYERROWPCT_VALIDN:
     case CTSF_LAYERCOLPCT_VALIDN:
-    case CTSF_ROWPCT_TOTALN:
-    case CTSF_COLPCT_TOTALN:
-    case CTSF_TABLEPCT_TOTALN:
-    case CTSF_SUBTABLEPCT_TOTALN:
-    case CTSF_LAYERPCT_TOTALN:
-    case CTSF_LAYERROWPCT_TOTALN:
-    case CTSF_LAYERCOLPCT_TOTALN:
+      if (is_scale
+          ? !is_scale_missing
+          : !is_missing)
+        s->count += d_weight;
+      break;
+
     case CTSF_MISSING:
-    case CTSF_ETOTALN:
+      if (is_missing)
+        s->count += d_weight;
+      break;
+
+    case CTSF_ECOUNT:
+      if (is_scale || !excluded_missing)
+        s->count += e_weight;
+      break;
+
     case CTSF_EVALIDN:
-      if (var_is_value_missing (var, value))
-        s->missing += e_weight;
-      else
-        s->valid += e_weight;
+      if (is_scale
+          ? !is_scale_missing
+          : !is_missing)
+        s->count += e_weight;
+      break;
+
+    case CTSF_ETOTALN:
+      s->count += e_weight;
       break;
 
     case CTSF_MAXIMUM:
     case CTSF_MINIMUM:
     case CTSF_RANGE:
-      if (!var_is_value_missing (var, value))
+      if (!is_scale_missing)
         {
           assert (!var_is_alpha (var)); /* XXX? */
           if (s->min == SYSMIS || value->f < s->min)
@@ -2318,14 +2319,14 @@ ctables_summary_add (union ctables_summary *s,
     case CTSF_LAYERPCT_SUM:
     case CTSF_LAYERROWPCT_SUM:
     case CTSF_LAYERCOLPCT_SUM:
-      if (!var_is_value_missing (var, value))
+      if (!is_scale_missing)
         moments1_add (s->moments, value->f, e_weight);
       break;
 
     case CTSF_MEDIAN:
     case CTSF_MODE:
     case CTSF_PTILE:
-      if (var_is_value_missing (var, value))
+      if (!is_scale_missing)
         {
           s->ovalid += e_weight;
 
@@ -2335,30 +2336,6 @@ ctables_summary_add (union ctables_summary *s,
           casewriter_write (s->writer, c);
         }
       break;
-
-    case CTSF_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES:
-    case CTSF_COLPCT_RESPONSES:
-    case CTSF_TABLEPCT_RESPONSES:
-    case CTSF_SUBTABLEPCT_RESPONSES:
-    case CTSF_LAYERPCT_RESPONSES:
-    case CTSF_LAYERROWPCT_RESPONSES:
-    case CTSF_LAYERCOLPCT_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES_COUNT:
-    case CTSF_COLPCT_RESPONSES_COUNT:
-    case CTSF_TABLEPCT_RESPONSES_COUNT:
-    case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
-    case CTSF_LAYERPCT_RESPONSES_COUNT:
-    case CTSF_LAYERROWPCT_RESPONSES_COUNT:
-    case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
-    case CTSF_ROWPCT_COUNT_RESPONSES:
-    case CTSF_COLPCT_COUNT_RESPONSES:
-    case CTSF_TABLEPCT_COUNT_RESPONSES:
-    case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
-    case CTSF_LAYERPCT_COUNT_RESPONSES:
-    case CTSF_LAYERROWPCT_COUNT_RESPONSES:
-    case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
-      NOT_REACHED ();
     }
 }
 
@@ -2385,67 +2362,45 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_MEDIAN:
     case CTSF_PTILE:
     case CTSF_MODE:
-    case CTSF_RESPONSES:
       NOT_REACHED ();
 
     case CTSF_COLPCT_COUNT:
-    case CTSF_COLPCT_COUNT_RESPONSES:
-    case CTSF_COLPCT_RESPONSES:
-    case CTSF_COLPCT_RESPONSES_COUNT:
     case CTSF_COLPCT_SUM:
     case CTSF_COLPCT_TOTALN:
     case CTSF_COLPCT_VALIDN:
       return CTDT_COL;
 
     case CTSF_LAYERCOLPCT_COUNT:
-    case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
-    case CTSF_LAYERCOLPCT_RESPONSES:
-    case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
     case CTSF_LAYERCOLPCT_SUM:
     case CTSF_LAYERCOLPCT_TOTALN:
     case CTSF_LAYERCOLPCT_VALIDN:
       return CTDT_LAYERCOL;
 
     case CTSF_LAYERPCT_COUNT:
-    case CTSF_LAYERPCT_COUNT_RESPONSES:
-    case CTSF_LAYERPCT_RESPONSES:
-    case CTSF_LAYERPCT_RESPONSES_COUNT:
     case CTSF_LAYERPCT_SUM:
     case CTSF_LAYERPCT_TOTALN:
     case CTSF_LAYERPCT_VALIDN:
       return CTDT_LAYER;
 
     case CTSF_LAYERROWPCT_COUNT:
-    case CTSF_LAYERROWPCT_COUNT_RESPONSES:
-    case CTSF_LAYERROWPCT_RESPONSES:
-    case CTSF_LAYERROWPCT_RESPONSES_COUNT:
     case CTSF_LAYERROWPCT_SUM:
     case CTSF_LAYERROWPCT_TOTALN:
     case CTSF_LAYERROWPCT_VALIDN:
       return CTDT_LAYERROW;
 
     case CTSF_ROWPCT_COUNT:
-    case CTSF_ROWPCT_COUNT_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES_COUNT:
     case CTSF_ROWPCT_SUM:
     case CTSF_ROWPCT_TOTALN:
     case CTSF_ROWPCT_VALIDN:
       return CTDT_ROW;
 
     case CTSF_SUBTABLEPCT_COUNT:
-    case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
-    case CTSF_SUBTABLEPCT_RESPONSES:
-    case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
     case CTSF_SUBTABLEPCT_SUM:
     case CTSF_SUBTABLEPCT_TOTALN:
     case CTSF_SUBTABLEPCT_VALIDN:
       return CTDT_SUBTABLE;
 
     case CTSF_TABLEPCT_COUNT:
-    case CTSF_TABLEPCT_COUNT_RESPONSES:
-    case CTSF_TABLEPCT_RESPONSES:
-    case CTSF_TABLEPCT_RESPONSES_COUNT:
     case CTSF_TABLEPCT_SUM:
     case CTSF_TABLEPCT_TOTALN:
     case CTSF_TABLEPCT_VALIDN:
@@ -2464,7 +2419,7 @@ ctables_summary_value (const struct ctables_cell *cell,
     {
     case CTSF_COUNT:
     case CTSF_ECOUNT:
-      return s->valid;
+      return s->count;
 
     case CTSF_ROWPCT_COUNT:
     case CTSF_COLPCT_COUNT:
@@ -2475,8 +2430,8 @@ ctables_summary_value (const struct ctables_cell *cell,
     case CTSF_LAYERCOLPCT_COUNT:
       {
         enum ctables_domain_type d = ctables_function_domain (ss->function);
-        return (cell->domains[d]->e_valid
-                ? s->valid / cell->domains[d]->e_valid * 100
+        return (cell->domains[d]->e_count
+                ? s->count / cell->domains[d]->e_count * 100
                 : SYSMIS);
       }
 
@@ -2487,6 +2442,13 @@ ctables_summary_value (const struct ctables_cell *cell,
     case CTSF_LAYERPCT_VALIDN:
     case CTSF_LAYERROWPCT_VALIDN:
     case CTSF_LAYERCOLPCT_VALIDN:
+      {
+        enum ctables_domain_type d = ctables_function_domain (ss->function);
+        return (cell->domains[d]->e_valid
+                ? s->count / cell->domains[d]->e_valid * 100
+                : SYSMIS);
+      }
+
     case CTSF_ROWPCT_TOTALN:
     case CTSF_COLPCT_TOTALN:
     case CTSF_TABLEPCT_TOTALN:
@@ -2494,18 +2456,25 @@ ctables_summary_value (const struct ctables_cell *cell,
     case CTSF_LAYERPCT_TOTALN:
     case CTSF_LAYERROWPCT_TOTALN:
     case CTSF_LAYERCOLPCT_TOTALN:
-      NOT_REACHED ();
+      {
+        enum ctables_domain_type d = ctables_function_domain (ss->function);
+        return (cell->domains[d]->e_total
+                ? s->count / cell->domains[d]->e_total * 100
+                : SYSMIS);
+      }
 
     case CTSF_MISSING:
-      return s->missing;
+      return s->count;
 
     case CSTF_TOTALN:
     case CTSF_ETOTALN:
-      return s->valid + s->missing;
+      return s->count;
 
     case CTSF_VALIDN:
+      return s->count;
+
     case CTSF_EVALIDN:
-      return s->valid;
+      return s->count;
 
     case CTSF_MAXIMUM:
       return s->max;
@@ -2589,30 +2558,6 @@ ctables_summary_value (const struct ctables_cell *cell,
           statistic_destroy (&mode->parent.parent);
         }
       return s->ovalue;
-
-    case CTSF_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES:
-    case CTSF_COLPCT_RESPONSES:
-    case CTSF_TABLEPCT_RESPONSES:
-    case CTSF_SUBTABLEPCT_RESPONSES:
-    case CTSF_LAYERPCT_RESPONSES:
-    case CTSF_LAYERROWPCT_RESPONSES:
-    case CTSF_LAYERCOLPCT_RESPONSES:
-    case CTSF_ROWPCT_RESPONSES_COUNT:
-    case CTSF_COLPCT_RESPONSES_COUNT:
-    case CTSF_TABLEPCT_RESPONSES_COUNT:
-    case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
-    case CTSF_LAYERPCT_RESPONSES_COUNT:
-    case CTSF_LAYERROWPCT_RESPONSES_COUNT:
-    case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
-    case CTSF_ROWPCT_COUNT_RESPONSES:
-    case CTSF_COLPCT_COUNT_RESPONSES:
-    case CTSF_TABLEPCT_COUNT_RESPONSES:
-    case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
-    case CTSF_LAYERPCT_COUNT_RESPONSES:
-    case CTSF_LAYERROWPCT_COUNT_RESPONSES:
-    case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
-      NOT_REACHED ();
     }
 
   NOT_REACHED ();
@@ -2652,6 +2597,7 @@ ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
           case CCT_SUBTOTAL:
           case CCT_TOTAL:
           case CCT_POSTCOMPUTE:
+          case CCT_EXCLUDED_MISSING:
             /* Must be equal. */
             continue;
 
@@ -2722,8 +2668,13 @@ ctables_domain_insert (struct ctables_section *s, struct ctables_cell *cell,
       for (size_t i = 0; i < nest->n_domains[domain]; i++)
         {
           size_t v_idx = nest->domains[domain][i];
-          hash = value_hash (&cell->axes[a].cvs[v_idx].value,
-                             var_get_width (nest->vars[v_idx]), hash);
+          struct ctables_cell_value *cv = &cell->axes[a].cvs[v_idx];
+          hash = hash_pointer (cv->category, hash);
+          if (cv->category->type != CCT_TOTAL
+              && cv->category->type != CCT_SUBTOTAL
+              && cv->category->type != CCT_POSTCOMPUTE)
+            hash = value_hash (&cv->value,
+                               var_get_width (nest->vars[v_idx]), hash);
         }
     }
 
@@ -2737,9 +2688,14 @@ ctables_domain_insert (struct ctables_section *s, struct ctables_cell *cell,
           for (size_t i = 0; i < nest->n_domains[domain]; i++)
             {
               size_t v_idx = nest->domains[domain][i];
-              if (!value_equal (&df->axes[a].cvs[v_idx].value,
-                                &cell->axes[a].cvs[v_idx].value,
-                                var_get_width (nest->vars[v_idx])))
+              struct ctables_cell_value *cv1 = &df->axes[a].cvs[v_idx];
+              struct ctables_cell_value *cv2 = &cell->axes[a].cvs[v_idx];
+              if (cv1->category != cv2->category
+                  || (cv1->category->type != CCT_TOTAL
+                      && cv1->category->type != CCT_SUBTOTAL
+                      && cv1->category->type != CCT_POSTCOMPUTE
+                      && !value_equal (&cv1->value, &cv2->value,
+                                       var_get_width (nest->vars[v_idx]))))
                 goto not_equal;
             }
         }
@@ -2758,6 +2714,9 @@ static const struct ctables_category *
 ctables_categories_match (const struct ctables_categories *c,
                           const union value *v, const struct variable *var)
 {
+  if (var_is_numeric (var) && v->f == SYSMIS)
+    return NULL;
+
   const struct ctables_category *othernm = NULL;
   for (size_t i = c->n_cats; i-- > 0; )
     {
@@ -2800,6 +2759,9 @@ ctables_categories_match (const struct ctables_categories *c,
         case CCT_FUNCTION:
           return (cat->include_missing || !var_is_value_missing (var, v) ? cat
                   : NULL);
+
+        case CCT_EXCLUDED_MISSING:
+          break;
         }
     }
 
@@ -2865,17 +2827,20 @@ ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
   cell = xmalloc (sizeof *cell);
   cell->hide = false;
   cell->sv = sv;
-  cell->contributes_to_domains = true;
+  cell->omit_domains = 0;
   cell->postcompute = false;
+  //struct string name = DS_EMPTY_INITIALIZER;
   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
     {
       const struct ctables_nest *nest = s->nests[a];
       cell->axes[a].cvs = (nest->n
-                        ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
-                        : NULL);
+                           ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
+                           : NULL);
       for (size_t i = 0; i < nest->n; i++)
         {
           const struct ctables_category *cat = cats[a][i];
+          const struct variable *var = nest->vars[i];
+          const union value *value = case_data (c, var);
           if (i != nest->scale_idx)
             {
               const struct ctables_category *subtotal = cat->subtotal;
@@ -2885,16 +2850,59 @@ ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
               if (cat->type == CCT_TOTAL
                   || cat->type == CCT_SUBTOTAL
                   || cat->type == CCT_POSTCOMPUTE)
-                cell->contributes_to_domains = false;
+                {
+                  /* XXX these should be more encompassing I think.*/
+
+                  switch (a)
+                    {
+                    case PIVOT_AXIS_COLUMN:
+                      cell->omit_domains |= ((1u << CTDT_TABLE) |
+                                             (1u << CTDT_LAYER) |
+                                             (1u << CTDT_LAYERCOL) |
+                                             (1u << CTDT_SUBTABLE) |
+                                             (1u << CTDT_COL));
+                      break;
+                    case PIVOT_AXIS_ROW:
+                      cell->omit_domains |= ((1u << CTDT_TABLE) |
+                                             (1u << CTDT_LAYER) |
+                                             (1u << CTDT_LAYERROW) |
+                                             (1u << CTDT_SUBTABLE) |
+                                             (1u << CTDT_ROW));
+                      break;
+                    case PIVOT_AXIS_LAYER:
+                      cell->omit_domains |= ((1u << CTDT_TABLE) |
+                                             (1u << CTDT_LAYER));
+                      break;
+                    }
+                }
               if (cat->type == CCT_POSTCOMPUTE)
                 cell->postcompute = true;
             }
 
           cell->axes[a].cvs[i].category = cat;
-          value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
-                       var_get_width (nest->vars[i]));
+          value_clone (&cell->axes[a].cvs[i].value, value, var_get_width (var));
+
+#if 0
+          if (i != nest->scale_idx)
+            {
+              if (!ds_is_empty (&name))
+                ds_put_cstr (&name, ", ");
+              char *value_s = data_out (value, var_get_encoding (var),
+                                        var_get_print_format (var),
+                                        settings_get_fmt_settings ());
+              if (cat->type == CCT_TOTAL
+                  || cat->type == CCT_SUBTOTAL
+                  || cat->type == CCT_POSTCOMPUTE)
+                ds_put_format (&name, "%s=total", var_get_name (var));
+              else
+                ds_put_format (&name, "%s=%s", var_get_name (var),
+                               value_s + strspn (value_s, " "));
+              free (value_s);
+            }
+#endif
         }
     }
+  //cell->name = ds_steal_cstr (&name);
 
   const struct ctables_nest *ss = s->nests[s->table->summary_axis];
   const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
@@ -2907,31 +2915,66 @@ ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
   return cell;
 }
 
+static bool
+is_scale_missing (const struct ctables_summary_spec_set *specs,
+                  const struct ccase *c)
+{
+  if (!specs->is_scale)
+    return false;
+
+  if (var_is_num_missing (specs->var, case_num (c, specs->var)))
+    return true;
+
+  for (size_t i = 0; i < specs->n_listwise_vars; i++)
+    {
+      const struct variable *var = specs->listwise_vars[i];
+      if (var_is_num_missing (var, case_num (c, var)))
+        return true;
+    }
+
+  return false;
+}
+
 static void
 ctables_cell_add__ (struct ctables_section *s, const struct ccase *c,
                     const struct ctables_category *cats[PIVOT_N_AXES][10],
+                    bool is_missing, bool excluded_missing,
                     double d_weight, double e_weight)
 {
   struct ctables_cell *cell = ctables_cell_insert__ (s, c, cats);
   const struct ctables_nest *ss = s->nests[s->table->summary_axis];
 
   const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
+
+  bool scale_missing = is_scale_missing (specs, c);
   for (size_t i = 0; i < specs->n; i++)
-    ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
-                         case_data (c, specs->var), d_weight, e_weight);
-  if (cell->contributes_to_domains)
-    {
-      for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
-        {
-          cell->domains[dt]->d_valid += d_weight;
-          cell->domains[dt]->e_valid += e_weight;
-        }
-    }
+    ctables_summary_add (&cell->summaries[i], &specs->specs[i],
+                         specs->var, case_data (c, specs->var), specs->is_scale,
+                         scale_missing, is_missing, excluded_missing,
+                         d_weight, e_weight);
+  for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+    if (!(cell->omit_domains && (1u << dt)))
+      {
+        struct ctables_domain *d = cell->domains[dt];
+        d->d_total += d_weight;
+        d->e_total += e_weight;
+        if (!excluded_missing)
+          {
+            d->d_count += d_weight;
+            d->e_count += e_weight;
+          }
+        if (!is_missing)
+          {
+            d->d_valid += d_weight;
+            d->e_valid += e_weight;
+          }
+      }
 }
 
 static void
 recurse_totals (struct ctables_section *s, const struct ccase *c,
                 const struct ctables_category *cats[PIVOT_N_AXES][10],
+                bool is_missing, bool excluded_missing,
                 double d_weight, double e_weight,
                 enum pivot_axis_type start_axis, size_t start_nest)
 {
@@ -2951,8 +2994,10 @@ recurse_totals (struct ctables_section *s, const struct ccase *c,
             {
               const struct ctables_category *save = cats[a][i];
               cats[a][i] = total;
-              ctables_cell_add__ (s, c, cats, d_weight, e_weight);
-              recurse_totals (s, c, cats, d_weight, e_weight, a, i + 1);
+              ctables_cell_add__ (s, c, cats, is_missing, excluded_missing,
+                                  d_weight, e_weight);
+              recurse_totals (s, c, cats, is_missing, excluded_missing,
+                              d_weight, e_weight, a, i + 1);
               cats[a][i] = save;
             }
         }
@@ -2963,6 +3008,7 @@ recurse_totals (struct ctables_section *s, const struct ccase *c,
 static void
 recurse_subtotals (struct ctables_section *s, const struct ccase *c,
                    const struct ctables_category *cats[PIVOT_N_AXES][10],
+                   bool is_missing, bool excluded_missing,
                    double d_weight, double e_weight,
                    enum pivot_axis_type start_axis, size_t start_nest)
 {
@@ -2978,8 +3024,10 @@ recurse_subtotals (struct ctables_section *s, const struct ccase *c,
           if (save->subtotal)
             {
               cats[a][i] = save->subtotal;
-              ctables_cell_add__ (s, c, cats, d_weight, e_weight);
-              recurse_subtotals (s, c, cats, d_weight, e_weight, a, i + 1);
+              ctables_cell_add__ (s, c, cats, is_missing, excluded_missing,
+                                  d_weight, e_weight);
+              recurse_subtotals (s, c, cats, is_missing, excluded_missing,
+                                 d_weight, e_weight, a, i + 1);
               cats[a][i] = save;
             }
         }
@@ -3012,6 +3060,15 @@ ctables_cell_insert (struct ctables_section *s,
                      double d_weight, double e_weight)
 {
   const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */
+
+  /* Does at least one categorical variable have a missing value in an included
+     or excluded category? */
+  bool is_missing = false;
+
+  /* Does at least one categorical variable have a missing value in an excluded
+     category? */
+  bool excluded_missing = false;
+
   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
     {
       const struct ctables_nest *nest = s->nests[a];
@@ -3023,32 +3080,50 @@ ctables_cell_insert (struct ctables_section *s,
           const struct variable *var = nest->vars[i];
           const union value *value = case_data (c, var);
 
-          if (var_is_numeric (var) && value->f == SYSMIS)
-            return;
+          bool var_missing = var_is_value_missing (var, value) != 0;
+          if (var_missing)
+            is_missing = true;
 
           cats[a][i] = ctables_categories_match (
             s->table->categories[var_get_dict_index (var)], value, var);
           if (!cats[a][i])
-            return;
+            {
+              if (!var_missing)
+                return;
+
+              static const struct ctables_category cct_excluded_missing = {
+                .type = CCT_EXCLUDED_MISSING,
+                .hide = true,
+              };
+              cats[a][i] = &cct_excluded_missing;
+              excluded_missing = true;
+            }
         }
     }
 
-  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
-    {
-      const struct ctables_nest *nest = s->nests[a];
-      for (size_t i = 0; i < nest->n; i++)
-        if (i != nest->scale_idx)
-          {
-            const struct variable *var = nest->vars[i];
-            const union value *value = case_data (c, var);
-            ctables_add_occurrence (var, value, &s->occurrences[a][i]);
-          }
-    }
+  if (!excluded_missing)
+    for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+      {
+        const struct ctables_nest *nest = s->nests[a];
+        for (size_t i = 0; i < nest->n; i++)
+          if (i != nest->scale_idx)
+            {
+              const struct variable *var = nest->vars[i];
+              const union value *value = case_data (c, var);
+              ctables_add_occurrence (var, value, &s->occurrences[a][i]);
+            }
+      }
 
-  ctables_cell_add__ (s, c, cats, d_weight, e_weight);
+  ctables_cell_add__ (s, c, cats, is_missing, excluded_missing,
+                      d_weight, e_weight);
 
-  recurse_totals (s, c, cats, d_weight, e_weight, 0, 0);
-  recurse_subtotals (s, c, cats, d_weight, e_weight, 0, 0);
+  //if (!excluded_missing)
+    {
+      recurse_totals (s, c, cats, is_missing, excluded_missing,
+                      d_weight, e_weight, 0, 0);
+      recurse_subtotals (s, c, cats, is_missing, excluded_missing,
+                         d_weight, e_weight, 0, 0);
+    }
 }
 
 struct merge_item
@@ -3486,6 +3561,14 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
           struct ctables_cell_sort_aux aux = { .nest = nest, .a = a };
           sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
 
+#if 0
+          for (size_t j = 0; j < n_sorted; j++)
+            {
+              printf ("%s (%s): %f/%f = %.1f%%\n", sorted[j]->name, sorted[j]->contributes_to_domains ? "y" : "n", sorted[j]->summaries[0].count, sorted[j]->domains[CTDT_COL]->e_count, sorted[j]->summaries[0].count / sorted[j]->domains[CTDT_COL]->e_count * 100.0);
+            }
+          printf ("\n");
+#endif
+          
           struct ctables_level
             {
               enum ctables_level_type
@@ -3877,12 +3960,11 @@ ctables_prepare_table (struct ctables_table *t)
           specs->n = 1;
 
           enum ctables_summary_function function
-            = specs->var ? CTSF_MEAN : CTSF_COUNT;
-          struct ctables_var var = { .is_mrset = false, .var = specs->var };
+            = specs->is_scale ? CTSF_MEAN : CTSF_COUNT;
 
           *specs->specs = (struct ctables_summary_spec) {
             .function = function,
-            .format = ctables_summary_default_format (function, &var),
+            .format = ctables_summary_default_format (function, specs->var),
             .label = ctables_summary_default_label (function, 0),
           };
           if (!specs->var)
@@ -3894,6 +3976,33 @@ ctables_prepare_table (struct ctables_table *t)
       else if (!nest->specs[CSV_TOTAL].n)
         ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
                                         &nest->specs[CSV_CELL]);
+
+      if (t->ctables->smissing_listwise)
+        {
+          struct variable **listwise_vars = NULL;
+          size_t n = 0;
+          size_t allocated = 0;
+
+          for (size_t j = nest->group_head; j < stack->n; j++)
+            {
+              const struct ctables_nest *other_nest = &stack->nests[j];
+              if (other_nest->group_head != nest->group_head)
+                break;
+
+              if (nest != other_nest && other_nest->scale_idx < other_nest->n)
+                {
+                  if (n >= allocated)
+                    listwise_vars = x2nrealloc (listwise_vars, &allocated,
+                                                sizeof *listwise_vars);
+                  listwise_vars[n++] = other_nest->vars[other_nest->scale_idx];
+                }
+            }
+          for (size_t j = 0; j < N_CSVS; j++)
+            {
+              nest->specs[j].listwise_vars = listwise_vars;
+              nest->specs[j].n_listwise_vars = n;
+            }
+        }
     }
 
   struct ctables_summary_spec_set *merged = &t->summary_specs;
@@ -4077,6 +4186,9 @@ ctables_add_category_occurrences (const struct variable *var,
             if (c->include_missing || !var_is_value_missing (var, &vl->value))
               ctables_add_occurrence (var, &vl->value, occurrences);
           break;
+
+        case CCT_EXCLUDED_MISSING:
+          break;
         }
     }
 }