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

st: RE: automatic transfer of variable labels

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: automatic transfer of variable labels
Date   Wed, 16 Apr 2003 09:18:25 +0100

Declan Trott
> This is probably impossible, but 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?
> Probably impossible since variable labels, unlike value 
> labels, aren't 
> defined with names of their own independent from the 
> variable they are 
> attached to.

Not impossible at all. First, there is an undocumented 
command which does this. 

_crcslbl newvar oldvar 

copies the variable label of oldvar to newvar. (If 
you like, think of the syntax as a terse way of 

give the same label (slbl) to newvar as that of oldvar

("crc" is a small piece of history codified in syntax, 
as Computing Resource Center was the company preceding 
Stata Corp).)  

Second, on SSC there is a more general command called 
-copydesc- which copies the variable label -- and other 
properties as well. The author of this command evidently 
didn't like the syntax of -_crcslbl-, as you have 
to go 

copydesc oldvar newvar 

What I guess was in the author's mind was the idea 
that you 

copy the descriptive properties of oldvar to newvar 

However, the difference in syntax is trivial. What's 
more notable is that other things will be copied
apart from the variable label. 

Third, the route from first principles is worth 

local lbl : variable label oldvar
label var newvar `"`lbl'"' 

The compound double quotes here are an 
example of programmer's paranoia. Very likely 
you would be all right with 

label var newvar "`lbl'"  

For one variable, that is likely to be no gain 
on typing out the label directly. But it is 
what underlies the other commands. 

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


foreach v of var bedroom? { 
	copydesc bedroom `v' 


local lbl : variable label bedroom 
foreach v of var bedroom? { 
	label var `v' `"`lbl'"' 

The last has the advantage from Stata's 
point view that it is faster to execute. 
It has the advantage from your point of 
view that you have learnt a small fundamental 
of Stata which may come in useful. 

Otherwise of course you might want to save 
on your typing at the cost of learning another 

[email protected] 

P.S. my syntax assumes no more than 9 bedrooms. 

*   For searches and help try:

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