Get rid of vestigial mrset support.
[pspp] / src / language / stats / ctables.c
index 4f231caa7a9f8f6ace7596be97576b52f6dfce3a..68239b85b9685f7495ca8a8c39f2038e82066126 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "data/casereader.h"
 #include "data/casewriter.h"
+#include "data/data-out.h"
 #include "data/dataset.h"
 #include "data/dictionary.h"
 #include "data/mrset.h"
@@ -114,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) \
@@ -138,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
   {
@@ -152,6 +154,8 @@ enum {
 #undef S
 };
 
+static bool ctables_summary_function_is_count (enum ctables_summary_function);
+
 enum ctables_domain_type
   {
     /* Within a section, where stacked variables divide one section from
@@ -176,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
@@ -195,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;
 
@@ -215,6 +222,8 @@ struct ctables_cell
     axes[PIVOT_N_AXES];
 
     union ctables_summary *summaries;
+
+    //char *name;
   };
 
 struct ctables
@@ -222,6 +231,14 @@ struct ctables
     const struct dictionary *dict;
     struct pivot_table_look *look;
 
+    /* CTABLES has a number of extra formats that we implement via custom
+       currency specifications on an alternate fmt_settings. */
+#define CTEF_NEGPAREN FMT_CCA
+#define CTEF_NEQUAL   FMT_CCB
+#define CTEF_PAREN    FMT_CCC
+#define CTEF_PCTPAREN FMT_CCD
+    struct fmt_settings ctables_formats;
+
     /* If this is NULL, zeros are displayed using the normal print format.
        Otherwise, this string is displayed. */
     char *zero;
@@ -325,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 *,
@@ -340,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];
   };
@@ -417,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;
@@ -469,6 +477,9 @@ struct ctables_category
         CCT_VALUE,
         CCT_LABEL,
         CCT_FUNCTION,
+
+        /* For contributing to TOTALN. */
+        CCT_EXCLUDED_MISSING,
       }
     type;
 
@@ -479,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
@@ -504,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;
   };
 
@@ -536,6 +547,9 @@ ctables_category_uninit (struct ctables_category *cat)
     case CCT_LABEL:
     case CCT_FUNCTION:
       break;
+
+    case CCT_EXCLUDED_MISSING:
+      break;
     }
 }
 
@@ -576,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 ();
@@ -651,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];
           };
@@ -684,7 +701,10 @@ struct ctables_summary_spec
     enum ctables_summary_function function;
     double percentile;          /* CTSF_PTILE only. */
     char *label;
-    struct fmt_spec format;     /* XXX extra CTABLES formats */
+
+    struct fmt_spec format;
+    bool is_ctables_format;       /* Is 'format' one of CTEF_*? */
+
     size_t axis_idx;
   };
 
@@ -715,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,
   };
 }
 
@@ -771,6 +792,65 @@ ctables_function_availability (enum ctables_summary_function f)
   return availability[f];
 }
 
+static bool
+ctables_summary_function_is_count (enum ctables_summary_function 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 ();
+}
+
+
 static bool
 parse_ctables_summary_function (struct lexer *lexer,
                                 enum ctables_summary_function *f)
@@ -859,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,
@@ -875,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 ();
@@ -912,26 +992,24 @@ static bool
 add_summary_spec (struct ctables_axis *axis,
                   enum ctables_summary_function function, double percentile,
                   const char *label, const struct fmt_spec *format,
-                  const struct msg_location *loc, enum ctables_summary_variant sv)
+                  bool is_ctables_format, const struct msg_location *loc,
+                  enum ctables_summary_variant sv)
 {
   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,
@@ -941,6 +1019,7 @@ add_summary_spec (struct ctables_axis *axis,
                       var_name);
               return false;
             }
+#endif
           break;
 
         case CTFA_ALL:
@@ -958,7 +1037,8 @@ 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;
     }
@@ -966,7 +1046,7 @@ add_summary_spec (struct ctables_axis *axis,
     {
       for (size_t i = 0; i < 2; i++)
         if (!add_summary_spec (axis->subs[i], function, percentile, label,
-                               format, loc, sv))
+                               format, is_ctables_format, loc, sv))
           return false;
       return true;
     }
