dates
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 12 Aug 2022 19:44:27 +0000 (12:44 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 12 Aug 2022 19:44:27 +0000 (12:44 -0700)
src/language/stats/ctables.c
tests/language/stats/ctables.at

index 25908eb65d5fa14626fd3b34e771ef7f2b909645..2611bfebb3a53cbc21fb18e9ce9f546790b2c0e7 100644 (file)
@@ -4283,11 +4283,21 @@ ctables_pcexpr_evaluate (const struct ctables_pcexpr_evaluate_ctx *ctx,
             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);
+
+        const struct ctables_category *category
+          = ctables_find_category_for_postcompute (
+            ctx->section->table->ctables->dict,
+            ctx->cats, ctx->parse_format, e);
+        assert (category != NULL);
+
+        struct ctables_cell_value cv = { .category = category };
+        if (category->type == CCT_NUMBER)
+          cv.value.f = category->number;
+        else if (category->type == CCT_STRING)
+          cv.value.s = CHAR_CAST (uint8_t *, s ? s : e->string.string);
+        else
+          NOT_REACHED ();
+
         double retval = ctables_pcexpr_evaluate_category (ctx, &cv);
         free (s);
         return retval;
index 03f14a66aef02c60ef58c3087b5e5426a796364c..12caa73d3b3c0b57bacb64900f091c950e88224a 100644 (file)
@@ -7,13 +7,6 @@ dnl - Test WEIGHT and adjustment weights.
 dnl - Summary functions:
 dnl   * Separate summary functions for totals and subtotals.
 dnl   * )CILEVEL in summary label specification
-dnl - CATEGORIES:
-dnl   * Date values
-dnl   * THRU (numeric ranges)
-dnl   * OTHERNM
-dnl - Date/time variables and values
-dnl - Test PCOMPUTE:
-dnl   * dates
 dnl - Summary functions:
 dnl   * U-prefix for unweighted summaries.
 dnl   * areaPCT.SUM and UareaPCT.SUM functions.
@@ -1803,6 +1796,74 @@ AT_CHECK([pspp ctables.sps -O box=unicode], [0],
 ]])
 AT_CLEANUP
 
