Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: automatic transfer of variable labels (2)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: automatic transfer of variable labels (2)
Date   Wed, 16 Apr 2003 10:00:15 +0100

Declan Trott

> > when I create new variables by renaming or 
> > transforming old ones, I would like to be able to give the 
> > new variables 
> > the same variable labels as the old ones without having to 
> > type out "label 
> > variable varname text" for each new variable.
> > 
> > eg  I have a collection of information on different houses 
> > and the variable 
> > "x" is the number of bedrooms.  First I want to rename x as 
> > something more 
> > intuitive:
> > 
> > generate bedrooms = x
> > 
> > And then I create dummy variables for each value of 
> > bedrooms that occurs in 
> > the data:
> > 
> > tab bedrooms, generate(bedrooms)
> > 
> > Is there any way that I can give each of the bedrooms 
> > dummies (bedrooms1, 
> > bedrooms2 etc) the same variable label as x without having 
> > to type the 
> > "label . . ." command individually for each one?
> > 
> 
> You want to do this for a bunch of variables. 
> The literal answer to your question is 
> 
> foreach v of var bedroom? { 
> 	_crcslbl `v' bedroom
> } 
> 
> or 
> 
> foreach v of var bedroom? { 
> 	copydesc bedroom `v' 
> } 
> 
> or 
> 
> local lbl : variable label bedroom 
> foreach v of var bedroom? { 
> 	label var `v' `"`lbl'"' 
> } 
> 

Not Declan's question, but there seems a natural 
sequel to this. What if you don't want exactly
the same label, but one indicating which 
variable you are dealing with? -tabulate, gen()- 
does produce such a label, but it is often ugly. 

Suppose the maximum number of bedrooms is 9. 

local lbl : variable label bedroom 
forval i = 1/9 { 
	label var bedroom`i' "`lbl' is `i'"  
} 
	
illustrates one line of attack -- and 
also how knowing the low level syntax 
is needed for some problems. 

Nick 
n.j.cox@durham.ac.uk 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index