@@ -975,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)
@@ -1023,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;
 }
 
@@ -1046,6 +1105,48 @@ has_digit (const char *s)
   return s[strcspn (s, "0123456789")] != '\0';
 }
 
+static bool
+parse_ctables_format_specifier (struct lexer *lexer, struct fmt_spec *format,
+                                bool *is_ctables_format)
+{
+  char type[FMT_TYPE_LEN_MAX + 1];
+  if (!parse_abstract_format_specifier__ (lexer, type, &format->w, &format->d))
+    return false;
+
+  if (!strcasecmp (type, "NEGPAREN"))
+    format->type = CTEF_NEGPAREN;
+  else if (!strcasecmp (type, "NEQUAL"))
+    format->type = CTEF_NEQUAL;
+  else if (!strcasecmp (type, "PAREN"))
+    format->type = CTEF_PAREN;
+  else if (!strcasecmp (type, "PCTPAREN"))
+    format->type = CTEF_PCTPAREN;
+  else
+    {
+      *is_ctables_format = false;
+      return (parse_format_specifier (lexer, format)
+              && fmt_check_output (format)
+              && fmt_check_type_compat (format, VAL_NUMERIC));
+    }
+
+  if (format->w < 2)
+    {
+      msg (SE, _("Output format %s requires width 2 or greater."), type);
+      return false;
+    }
+  else if (format->d > format->w - 1)
+    {
+      msg (SE, _("Output format %s requires width greater than decimals."),
+           type);
+      return false;
+    }
+  else
+    {
+      *is_ctables_format = true;
+      return true;
+    }
+}
+
 static struct ctables_axis *
 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
 {
@@ -1086,12 +1187,12 @@ ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
       /* Parse format. */
       struct fmt_spec format;
       const struct fmt_spec *formatp;
+      bool is_ctables_format = false;
       if (lex_token (ctx->lexer) == T_ID
           && has_digit (lex_tokcstr (ctx->lexer)))
         {
-          if (!parse_format_specifier (ctx->lexer, &format)
-              || !fmt_check_output (&format)
-              || !fmt_check_type_compat (&format, VAL_NUMERIC))
+          if (!parse_ctables_format_specifier (ctx->lexer, &format,
+                                               &is_ctables_format))
             {
               free (label);
               goto error;
@@ -1103,7 +1204,8 @@ ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
 
       struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
                                                    lex_ofs (ctx->lexer) - 1);
-      add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
+      add_summary_spec (sub, function, percentile, label, formatp,
+                        is_ctables_format, loc, sv);
       free (label);
       msg_location_destroy (loc);
 
@@ -1133,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++)
@@ -1805,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;
         }
     }
@@ -1886,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)
 {
@@ -1902,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]));
     }
@@ -1936,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
@@ -1997,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:
@@ -2039,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 ();
     }
 }
 
@@ -2127,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 ();
     }
 }
 
@@ -2158,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:
@@ -2179,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:
@@ -2186,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)
@@ -2227,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;
 
@@ -2244,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 ();
     }
 }
 
@@ -2294,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:
@@ -2373,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:
@@ -2384,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);
       }
 
@@ -2396,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:
@@ -2403,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;
@@ -2498,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 ();
@@ -2561,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;
 
@@ -2631,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);
         }
     }
 
@@ -2646,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;
             }
         }
@@ -2667,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; )
     {
@@ -2709,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;
         }
     }
 
@@ -2774,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;
@@ -2794,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];
@@ -2816,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)
 {
@@ -2860,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;
             }
         }
@@ -2872,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)
 {
@@ -2887,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;
             }
         }
@@ -2921,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];
@@ -2932,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
@@ -3395,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
@@ -3580,11 +3754,38 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
                     dindexes[n_dindexes++] = leaf;
                   }
 
