AUTORECODE: Properly handle variable labels.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 2 Sep 2019 06:37:19 +0000 (06:37 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 2 Sep 2019 06:37:19 +0000 (06:37 +0000)
doc/transformation.texi
src/language/stats/autorecode.c
tests/language/stats/autorecode.at

index 01d26621d4f8d0febaa5422ea7736bce0943135d..9da42937ea62d7e69f806a473b5ded3d1491f9e7 100644 (file)
@@ -271,6 +271,8 @@ If a source value has a value label, then that value label is retained
 for the new value in the target variable.  Otherwise, the source value
 itself becomes each new value's label.
 
+Variable labels are copied from the source to target variables.
+
 @subcmd{PRINT} is currently ignored.
 
 The @subcmd{GROUP} subcommand is relevant only if more than one variable is to be
index e0b5773909bd7f5c50ad69afb99d8fa714d8b593..de7848202f9e1d49b15bcdf0ade32979fdca5a1f 100644 (file)
@@ -66,6 +66,7 @@ struct arc_spec
     int src_idx;                /* Case index of source variable. */
     struct variable *dst;       /* Target variable. */
     struct missing_values mv;   /* Missing values of source variable. */
+    char *label;                /* Variable label of source variable. */
     struct rec_items *items;
   };
 
@@ -240,6 +241,9 @@ cmd_autorecode (struct lexer *lexer, struct dataset *ds)
       spec->width = var_get_width (src_vars[i]);
       spec->src_idx = var_get_case_index (src_vars[i]);
 
+      const char *label = var_get_label (src_vars[i]);
+      spec->label = label ? xstrdup (label) : NULL;
+
       if (group && i > 0)
         spec->items = arc->specs[0].items;
       else
@@ -329,6 +333,7 @@ cmd_autorecode (struct lexer *lexer, struct dataset *ds)
 
       /* Create destination variable. */
       spec->dst = dict_create_var_assert (dict, dst_names[i], 0);
+      var_set_label (spec->dst, spec->label);
 
       /* Create array of pointers to items. */
       n_items = hmap_count (&spec->items->ht);
@@ -430,6 +435,7 @@ arc_free (struct autorecode_pgm *arc)
               hmap_delete (&spec->items->ht, &item->hmap_node);
               free (item);
             }
+          free (spec->label);
           mv_destroy (&spec->mv);
         }
 
index 0397e78e4a7607cf3de8bf6d6d330c6d4bb95e76..97ad28e1c732341ebfff96e9c8b5bf1d613dffd4 100644 (file)
@@ -131,6 +131,7 @@ thingummies  6
 oojimiflips  7
 end data.
 
+variable labels s 'tracking my stuff'.
 value labels /s 'thingummies' 'Funny sticky things'.
 
 autorecode s into new.
@@ -153,11 +154,11 @@ oojimiflips,7.00,2.00
 
 Table: Variables
 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
-new,3,,Scale,Input,8,Right,F8.2,F8.2,
+new,3,tracking my stuff,Scale,Input,8,Right,F8.2,F8.2,
 
 Table: Value Labels
 Variable Value,,Label
-new,1.00,oojars
+tracking my stuff,1.00,oojars
 ,2.00,oojimiflips
 ,3.00,Funny sticky things
 ,4.00,widgets