UPDATE 28 November 2012: Forget this post. I wrote a program to do this, which you can find here.
UPDATE 03 July 2012: I updated the code so that it works (there were some typos before)
Using the collapse command in STATA tends to remove value labels and variable labels. Below I have included code to save and restore the value labels (assuming you keep all the variables except the 'over' variable that you specify in the collapse command).
This is simple and has probably been posted elsewhere (and there is probably an easier, more elegant solution). The collapse command really should include an option for this.
If you want to learn how to restore value labels after using the reshape command, see this link:
http://www.stata.com/support/faqs/data/labels_reshape.html
For preserving value labels:
// Save and restore value labels
// Shafique Jamal, shafique.jamal@gmail.com
// Saturday 7 May 2011
// define the path for saving temporary files
global path_for_tempfiles "/Users/shafique/allfiles/ECA_projects/TJ_social_assistance/data_qnr/raw/zerkola2012pilot"
// This is the list of variables for which we want to save the value labels.
label dir
local list_of_valuelables = r(names)
// save the label values
label save using $path_for_tempfiles/label_values.do, replace
// note the names of the label values for each variable that has a label value attached to it: need the variable name - value label correspodence
local list_of_vars_w_valuelables
foreach var of varlist * {
local templocal : value label `var'
if ("`templocal'" != "") {
local varlabel_`var' : value label `var'
di "`var': `varlabel_`var''"
local list_of_vars_w_valuelables "`list_of_vars_w_valuelables' `var'"
}
}
di "`list_of_vars_w_valuelables'"
// do the collapse here
// redefine the label values
do $path_for_tempfiles/label_values.do
// reattach the label values
foreach var of local list_of_vars_w_valuelables {
cap label values `var' `varlabel_`var''
}
UPDATE 03 July 2012: I updated the code so that it works (there were some typos before)
Using the collapse command in STATA tends to remove value labels and variable labels. Below I have included code to save and restore the value labels (assuming you keep all the variables except the 'over' variable that you specify in the collapse command).
This is simple and has probably been posted elsewhere (and there is probably an easier, more elegant solution). The collapse command really should include an option for this.
If you want to learn how to restore value labels after using the reshape command, see this link:
http://www.stata.com/support/faqs/data/labels_reshape.html
For preserving value labels:
// Save and restore value labels
// Shafique Jamal, shafique.jamal@gmail.com
// Saturday 7 May 2011
// define the path for saving temporary files
global path_for_tempfiles "/Users/shafique/allfiles/ECA_projects/TJ_social_assistance/data_qnr/raw/zerkola2012pilot"
// This is the list of variables for which we want to save the value labels.
label dir
local list_of_valuelables = r(names)
// save the label values
label save using $path_for_tempfiles/label_values.do, replace
// note the names of the label values for each variable that has a label value attached to it: need the variable name - value label correspodence
local list_of_vars_w_valuelables
foreach var of varlist * {
local templocal : value label `var'
if ("`templocal'" != "") {
local varlabel_`var' : value label `var'
di "`var': `varlabel_`var''"
local list_of_vars_w_valuelables "`list_of_vars_w_valuelables' `var'"
}
}
di "`list_of_vars_w_valuelables'"
// do the collapse here
// redefine the label values
do $path_for_tempfiles/label_values.do
// reattach the label values
foreach var of local list_of_vars_w_valuelables {
cap label values `var' `varlabel_`var''
}
No comments:
Post a Comment