CTABLES strings work
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 2 Aug 2022 05:19:09 +0000 (22:19 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 2 Aug 2022 05:19:09 +0000 (22:19 -0700)
src/language/stats/ctables.c
tests/language/stats/ctables.at

index 3b7dc24d1051daded14b47a9fc25fc6f6c42263f..a69e8d56593a035aad6e6fba5c249681a8c45387 100644 (file)
@@ -4261,7 +4261,6 @@ ctables_pcexpr_evaluate (const struct ctables_pcexpr_evaluate_ctx *ctx,
       }
 
     case CTPO_CAT_NUMBER:
-    case CTPO_CAT_STRING:
     case CTPO_CAT_MISSING:
     case CTPO_CAT_OTHERNM:
     case CTPO_CAT_SUBTOTAL:
@@ -4275,6 +4274,25 @@ ctables_pcexpr_evaluate (const struct ctables_pcexpr_evaluate_ctx *ctx,
         return ctables_pcexpr_evaluate_category (ctx, &cv);
       }
 
+    case CTPO_CAT_STRING:
+      {
+        int width = var_get_width (ctx->section->nests[ctx->pc_a]->vars[ctx->pc_a_idx]);
+        char *s = NULL;
+        if (width > e->string.length)
+          {
+            s = xmalloc (width);
+            buf_copy_rpad (s, width, e->string.string, e->string.length, ' ');
+          }
+        struct ctables_cell_value cv = {
+          .category = ctables_find_category_for_postcompute (ctx->section->table->ctables->dict, ctx->cats, ctx->parse_format, e),
+          .value = { .s = CHAR_CAST (uint8_t *, s ? s : e->string.string) },
+        };
+        assert (cv.category != NULL);
+        double retval = ctables_pcexpr_evaluate_category (ctx, &cv);
+        free (s);
+        return retval;
+      }
+
     case CTPO_ADD:
       return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_add);
 
index 82e0f9bea8bfc703cc6cdc99331461437a8a12a7..ebda3ce826c8378a8ff221130c282688024304da 100644 (file)
@@ -2,12 +2,10 @@ AT_BANNER([CTABLES])
 
 dnl Features not yet tested:
 dnl - Preprocessing to distinguish categorical from scale.
-dnl - String variables and values
 dnl - Testing details of missing value handling in summaries.
 dnl - test CLABELS ROWLABELS=LAYER.
 dnl - Test VLABELS.
 dnl - Test WEIGHT and adjustment weights.
-dnl - EMPTY=INCLUDE For string ranges.
 dnl - Summary functions:
 dnl   * Separate summary functions for totals and subtotals.
 dnl   * )CILEVEL in summary label specification
@@ -16,10 +14,8 @@ dnl   * VALUE
 dnl   * LABEL
 dnl   * ascending/descending
 dnl - CATEGORIES:
-dnl   * String values
 dnl   * Date values
 dnl   * THRU (numeric ranges)
-dnl   * THRU (string ranges)
 dnl   * OTHERNM
 dnl - FORMAT:
 dnl   * MINCOLWIDTH, MAXCOLWIDTH, UNITS.
@@ -54,23 +50,6 @@ dnl   * .LCL and .UCL suffixes.
 dnl   * .SE suffixes.
 dnl - CATEGORIES:
 dnl   * Data-dependent sorting.
-dnl
-dnl
-dnl Bug:
-dnl     CTABLES /TABLE=qnd1 [MEAN, MEDIAN] BY qns3a.
-dnl produces a bad median:
-dnl                     Custom Tables
-dnl +--------------------------+-----------------------+
-dnl |                          |      S3a. GENDER:     |
-dnl |                          +-----------+-----------+
-dnl |                          |    Male   |   Female  |
-dnl |                          +----+------+----+------+
-dnl |                          |Mean|Median|Mean|Median|
-dnl +--------------------------+----+------+----+------+
-dnl |D1. AGE: What is your age?|  46|   999|  50|   999|
-dnl +--------------------------+----+------+----+------+
-
-
 
 AT_SETUP([CTABLES parsing])
 AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .])
@@ -760,6 +739,88 @@ Every day
 ])
 AT_CLEANUP
 
+AT_SETUP([CTABLES one string variable])
+AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .])
+AT_DATA([ctables.sps],
+[[GET 'nhtsa.sav'.
+STRING licensed(A8).
+MISSING VALUES licensed('DontKnow', 'Refused').
+RECODE qnd7a(1='Yes')(2='No')(3='DontKnow')(4='Refused') INTO licensed.
+CTABLES /TABLE licensed.
+CTABLES /TABLE licensed [COUNT, TOTALS[COUNT, VALIDN]] /CATEGORIES VARIABLES=ALL TOTAL=YES MISSING=INCLUDE.
+CTABLES /TABLE licensed /CATEGORIES VARIABLES=licensed ['Yes', 'No'] TOTAL=YES.
+* Notice that the string matching is case-sensitive.
+CTABLES /TABLE licensed /CATEGORIES VARIABLES=licensed ['Yes', 'no'] TOTAL=YES.
+CTABLES /TABLE licensed /CATEGORIES VARIABLES=licensed ['No' THRU 'yes'] TOTAL=YES.
+CTABLES
+    /PCOMPUTE &notyes=EXPR(['No']+['DontKnow']+['Refused'])
+    /PPROPERTIES &notyes LABEL='Not Yes' HIDESOURCECATS=YES
+    /TABLE licensed
+    /CATEGORIES VARIABLES=licensed ['Yes', &notyes, 'No', 'DontKnow', 'Refused'].
+CTABLES
+    /PCOMPUTE &notyes=EXPR(['No']+['DontKnow']+['Refused'])
+    /PPROPERTIES &notyes LABEL='Not Yes' HIDESOURCECATS=YES
+    /TABLE licensed
+    /CATEGORIES VARIABLES=licensed ['Yes', &notyes, 'DontKnow' THRU 'No', 'Refused'].
+]])
+AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl
+    Custom Tables
+╭────────────┬─────╮
+│            │Count│
+├────────────┼─────┤
+│licensed No │  572│
+│         Yes│ 6379│
+╰────────────┴─────╯
+
+          Custom Tables
+╭─────────────────┬─────┬───────╮
+│                 │Count│Valid N│
+├─────────────────┼─────┼───────┤
+│licensed DontKnow│    4│       │
+│         No      │  572│       │
+│         Refused │   44│       │
+│         Yes     │ 6379│       │
+│         Total   │ 6999│   6951│
+╰─────────────────┴─────┴───────╯
+
+     Custom Tables
+╭──────────────┬─────╮
+│              │Count│
+├──────────────┼─────┤
+│licensed Yes  │ 6379│
+│         No   │  572│
+│         Total│ 6951│
+╰──────────────┴─────╯
+
+     Custom Tables
+╭──────────────┬─────╮
+│              │Count│
+├──────────────┼─────┤
+│licensed Yes  │ 6379│
+│         no   │    0│
+│         Total│ 6379│
+╰──────────────┴─────╯
+
+      Custom Tables
+╭────────────────┬─────╮
+│                │Count│
+├────────────────┼─────┤
+│licensed No     │  572│
+│         Refused│   44│
+│         Yes    │ 6379│
+│         Total  │ 6995│
+╰────────────────┴─────╯
+
+      Custom Tables
+╭────────────────┬─────╮
+│                │Count│
+├────────────────┼─────┤
+│licensed Yes    │ 6379│
+│         Not Yes│  620│
+╰────────────────┴─────╯
+])
+AT_CLEANUP
+
 AT_SETUP([CTABLES one scale variable])
 AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .])
 AT_DATA([ctables.sps],