+              const struct ctables_summary_spec *ss = &specs->specs[j];
+
               double d = (cell->postcompute
                           ? ctables_cell_calculate_postcompute (s, cell)
-                          : ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]));
-              struct pivot_value *value = pivot_value_new_number (d);
-              value->numeric.format = specs->specs[j].format;
+                          : ctables_summary_value (cell, &cell->summaries[j], ss));
+              struct pivot_value *value;
+              if (ct->hide_threshold != 0
+                  && d < ct->hide_threshold
+                  && (cell->postcompute
+                      ? false /* XXX */
+                      : ctables_summary_function_is_count (ss->function)))
+                {
+                  value = pivot_value_new_user_text_nocopy (
+                    xasprintf ("<%d", ct->hide_threshold));
+                }
+              else if (d == 0 && ct->zero)
+                value = pivot_value_new_user_text (ct->zero, SIZE_MAX);
+              else if (d == SYSMIS && ct->missing)
+                value = pivot_value_new_user_text (ct->missing, SIZE_MAX);
+              else if (specs->specs[j].is_ctables_format)
+                {
+                  char *s = data_out_stretchy (&(union value) { .f = d },
+                                               "UTF-8",
+                                               &specs->specs[j].format,
+                                               &ct->ctables_formats, NULL);
+                  value = pivot_value_new_user_text_nocopy (s);
+                }
+              else
+                {
+                  value = pivot_value_new_number (d);
+                  value->numeric.format = specs->specs[j].format;
+                }
               pivot_table_put (pt, dindexes, n_dindexes, value);
             }
         }
@@ -3759,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)
@@ -3776,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;
@@ -3959,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;
         }
     }
 }
@@ -4621,16 +4851,38 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds)
   for (size_t i = 0; i < n_vars; i++)
     vlabels[i] = (enum ctables_vlabel) tvars;
 
+  struct pivot_table_look *look = pivot_table_look_unshare (
+    pivot_table_look_ref (pivot_table_look_get_default ()));
+  look->omit_empty = false;
+
   struct ctables *ct = xmalloc (sizeof *ct);
   *ct = (struct ctables) {
     .dict = dataset_dict (ds),
-    .look = pivot_table_look_unshare (pivot_table_look_ref (
-                                        pivot_table_look_get_default ())),
+    .look = look,
+    .ctables_formats = FMT_SETTINGS_INIT,
     .vlabels = vlabels,
     .postcomputes = HMAP_INITIALIZER (ct->postcomputes),
-    .hide_threshold = 5,
   };
-  ct->look->omit_empty = false;
+
+  struct ctf
+    {
+      enum fmt_type type;
+      const char *dot_string;
+      const char *comma_string;
+    };
+  static const struct ctf ctfs[4] = {
+    { CTEF_NEGPAREN, "(,,,)",   "(...)" },
+    { CTEF_NEQUAL,   "-,N=,,",  "-.N=.." },
+    { CTEF_PAREN,    "-,(,),",  "-.(.)." },
+    { CTEF_PCTPAREN, "-,(,%),", "-.(.%)." },
+  };
+  bool is_dot = settings_get_fmt_settings ()->decimal == '.';
+  for (size_t i = 0; i < 4; i++)
+    {
+      const char *s = is_dot ? ctfs[i].dot_string : ctfs[i].comma_string;
+      fmt_settings_set_cc (&ct->ctables_formats, ctfs[i].type,
+                           fmt_number_style_from_string (s));
+    }
 
   if (!lex_force_match (lexer, T_SLASH))
     goto error;
@@ -4806,15 +5058,19 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds)
           if (!ct->e_weight)
             goto error;
         }
-      else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
+      else if (lex_match_id (lexer, " HIDESMALLCOUNTS"))
         {
-          if (!lex_force_match_id (lexer, "COUNT"))
-            goto error;
-          lex_match (lexer, T_EQUALS);
-          if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
-            goto error;
-          ct->hide_threshold = lex_integer (lexer);
-          lex_get (lexer);
+          if (lex_match_id (lexer, "COUNT"))
+            {
+              lex_match (lexer, T_EQUALS);
+              if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT",
+                                        2, INT_MAX))
+                goto error;
+              ct->hide_threshold = lex_integer (lexer);
+              lex_get (lexer);
+            }
+          else if (ct->hide_threshold == 0)
+            ct->hide_threshold = 5;
         }
       else
         {