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]
Re: st: Variable name length limit
From
"Eric A. Booth" <[email protected]>
To
[email protected]
Subject
Re: st: Variable name length limit
Date
Tue, 20 Aug 2013 09:03:37 -0500
<>
When I encounter this situation, my preference is to import the excel
file without the first row of the excel file being read in as variable
names (so in -import excel- that mean leaving out the 'first' option,
or in -insheet- I would specify the option -nonames-, etc.).
Then, I store that first row from the excel file (which is now the
first row in the stata dataset) as either variable labels, if that
makes sense, or if I need to specify other var labels but I want to
keep this header row elsewhere, I store them as chars. Then after
storing them I can delete the first row and use the shorter variable
names in programs/code that might bite if I had 32 character variable
names.
A few notes:
If I"m using -import excel- I dont like the automatic naming of {A, B,
C, D, ...}, because I want to be able to specify ranges of variables
easily (e.g., v2-v6 or v?2) in commands, so I rename them to
something like the naming convention below. Also, to store the
existing excel top row into variable labels or chars ( I do both
below), I do this in the same loop, example:
***************************************! begin example
*--rename all vars with numbers & add excel headers to labels/chars:
loc i = 1
foreach x of varlist * {
rename `x' v`i'
label var v`i' `"`=v`i'[1]'"'
**or**
char def v`i'[lab] `"`=v`i'[1]'"'
loc i `++i'
}
describe
*--for use elsewhere:
char list
di `"`v1[lab]'"'
*--now you can drop the first row:
drop in 1
***************************************! end example
- Eric
On Tue, Aug 20, 2013 at 7:59 AM, Jean Bouyer <[email protected]> wrote:
> I come back to an "old" post since I have a similar problem.
> I have imported data into Stata from an Excel file. Initial variable names have been truncated to 32 characters when necessary.
> I have had to use reshape wide that builds new variables adding number 1, 2, 3 ... at the end of the name. Therefore, some variables would have a name longer than 32 and the program returned an error.
> I had to change one by one (I found no other way...) the name of the variables equal to 32. By the way, this was not so easy since the importation from Excel produced variable names with the same 31 first characters and a number in 32nd position. I agree that the Excel file could be better, but it was not mine ...
>
> Is there a better way to do ?
>
> Thanks
>
> Jean Bouyer
>
>
>
> Date: Wed, 19 Jun 2013 18:32:45 +0800
> From: James Bernard <[email protected]>
> Subject: Re: st: RE: Variable name length limit
>
> Thanks Nick! Very Helpful
>
> On Wed, Jun 19, 2013 at 3:47 PM, Nick Cox <[email protected]> wrote:
>> Let's revise this description to fill in gaps and correct misstatements.
>>
>> 1. James is using -diff-, a user-written program from SSC. (Please
>> remember to explain where user-written programs you refer to come
>> from. Other posts show that failing to explain this just wastes other
>> people's time, not a good thing to do when you want their help.)
>>
>> 2. From the code it appears that -diff- at various points may attempt
>> to create scalars (not variables) with temporary names longer than
>> Stata's limit, which Stata can't allow.
>>
>> There is no sense in which Stata can break its own limits.
>>
>> James alludes to discussions in September 2010 -- search
>> http://www.stata.com/statalist/archive/2010-09/maillist.html -- but
>> the nub of the matter is that the limit on scalar and variable names
>> can't be disabled.
>>
>> This is arguably a bug, or limitation, in -diff-, but short of
>> rewriting the program before the author does the only work-around for
>> James is to use shorter variable names, say by
>>
>> clonevar copy1 = no_dynamic_boxes_fixedat_94
>>
>> and feeding -copy1- and anything else problematic to -diff-..
>>
>> Nick
>> [email protected]
>>
>>
>> On 19 June 2013 08:02, James Bernard <[email protected]> wrote:
>>
>>> I have a variable named "no_dynamic_boxes_fixedat_94". After running a
>>> t-test in -diff command (for Difference-in-Difference estimation)
>>> Stata creates a variable named "_no_dynamic_boxes_fixedat_94_ttest_mc"
>>> which has more than 32 characters....
>>>
>>> it then returns error
>>
>
>
> *
> * 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/
*
* 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/