Bookmark and Share

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

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

Re: st: renaming variables based on long labels

From   Nick Cox <>
Subject   Re: st: renaming variables based on long labels
Date   Thu, 21 Jul 2011 09:37:19 -0500

You are confusing quite different facts.

In your code, -renvars- (SJ) is adding the prefix "longprefix" to the
variable names in the -bg2- dataset. But it so happens that all the
variable names so created remain legal. The longest is just 18
characters long so the upper limit of 32 is not biting at all.

However, the names are long enough for Stata's abbreviation machinery
to be used when you call -summarize-, but this is not a matter of
changing the variable names, and in any case the character ~ is not
allowed within variable names.

I imagine your problem is perfectly soluble, but will require more ad
hoc code from you. Example:

foreach var of varlist VAR* {
        local label `=strtoname("`:var lab `var''")
        local label : subinstr local label "Relationship" "Reln", all
        local label : subinstr local label "Democrat" "D", all

On Thu, Jul 21, 2011 at 9:00 AM, Cohen, Elan <> wrote:
> I have a dataset with variable names VAR1-VAR40.  All variables have (pretty long) labels.  I'm using
> foreach var of varlist VAR* {
>        rename `var' `=strtoname("`:var lab `var''")'
> }
> to rename each variable according to their label.  I'm running into a problem because the variable labels are not unique within the first 32 characters and so I'm getting "Relationship_of_interviewee_to_s already defined".
> Can Stata automatically generate a unique name based on existing variables?  This seems like it's possible based on the following output:
> webuse bg2
> renvars, prefix(longprefix)
> su
> where Stata truncates variables names and automatically finds a unique string to display for each variable (-findit renvars-).

*   For searches and help try:

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