CTABLES
[pspp] / src / language / stats / ctables.c
index 5393c1b838b5343bc4eb71433c0246c130d26eac..7b7a81946ca29bbec057550e06772e5c99f15c9d 100644 (file)
@@ -69,136 +69,18 @@ enum ctables_vlabel
    - upper confidence limits (*.UCL)
    - standard error (*.SE)
  */
-#define SUMMARIES                                                       \
-    /* All variables. */                                                \
-    S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL)            \
-    S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
-    S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
-                                                                        \
-    /* All variables (unweighted.) */ \
-    S(CTSF_UCOUNT, "UCOUNT", N_("Unweighted Count"), CTF_COUNT, CTFA_ALL)            \
-    S(CTSF_UROWPCT_COUNT, "UROWPCT.COUNT", N_("Unweighted Row %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UCOLPCT_COUNT, "UCOLPCT.COUNT", N_("Unweighted Column %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UTABLEPCT_COUNT, "UTABLEPCT.COUNT", N_("Unweighted Table %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_USUBTABLEPCT_COUNT, "USUBTABLEPCT.COUNT", N_("Unweighted Subtable %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERPCT_COUNT, "ULAYERPCT.COUNT", N_("Unweighted Layer %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERROWPCT_COUNT, "ULAYERROWPCT.COUNT", N_("Unweighted Layer Row %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERCOLPCT_COUNT, "ULAYERCOLPCT.COUNT", N_("Unweighted Layer Column %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UROWPCT_VALIDN, "UROWPCT.VALIDN", N_("Unweighted Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UCOLPCT_VALIDN, "UCOLPCT.VALIDN", N_("Unweighted Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UTABLEPCT_VALIDN, "UTABLEPCT.VALIDN", N_("Unweighted Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_USUBTABLEPCT_VALIDN, "USUBTABLEPCT.VALIDN", N_("Unweighted Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERPCT_VALIDN, "ULAYERPCT.VALIDN", N_("Unweighted Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERROWPCT_VALIDN, "ULAYERROWPCT.VALIDN", N_("Unweighted Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERCOLPCT_VALIDN, "ULAYERCOLPCT.VALIDN", N_("Unweighted Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UROWPCT_TOTALN, "UROWPCT.TOTALN", N_("Unweighted Row Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UCOLPCT_TOTALN, "UCOLPCT.TOTALN", N_("Unweighted Column Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_UTABLEPCT_TOTALN, "UTABLEPCT.TOTALN", N_("Unweighted Table Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_USUBTABLEPCT_TOTALN, "USUBTABLEPCT.TOTALN", N_("Unweighted Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERPCT_TOTALN, "ULAYERPCT.TOTALN", N_("Unweighted Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERROWPCT_TOTALN, "ULAYERROWPCT.TOTALN", N_("Unweighted Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
-    S(CTSF_ULAYERCOLPCT_TOTALN, "ULAYERCOLPCT.TOTALN", N_("Unweighted Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
-                                                                        \
-    /* Scale variables, totals, and subtotals. */                       \
-    S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE)  \
-    S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE)           \
-    S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE)     \
-    S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE)  \
-    S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE)  \
-    S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE)           \
-    S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE)   \
-    S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE)        \
-    S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
-    S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
-    S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE)              \
-    S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE)      \
-    S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
-    S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE)      \
-    S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
-    S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
-    S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    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) \
-                                                                        \
-    /* Scale variables, totals, and subtotals (unweighted). */                       \
-    S(CTSF_UMEAN, "UMEAN", N_("Unweighted Mean"), CTF_GENERAL, CTFA_SCALE)           \
-    S(CTSF_UMEDIAN, "UMEDIAN", N_("Unweighted Median"), CTF_GENERAL, CTFA_SCALE)     \
-    S(CTSF_UMISSING, "UMISSING", N_("Unweighted Missing"), CTF_GENERAL, CTFA_SCALE)  \
-    S(CTSF_UMODE, "UMODE", N_("Unweighted Mode"), CTF_GENERAL, CTFA_SCALE)           \
-    S(CTSF_UPTILE, "UPTILE", N_("Unweighted Percentile"), CTF_GENERAL, CTFA_SCALE)   \
-    S(CTSF_USEMEAN, "USEMEAN", N_("Unweighted Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
-    S(CTSF_USTDDEV, "USTDDEV", N_("Unweighted Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
-    S(CTSF_USUM, "USUM", N_("Unweighted Sum"), CTF_GENERAL, CTFA_SCALE)              \
-    S(CSTF_UTOTALN, "UTOTALN", N_("Unweighted Total N"), CTF_COUNT, CTFA_SCALE)      \
-    S(CTSF_UVALIDN, "UVALIDN", N_("Unweighted Valid N"), CTF_COUNT, CTFA_SCALE)      \
-    S(CTSF_UVARIANCE, "UVARIANCE", N_("Unweighted Variance"), CTF_GENERAL, CTFA_SCALE) \
-    S(CTSF_UROWPCT_SUM, "UROWPCT.SUM", N_("Unweighted Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_UCOLPCT_SUM, "UCOLPCT.SUM", N_("Unweighted Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_UTABLEPCT_SUM, "UTABLEPCT.SUM", N_("Unweighted Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_USUBTABLEPCT_SUM, "USUBTABLEPCT.SUM", N_("Unweighted Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_ULAYERPCT_SUM, "ULAYERPCT.SUM", N_("Unweighted Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_ULAYERROWPCT_SUM, "ULAYERROWPCT.SUM", N_("Unweighted Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
-    S(CTSF_ULAYERCOLPCT_SUM, "ULAYERCOLPCT.SUM", N_("Unweighted Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
-
-#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) \
-    S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
-    S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
-    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
   {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
-    SUMMARIES
+#include "ctables.inc"
 #undef S
   };
 
 enum {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
-  N_CTSF_FUNCTIONS = SUMMARIES
+  N_CTSF_FUNCTIONS =
+#include "ctables.inc"
 #undef S
 };
 
@@ -227,6 +109,7 @@ struct ctables_domain
 
     const struct ctables_cell *example;
 
+    size_t sequence;
     double d_valid;             /* Dictionary weight. */
     double d_count;
     double d_total;
@@ -895,7 +778,7 @@ ctables_function_availability (enum ctables_summary_function f)
 {
   static enum ctables_function_availability availability[] = {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
-    SUMMARIES
+#include "ctables.inc"
 #undef S
   };
 
@@ -905,102 +788,9 @@ ctables_function_availability (enum ctables_summary_function 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:
-    case CTSF_UCOUNT:
-    case CTSF_UROWPCT_COUNT:
-    case CTSF_UCOLPCT_COUNT:
-    case CTSF_UTABLEPCT_COUNT:
-    case CTSF_USUBTABLEPCT_COUNT:
-    case CTSF_ULAYERPCT_COUNT:
-    case CTSF_ULAYERROWPCT_COUNT:
-    case CTSF_ULAYERCOLPCT_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:
-    case CTSF_UROWPCT_VALIDN:
-    case CTSF_UCOLPCT_VALIDN:
-    case CTSF_UTABLEPCT_VALIDN:
-    case CTSF_USUBTABLEPCT_VALIDN:
-    case CTSF_ULAYERPCT_VALIDN:
-    case CTSF_ULAYERROWPCT_VALIDN:
-    case CTSF_ULAYERCOLPCT_VALIDN:
-    case CTSF_UROWPCT_TOTALN:
-    case CTSF_UCOLPCT_TOTALN:
-    case CTSF_UTABLEPCT_TOTALN:
-    case CTSF_USUBTABLEPCT_TOTALN:
-    case CTSF_ULAYERPCT_TOTALN:
-    case CTSF_ULAYERROWPCT_TOTALN:
-    case CTSF_ULAYERCOLPCT_TOTALN:
-    case CTSF_UMEAN:
-    case CTSF_UMEDIAN:
-    case CTSF_UMISSING:
-    case CTSF_UMODE:
-    case CTSF_UPTILE:
-    case CTSF_USEMEAN:
-    case CTSF_USTDDEV:
-    case CTSF_USUM:
-    case CSTF_UTOTALN:
-    case CTSF_UVALIDN:
-    case CTSF_UVARIANCE:
-    case CTSF_UROWPCT_SUM:
-    case CTSF_UCOLPCT_SUM:
-    case CTSF_UTABLEPCT_SUM:
-    case CTSF_USUBTABLEPCT_SUM:
-    case CTSF_ULAYERPCT_SUM:
-    case CTSF_ULAYERROWPCT_SUM:
-    case CTSF_ULAYERCOLPCT_SUM:
-      return false;
-  }
-  NOT_REACHED ();
+  return f == CTSF_COUNT || f == CTSF_ECOUNT || f == CTSF_UCOUNT;
 }
 
-
 static bool
 parse_ctables_summary_function (struct lexer *lexer,
                                 enum ctables_summary_function *f)
@@ -1013,8 +803,7 @@ parse_ctables_summary_function (struct lexer *lexer,
   static struct pair names[] = {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
     { ENUM, SS_LITERAL_INITIALIZER (NAME) },
-    SUMMARIES
-
+#include "ctables.inc"
     /* The .COUNT suffix may be omitted. */
     S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
     S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
@@ -1093,7 +882,7 @@ ctables_summary_default_format (enum ctables_summary_function function,
 {
   static const enum ctables_format default_formats[] = {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
-    SUMMARIES
+#include "ctables.inc"
 #undef S
   };
   switch (default_formats[function])
@@ -1119,7 +908,7 @@ ctables_summary_label (const struct ctables_summary_spec *spec, double cilevel)
     {
       static const char *default_labels[] = {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
-        SUMMARIES
+#include "ctables.inc"
 #undef S
       };
 
@@ -1153,7 +942,7 @@ ctables_summary_function_name (enum ctables_summary_function function)
 {
   static const char *names[] = {
 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
-    SUMMARIES
+#include "ctables.inc"
 #undef S
   };
   return names[function];
@@ -1450,7 +1239,10 @@ ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
     {
       struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
       if (!rhs)
-        return NULL;
+        {
+          ctables_axis_destroy (lhs);
+          return NULL;
+        }
 
       struct ctables_axis *nest = ctables_axis_new_nonterminal (
         CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
@@ -1496,7 +1288,10 @@ ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
     {
       struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
       if (!rhs)
-        return NULL;
+        {
+          ctables_axis_destroy (lhs);
+          return NULL;
+        }
 
       lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
                                           ctx->lexer, start_ofs);
@@ -2594,6 +2389,15 @@ ctables_summary_init (union ctables_summary *s,
       s->count = 0;
       break;
 
+    case CTSF_ROW_ID:
+    case CTSF_COL_ID:
+    case CTSF_TABLE_ID:
+    case CTSF_SUBTABLE_ID:
+    case CTSF_LAYER_ID:
+    case CTSF_LAYERROW_ID:
+    case CTSF_LAYERCOL_ID:
+      break;
+
     case CTSF_MAXIMUM:
     case CTSF_MINIMUM:
     case CTSF_RANGE:
@@ -2712,6 +2516,15 @@ ctables_summary_uninit (union ctables_summary *s,
     case CTSF_UVALIDN:
       break;
 
+    case CTSF_ROW_ID:
+    case CTSF_COL_ID:
+    case CTSF_TABLE_ID:
+    case CTSF_SUBTABLE_ID:
+    case CTSF_LAYER_ID:
+    case CTSF_LAYERROW_ID:
+    case CTSF_LAYERCOL_ID:
+      break;
+
     case CTSF_MAXIMUM:
     case CTSF_MINIMUM:
     case CTSF_RANGE:
@@ -2857,6 +2670,15 @@ ctables_summary_add (union ctables_summary *s,
         s->count += 1.0;
       break;
 
+    case CTSF_ROW_ID:
+    case CTSF_COL_ID:
+    case CTSF_TABLE_ID:
+    case CTSF_SUBTABLE_ID:
+    case CTSF_LAYER_ID:
+    case CTSF_LAYERROW_ID:
+    case CTSF_LAYERCOL_ID:
+      break;
+
     case CTSF_MISSING:
       if (is_missing)
         s->count += d_weight;
@@ -2994,6 +2816,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_UCOLPCT_SUM:
     case CTSF_UCOLPCT_TOTALN:
     case CTSF_UCOLPCT_VALIDN:
+    case CTSF_COL_ID:
       return CTDT_COL;
 
     case CTSF_LAYERCOLPCT_COUNT:
@@ -3004,6 +2827,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_ULAYERCOLPCT_SUM:
     case CTSF_ULAYERCOLPCT_TOTALN:
     case CTSF_ULAYERCOLPCT_VALIDN:
+    case CTSF_LAYERCOL_ID:
       return CTDT_LAYERCOL;
 
     case CTSF_LAYERPCT_COUNT:
@@ -3014,6 +2838,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_ULAYERPCT_SUM:
     case CTSF_ULAYERPCT_TOTALN:
     case CTSF_ULAYERPCT_VALIDN:
+    case CTSF_LAYER_ID:
       return CTDT_LAYER;
 
     case CTSF_LAYERROWPCT_COUNT:
@@ -3024,6 +2849,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_ULAYERROWPCT_SUM:
     case CTSF_ULAYERROWPCT_TOTALN:
     case CTSF_ULAYERROWPCT_VALIDN:
+    case CTSF_LAYERROW_ID:
       return CTDT_LAYERROW;
 
     case CTSF_ROWPCT_COUNT:
@@ -3034,6 +2860,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_UROWPCT_SUM:
     case CTSF_UROWPCT_TOTALN:
     case CTSF_UROWPCT_VALIDN:
+    case CTSF_ROW_ID:
       return CTDT_ROW;
 
     case CTSF_SUBTABLEPCT_COUNT:
@@ -3044,6 +2871,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_USUBTABLEPCT_SUM:
     case CTSF_USUBTABLEPCT_TOTALN:
     case CTSF_USUBTABLEPCT_VALIDN:
+    case CTSF_SUBTABLE_ID:
       return CTDT_SUBTABLE;
 
     case CTSF_TABLEPCT_COUNT:
@@ -3054,6 +2882,7 @@ ctables_function_domain (enum ctables_summary_function function)
     case CTSF_UTABLEPCT_SUM:
     case CTSF_UTABLEPCT_TOTALN:
     case CTSF_UTABLEPCT_VALIDN:
+    case CTSF_TABLE_ID:
       return CTDT_TABLE;
     }
 
@@ -3137,6 +2966,13 @@ ctables_function_is_pctsum (enum ctables_summary_function function)
     case CTSF_UTABLEPCT_COUNT:
     case CTSF_UTABLEPCT_TOTALN:
     case CTSF_UTABLEPCT_VALIDN:
+    case CTSF_ROW_ID:
+    case CTSF_COL_ID:
+    case CTSF_TABLE_ID:
+    case CTSF_SUBTABLE_ID:
+    case CTSF_LAYER_ID:
+    case CTSF_LAYERROW_ID:
+    case CTSF_LAYERCOL_ID:
       return false;
 
     case CTSF_COLPCT_SUM:
@@ -3171,6 +3007,15 @@ ctables_summary_value (const struct ctables_cell *cell,
     case CTSF_UCOUNT:
       return s->count;
 
+    case CTSF_ROW_ID:
+    case CTSF_COL_ID:
+    case CTSF_TABLE_ID:
+    case CTSF_SUBTABLE_ID:
+    case CTSF_LAYER_ID:
+    case CTSF_LAYERROW_ID:
+    case CTSF_LAYERCOL_ID:
+      return cell->domains[ctables_function_domain (ss->function)]->sequence;
+
     case CTSF_ROWPCT_COUNT:
     case CTSF_COLPCT_COUNT:
     case CTSF_TABLEPCT_COUNT:
@@ -3447,10 +3292,19 @@ ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
             {
               const char *a_label = var_lookup_value_label (var, a_val);
               const char *b_label = var_lookup_value_label (var, b_val);
-              int cmp = (a_label
-                         ? (b_label ? strcmp (a_label, b_label) : 1)
-                         : (b_label ? -1 : value_compare_3way (
-                              a_val, b_val, var_get_width (var))));
+              int cmp;
+              if (a_label)
+                {
+                  if (!b_label)
+                    return -1;
+                  cmp = strcmp (a_label, b_label);
+                }
+              else
+                {
+                  if (b_label)
+                    return 1;
+                  cmp = value_compare_3way (a_val, b_val, var_get_width (var));
+                }
               if (cmp)
                 return a_cv->category->sort_ascending ? cmp : -cmp;
             }
@@ -3463,6 +3317,25 @@ ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
   return 0;
 }
 
+static int
+ctables_cell_compare_leaf_3way (const void *a_, const void *b_,
+                                const void *aux UNUSED)
+{
+  struct ctables_cell *const *ap = a_;
+  struct ctables_cell *const *bp = b_;
+  const struct ctables_cell *a = *ap;
+  const struct ctables_cell *b = *bp;
+
+  for (enum pivot_axis_type axis = 0; axis < PIVOT_N_AXES; axis++)
+    {
+      int al = a->axes[axis].leaf;
+      int bl = b->axes[axis].leaf;
+      if (al != bl)
+        return al > bl ? 1 : -1;
+    }
+  return 0;
+}
+
 /* Algorithm:
 
    For each row:
@@ -4451,6 +4324,37 @@ ctables_cell_calculate_postcompute (const struct ctables_section *s,
   return ctables_pcexpr_evaluate (&ctx, pc->expr);
 }
 
+static char *
+ctables_format (double d, const struct fmt_spec *format,
+                const struct fmt_settings *settings)
+{
+  const union value v = { .f = d };
+  char *s = data_out_stretchy (&v, "UTF-8", format, settings, NULL);
+
+  /* The custom-currency specifications for NEQUAL, PAREN, and PCTPAREN don't
+     produce the results we want for negative numbers, putting the negative
+     sign in the wrong spot, before the prefix instead of after it.  We can't,
+     in fact, produce the desired results using a custom-currency
+     specification.  Instead, we postprocess the output, moving the negative
+     sign into place:
+
+         NEQUAL:   "-N=3"  => "N=-3"
+         PAREN:    "-(3)"  => "(-3)"
+         PCTPAREN: "-(3%)" => "(-3%)"
+
+     This transformation doesn't affect NEGPAREN. */
+  char *minus_src = strchr (s, '-');
+  if (minus_src && (minus_src == s || minus_src[-1] != 'E'))
+    {
+      char *n_equals = strstr (s, "N=");
+      char *lparen = strchr (s, '(');
+      char *minus_dst = n_equals ? n_equals + 1 : lparen;
+      if (minus_dst)
+        move_element (s, minus_dst - s + 1, 1, minus_src - s, minus_dst - s);
+    }
+  return s;
+}
+
 static void
 ctables_table_output (struct ctables *ct, struct ctables_table *t)
 {
@@ -4533,7 +4437,7 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
               {
                 struct ctables_section *s = &t->sections[j];
                 sections[n_sections++] = s;
-                n_total_cells += s->cells.count;
+                n_total_cells += hmap_count (&s->cells);
 
                 size_t depth = s->nests[a]->n;
                 max_depth = MAX (depth, max_depth);
@@ -4557,6 +4461,24 @@ 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
+          if (a == PIVOT_AXIS_ROW)
+            {
+              size_t ids[N_CTDTS];
+              memset (ids, 0, sizeof ids);
+              for (size_t j = 0; j < n_sorted; j++)
+                {
+                  struct ctables_cell *cell = sorted[j];
+                  for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+                    {
+                      struct ctables_domain *domain = cell->domains[dt];
+                      if (!domain->sequence)
+                        domain->sequence = ++ids[dt];
+                    }
+                }
+            }
+#endif
+
 #if 0
           for (size_t j = 0; j < n_sorted; j++)
             {
@@ -4714,6 +4636,40 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
         }
     }
 
+  {
+    size_t n_total_cells = 0;
+    for (size_t j = 0; j < t->n_sections; j++)
+      n_total_cells += hmap_count (&t->sections[j].cells);
+
+    struct ctables_cell **sorted = xnmalloc (n_total_cells, sizeof *sorted);
+    size_t n_sorted = 0;
+    for (size_t j = 0; j < t->n_sections; j++)
+      {
+        const struct ctables_section *s = &t->sections[j];
+        struct ctables_cell *cell;
+        HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
+          if (!cell->hide)
+            sorted[n_sorted++] = cell;
+      }
+    assert (n_sorted <= n_total_cells);
+    sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_leaf_3way,
+          NULL);
+    size_t ids[N_CTDTS];
+    memset (ids, 0, sizeof ids);
+    for (size_t j = 0; j < n_sorted; j++)
+      {
+        struct ctables_cell *cell = sorted[j];
+        for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+          {
+            struct ctables_domain *domain = cell->domains[dt];
+            if (!domain->sequence)
+              domain->sequence = ++ids[dt];
+          }
+      }
+
+    free (sorted);
+  }
+
   for (size_t i = 0; i < t->n_sections; i++)
     {
       struct ctables_section *s = &t->sections[i];
@@ -4777,17 +4733,14 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
               else if (d == SYSMIS && ct->missing)
                 value = pivot_value_new_user_text (ct->missing, SIZE_MAX);
               else if (is_ctables_format)
-                {
-                  char *s = data_out_stretchy (&(union value) { .f = d },
-                                               "UTF-8", &format,
-                                               &ct->ctables_formats, NULL);
-                  value = pivot_value_new_user_text_nocopy (s);
-                }
+                value = pivot_value_new_user_text_nocopy (
+                  ctables_format (d, &format, &ct->ctables_formats));
               else
                 {
                   value = pivot_value_new_number (d);
                   value->numeric.format = format;
                 }
+              /* XXX should text values be right-justified? */
               pivot_table_put (pt, dindexes, n_dindexes, value);
             }
         }
@@ -6552,15 +6505,21 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds)
       if (n_summaries > 1)
         {
           msg (SE, _("Summaries may appear only on one axis."));
-          if (summaries[PIVOT_AXIS_ROW])
-            msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
-                    _("This variable on the rows axis has a summary."));
-          if (summaries[PIVOT_AXIS_COLUMN])
-            msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
-                    _("This variable on the columns axis has a summary."));
-          if (summaries[PIVOT_AXIS_LAYER])
-            msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
-                    _("This variable on the layers axis has a summary."));
+          for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+            if (summaries[a])
+              {
+                msg_at (SN, summaries[a]->loc,
+                        a == PIVOT_AXIS_ROW
+                        ? _("This variable on the rows axis has a summary.")
+                        : a == PIVOT_AXIS_COLUMN
+                        ? _("This variable on the columns axis has a summary.")
+                        : _("This variable on the layers axis has a summary."));
+                if (scales[a])
+                  msg_at (SN, summaries[a]->loc,
+                          _("This is a scale variable, so it always has a "
+                            "summary even if the syntax does not explicitly "
+                            "specify one."));
+              }
           goto error;
         }
       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)