Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Resolving variable name conflicts when renaming


From   "James Beard" <[email protected]>
To   [email protected]
Subject   st: Resolving variable name conflicts when renaming
Date   Fri, 02 Aug 2013 11:28:51 +0545

Dear Statalisters,

I'm using -import excel- (Stata 12.1) to import data that have been 
collected using the CommCare system on Android phones. The Excel 
sheets have column headings like form|LMPgroup|LMPmonth or 
form|subcase_0|case|create|case_name, for example, which -import 
excel- will turn into something that works as a Stata variable name, 
in these cases formLMPgroupLMPmonth and formsubcase_0casecreatecase_.
When there is a name conflict, -import excel- will give the variable 
the name of the Excel column, for example BB. That's absolutely fine 
and a sensible thing for Stata to do. 

But I don't like (any of) those names, and I want to generate an 
automatic procedure to change the names to something I and my 
colleagues think more sensible. In the examples above, say, LMPmonth 
and case_name. Fortunately -import excel- preserves the original 
column headings in the labels of the variable created. So I've 
written some code that extracts the last "word" (where words are 
delimited by |) of the label, uses -strtoname()- and does a rename 
(actually, it's a bit more complicated than that, but for the present 
purposes, it's a good enough description). But, I don't have control 
over what the original column names might be, and it's possible, even 
likely, that the last words will be duplicated across columns. I've 
written some very mickey mouse code to deal with this by adding 0 to 
9 in turn to the names (after reducing the name length to 31 
characters if necessary), and checking to see if those names already 
exist. I could expand this to (say) 0 to 99, which should cover all 
cases likely to be encountered in practice. But does anyone have a 
suggestion for a better approach? I sort-of feel there "ought" to be 
an option in -strtoname()- to do this automatically!

Thanks for reading this far.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index