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

Re: st: foreach and postfile

From   Simon Moore <[email protected]>
To   [email protected]
Subject   Re: st: foreach and postfile
Date   Thu, 13 Dec 2007 11:38:03 +0000

Very many thanks Maarten it works very well! I had not come across the very useful _rc before. I also added the option asis to the logit just to capture the last few stragglers.
All the best

Maarten buis wrote:

--- Simon Moore <[email protected]> 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
*-------------------- end example --------------------
(For more on how to use examples I sent to the Statalist, see )

Hope this helps,

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

Sent from Yahoo! Mail - a smarter inbox

* For searches and help try:

*   For searches and help try:

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