You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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.
Activity
evanmiller commentedon Jan 31, 2016
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_$andfilter_%might both be squashed tofilter_, 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.wbuchanan commentedon Feb 24, 2016
The simplest solution for those cases might be to append a numeric value instead of truncating the illegal characters. For example,
filter_$andfilter_%would becomefilter_1andfilter_2assuming 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.diogocp commentedon Feb 26, 2016
For reference, here is what Stata does when reading CSV files:
evanmiller commentedon Jun 21, 2016
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.