Bookmark and Share

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]

st: PRESERVING VARIABLE LABELS AFTER REPEATED COLLAPSE


From   Giovanna <[email protected]>
To   [email protected]
Subject   st: PRESERVING VARIABLE LABELS AFTER REPEATED COLLAPSE
Date   Thu, 6 Jan 2011 17:20:05 +0100

Hi Statalisters!

I am collapsing a dataset containing data on interviews to teachers in India.
Each teacher (variable: utid) has been interviewed three times
therefore we have three observations per every value of utid.

i.e.:
utid                   id                 water              distance
           pure_public                ...                      ...
10                      1                   1                    40
                      1
10                      2                   1                    100
                     1
10                      1                   0                    25
                      0

20                      5                   0                    100
                      0
20                      7                   1                    75
                      1
20                      1                   0                    10
                      1


I want to collapse the dataset so to get one observation per teacher
in place of three, using the mean as statistic AND KEEP THE SAME
LABELS TO THE VARIABLES (I totally have 1172 variables)
I have used the following script so to keep the labels of the
variables after collapsing them:

*Save the labels before collapsing:

  foreach v of var * {
  local l`v' : variable label `v'
        if `"`l`v''"' == "" {
  local l`v' "`v'"
   }
  }


*Collapse each variable by teacher identity (utid)
local i=1
tempfile temp
save `temp'
foreach var of varlist id-pure_public {
use `temp', clear
collapse (mean)`var', by(utid)
sort utid
tempfile `i'
save ``i''
local i=`i'+1
}



*Merge all the variables using the teacher identity as key (utid)
use `1', clear
local i=2
while `i'<1172 {
sort utid
merge utid using ``i'', unique
drop _merge
local i=`i'+1
}

sort utid
save Raj, replace
* Relabel the variables with the original labels they used to have
before collapsing them:
foreach v of var * {
label var `v' "`l`v''"
}


When operating the last portion of the script, Stata stops in
relebelling the variables after having successfully renamed 786
variables.
Why? What am I doing wrong?

I have already checked this:
http://www.stata.com/support/faqs/data/variables.html
but I wasn't able to get the problem sorted out.

Thank you very much!

Best,
Giovanna

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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