+AT_SETUP([CTABLES string and date and time])
+
+weight=1
+for gender in F M; do
+    for month in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec; do
+        for wkday in Sun Mon Tue Wed Thu Fri Sat Sun; do
+           printf "$weight $gender $month $wkday\n"
+           weight=$(expr \( $weight + 3 \) % 7 + 2)
+       done
+    done
+done > ctables.txt
+
+AT_DATA([ctables.sps],
+[[DATA LIST LIST NOTABLE FILE='ctables.txt'
+    /w (F5.0) gender (A1) fmon (MONTH3) fday (WKDAY3).
+WEIGHT by w.
+VARIABLE LEVEL w (SCALE).
+VARIABLE LEVEL gender fmon fday (NOMINAL).
+VARIABLE LABEL
+  gender 'Gender'
+  fmon 'Favorite month'
+  fday 'Favorite day of the week'.
+VALUE LABELS /gender 'M' 'Male' 'F' 'Female'.
+CTABLES
+    /PCOMPUTE &q2 = EXPR(['APR' THRU 'June'])
+    /PPROPERTIES &q2 LABEL='Q2'
+    /PCOMPUTE &weekend = EXPR(['sun'] + ['Sat'])
+    /PPROPERTIES &weekend LABEL='Weekend'
+    /TABLE fmon BY gender > fday
+    /CATEGORIES VARIABLES=fmon ['JAN', 'FEB', 'Mar', SUBTOTAL="Q1",
+                                4 THRU 6, &q2,
+                               'JUL' THRU 'sep', SUBTOTAL="Q3",
+                               OTHERNM, SUBTOTAL='Q4']
+    /CATEGORIES VARIABLES=gender ['M', 'F']
+    /CATEGORIES VARIABLE=fday ['Sun', 2 THRU 6, 'Sat', &weekend] TOTAL=YES
+    /SLABELS VISIBLE=NO.
+]])
+AT_CHECK([pspp ctables.sps -O box=unicode -O width=120], [0], [dnl
+                                              Custom Tables
+╭──────────────────┬───────────────────────────────────────────────────────────────────────────────────╮
+│                  │                                       Gender                                      │
+│                  ├─────────────────────────────────────────┬─────────────────────────────────────────┤
+│                  │                   Male                  │                  Female                 │
+│                  ├─────────────────────────────────────────┼─────────────────────────────────────────┤
+│                  │         Favorite day of the week        │         Favorite day of the week        │
+│                  ├───┬───┬───┬───┬───┬───┬───┬───────┬─────┼───┬───┬───┬───┬───┬───┬───┬───────┬─────┤
+│                  │SUN│MON│TUE│WED│THU│FRI│SAT│Weekend│Total│SUN│MON│TUE│WED│THU│FRI│SAT│Weekend│Total│
+├──────────────────┼───┼───┼───┼───┼───┼───┼───┼───────┼─────┼───┼───┼───┼───┼───┼───┼───┼───────┼─────┤
+│Favorite month JAN│ 10│  3│  8│  6│  4│  2│  7│     17│   40│  9│  6│  4│  2│  7│  5│  3│     12│   36│
+│               FEB│  6│  8│  6│  4│  2│  7│  5│     11│   38│ 12│  4│  2│  7│  5│  3│  8│     20│   41│
+│               MAR│ 16│  6│  4│  2│  7│  5│  3│     19│   43│  8│  2│  7│  5│  3│  8│  6│     14│   39│
+│               Q1 │ 32│ 17│ 18│ 12│ 13│ 14│ 15│       │     │ 29│ 12│ 13│ 14│ 15│ 16│ 17│       │     │
+│               APR│ 12│  4│  2│  7│  5│  3│  8│     20│   41│  4│  7│  5│  3│  8│  6│  4│      8│   37│
+│               MAY│  8│  2│  7│  5│  3│  8│  6│     14│   39│ 14│  5│  3│  8│  6│  4│  2│     16│   42│
+│               JUN│  4│  7│  5│  3│  8│  6│  4│      8│   37│ 10│  3│  8│  6│  4│  2│  7│     17│   40│
+│               Q2 │ 24│ 13│ 14│ 15│ 16│ 17│ 18│      .│     │ 28│ 15│ 16│ 17│ 18│ 12│ 13│      .│     │
+│               JUL│ 14│  5│  3│  8│  6│  4│  2│     16│   42│  6│  8│  6│  4│  2│  7│  5│     11│   38│
+│               AUG│ 10│  3│  8│  6│  4│  2│  7│     17│   40│ 16│  6│  4│  2│  7│  5│  3│     19│   43│
+│               SEP│  6│  8│  6│  4│  2│  7│  5│     11│   38│ 12│  4│  2│  7│  5│  3│  8│     20│   41│
+│               Q3 │ 54│ 29│ 31│ 33│ 28│ 30│ 32│       │     │ 62│ 33│ 28│ 30│ 32│ 27│ 29│       │     │
+│               OCT│ 16│  6│  4│  2│  7│  5│  3│     19│   43│  8│  2│  7│  5│  3│  8│  6│     14│   39│
+│               NOV│ 12│  4│  2│  7│  5│  3│  8│     20│   41│  4│  7│  5│  3│  8│  6│  4│      8│   37│
+│               DEC│  8│  2│  7│  5│  3│  8│  6│     14│   39│ 14│  5│  3│  8│  6│  4│  2│     16│   42│
+│               Q4 │ 36│ 12│ 13│ 14│ 15│ 16│ 17│       │     │ 26│ 14│ 15│ 16│ 17│ 18│ 12│       │     │
+╰──────────────────┴───┴───┴───┴───┴───┴───┴───┴───────┴─────┴───┴───┴───┴───┴───┴───┴───┴───────┴─────╯
+])
+AT_CLEANUP
+
 AT_SETUP([CTABLES CLABELS])
 AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .])
 AT_DATA([ctables.sps],