Statalist


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

Re: st: foreach and postfile


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: foreach and postfile
Date   Thu, 13 Dec 2007 11:05:30 +0000 (GMT)

--- Simon Moore <simoncmoore@gmail.com> wrote:
> I'm running something along the lines of:-
> 
> postfile coef id var1 var2 var3 using "`dir'\fit.dta", replace
> foreach num of numlist 1(1)1000 {
> qui logit expcont var1 var2 var3 if id == `num'
> post coef (`num') (_b[var1]) (_b[var2]) (_b[var3])
> }
> postclose coef
> 
> occasionally the logit does not work as planned, gives an error (e.g.
> 
> "[var] not found")  and the foreach loop grinds to a halt.  I would
> much rather capture the error somehow, post it to the file coef and
> have the loop move onto the next iteration.

what about this:
*--------------- begin example -----------------
sysuse auto, clear

/*-logit- for foreign on the subsamples*/
/*rep78==1 and rep78==2 will fail*/
tab rep78 foreign

tempname memhold
tempfile results

postfile `memhold' id mpg ok using `results'
forvalues num = 1/5 {
    capture logit foreign mpg if rep78 == `num'
    if _rc > 0 {
        post `memhold' (`num') (.) (0) 
    }
    else {
        post `memhold' (`num') (_b[mpg]) (1) 
    }
}
postclose `memhold'
use `results', clear
list
*-------------------- end example --------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      __________________________________________________________
Sent from Yahoo! Mail - a smarter inbox http://uk.mail.yahoo.com

*
*   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–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index