Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Kyle Peyton <kyle.peyton@unimelb.edu.au> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: forcing loops through errors |
Date | Wed, 27 Jul 2011 06:34:20 +0000 |
Hi Linh, Generally if you include "capture" then Stata will continue through despite the error. If you want it to print then you can tell Stata to capture noisily. Try: Foreach varname of varlist V0_Sev EQ_Sev ET_Sev DR_Sev WF_Sev FL_Sev { Capture noisily reg killed `varname' if startyear2==`year' Capture noisily reg totaffected `varname' if startyear2==`year' Capture noisily reg estdamageusmillion `varname' if startyear2==`year' } Hopefully this helps! -Kyle -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Linh Vuong Sent: Wednesday, 27 July 2011 4:15 PM To: statalist@hsphsun2.harvard.edu Subject: st: forcing loops through errors Stata/IC 11.2 for Mac (64-bit Intel) Born 30 Mar 2011 Hello Statalist! I'm trying to write a loop that runs through 6 variables: VO_Sev EQ_Sev ET_Sev DR_Sev WF_Sev FL_Sev, for 2000-2011. This is what I tried: forvalues year=2000/2011 { foreach varname of varlist VO_Sev EQ_Sev ET_Sev DR_Sev WF_Sev FL_Sev { reg killed `varname' if startyear2==`year' reg totaffected `varname' if startyear2==`year' reg estdamageusmillion `varname' if startyear2==`year' } } The first problem I encounter is an error message that says "No observations", which is true for some of these regressions. However, I'd like Stata to continue with the loop and do the next regression. How can I make Stata continue the loop even though it's finding an error? The second thing I'd like it to do is to add a variable in the regression that corresponds to the year. The variables are quint0 quint1 quint2 quint3 quint4 quint5 quint6 quint7 quint8 quint9, where the last digit corresponds to the year. For example, quint0 corresponds to 2000 and quint9 corresponds to 2009. So I think I want something like: reg killed `varname' quint0 if startyear2==2000 Any idea on how to write this into a loop? Thanks so much, Linh -- Linh Vuong Master of Public Policy, Candidate 2012 Goldman School of Public Policy, UC Berkeley linh@berkeley.edu | @linh8u http://www.linkedin.com/in/mlvuong * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/