On 1/12/07, Steven Gorl� <[email protected]> wrote:
Dear Stata experts,
How can I append the results of my for loop in a file "sample"?
I would like to store the summary results of each i loop in a datafile
sample.
Unfortunately the loop overwrites sample.
I would like to keep 4 variables gem se sd a.
Below you will find the syntax.
Any tips are welcome,
I'm surprised the file even gets over-written as Stata would normally
complain about trying to save a file that already exists without using
the -, replace- option.
Anyway, the 'trick' is to append the files. You can also shorten your
code to the collapse statement alone.
forvalues i=1/100 {
use bmi_voeg, clear
sample 200, count
/* These don't seem to be used in this loop?
*/
* gen pw = 8564/200
* gen fpc = 8564
/* Unless you _really_ want to see the output from each loop these are
superflous */
/* and besides you can load the data set at the end and look at the
values */
* summarize lnvoeg
* generate gem = r(mean)
* generate sd = r(sd)
* generate a = r(N)
* generate se = sd/(sqrt(a))
* display "gem " gem " " "sd " sd " " "se " se " " "a " a
/* Collapse the data calculating the mean (gem), sd and N (a)
*/
collapse (mean) gem=lnvoeg (sd) sd=lnoveg (count) a=lnvoeg
/* Generate an indicator to show which iteration the sample comes from
*/
gen sample = `i'
/* Save the file, if this is the first loop then save the file on its
own.. */
if(`i' == 1){
save sample, replace
}
/* ...otherwise append the previous results and save the file again
*/
else{
append using sample
save sample, replace
}
}
* Now generate your se for all iterations
gen se = sd / sqrt(a)
save sample, replace
Neil
--
"Every great advance in natural knowledge has involved the absolute
rejection of authority." - Thomas H. Huxley
Email - [email protected] / [email protected]
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/
*
* 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/