Skip to content

Validating variable names when writing data #46

Closed

Description

@wbuchanan

Was trying to use @hadley's haven package to convert some SPSS files that some colleagues created a while ago and ran into some issues with a variable name in the original SPSS file that contained an invalid character for variable names in Stata. The variable name in question was filter_$, but the $ is an illegal character for a variable name in Stata. It might be good to standardize variable names to the set that is typically valid [a-zA-Z0-9_] across all file formats and provide an option to either substitute illegal characters with an underscore or to remove them from the variable name. When the file is read into Stata is causes all sorts of issues when trying to reference variable names.

Activity

  1. evanmiller commented on Jan 31, 2016

    @evanmiller
    Contributor

    I am in favor of validating and sanitizing the variable names. One potential issue will be ensuring that the sanitized column names are unique within the data set (e.g. filter_$ and filter_% might both be squashed to filter_, which will wreak other kinds of havoc within Stata). So to do this properly we'll need an algorithm to assign unique names in the event of collisions.

  2. wbuchanan commented on Feb 24, 2016

    @wbuchanan
    Author

    The simplest solution for those cases might be to append a numeric value instead of truncating the illegal characters. For example, filter_$ and filter_% would become filter_1 and filter_2 assuming that was the order in which they were stored in the underlying file. Not sure if C/C++ have any objects similar to a Set in Java, but that class only allows unique values of the collection (e.g., filter_ and filter_ would become filter_ and one of the instances would be lost), it might be helpful as a comparator for testing whether the number of unique variable names and the number of variable names parsed from the dataset are the same.

  3. diogocp commented on Feb 26, 2016

    @diogocp

    For reference, here is what Stata does when reading CSV files:

    . type "stata_names_test.csv"
    filter_$,filter_%
    1,a
    2,b
    3,c
    
    . import delimited "stata_names_test.csv"
    (2 vars, 3 obs)
    
    . describe
    
    Contains data
      obs:             3                          
     vars:             2                          
     size:             6                          
    ------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------
    filter_         byte    %8.0g                 filter_$
    v2              str1    %9s                   filter_%
    ------------------------------------------------------------
  4. evanmiller commented on Jun 21, 2016

    @evanmiller
    Contributor

    Addressed in 2baad22

    The library will now return an error when attempting to write an illegal column name -- it is the responsibility of the user to provide a legal / sanitized version of the column name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions