Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: variable labels lost due to reshape

From   Joseph Coveney <>
To   Statalist <>
Subject   Re: st: variable labels lost due to reshape
Date   Wed, 14 Jun 2006 09:40:49 +0900

Jelena Zurovac wrote:

I am using Stata 9. After I reshape my data from child-level to mother-level, I lose all the variable labels. This makes sense, since my reshape drops all variables and creates new ones from them. But, losing all the variable labels is rather inconvenient. How can I retrieve them?

I searched ths listserv, and couldn't find the answer. I tried to use label
save, but this refers to label values, not variable labels.

It seems that there would be two ways to approach this:
- Force Stata to somehow keep the variable labels (I doubt this is possible)
- Use some efficient method of retrieving the labels from the original
dataset and applying after the reshape.


You can put the variable labels into local macros for retrieval
after -reshape-. -help macro- and then click on the "extended_fcn"
hyperlink to see the help file the one to use. Its use is illustrated in a dummy example below.

I'm not aware of any metadata that you can't get to in Stata.

Joseph Coveney

set more off
set obs 3
set seed `=date("2006-06-14", "ymd")'
generate byte row = _n
foreach var of newlist magnitude1 magnitude2 magnitude3 {
generate byte `var' = ceil(10 * uniform())
label variable magnitude1 Height
label variable magnitude2 Width
label variable magnitude3 Length
* Begin here
foreach var of varlist magnitude* {
local i = subinstr("`var'", "magnitude", "", 1)
local variable_label`i' : variable label `var'
reshape long magnitude, i(row) j(column)
macro list
// You can now re-apply the variable labels
local variable_label_list
forvalues j = 1/`i' {
local variable_label_list `variable_label_list' `j' `variable_label`j''
label define Dimensions `variable_label_list'
label values column Dimensions
list , noobs sepby(row) abbreviate(15)

* For searches and help try:

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