Statalist The Stata Listserver


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

Re: st: for loop


From   Steven Gorl� <[email protected]>
To   <[email protected]>
Subject   Re: st: for loop
Date   Fri, 12 Jan 2007 21:34:57 +0100

Dear Neil,

Thanks for the tip! I am a new stata user and sometimes small issues like this can take a lot of time and worry.
I am still discovering the possibilities of stata.

* gen pw = 8564/200
* gen fpc = 8564

These commands were indeed superfluous in this loop. But I have a similar loop in which I take the surveydesign into account.
I wonder if the standard errors will be the same...
First I did a bootstrap, but I didn't find an option ('without replacement'). (Or does it exist?)
That's why I used the - sample- command in a loop.

Thanks!

Steven Gorl�

----- Original Message ----- From: "Neil Shephard" <[email protected]>
To: <[email protected]>
Sent: Friday, January 12, 2007 12:47 AM
Subject: Re: st: for loop



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/



*
*   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/



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