Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: How to loop only over the periods in which the commands are feasible, and automatically ignore the others?


From   Stata Chris <statachris@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: How to loop only over the periods in which the commands are feasible, and automatically ignore the others?
Date   Tue, 4 Aug 2009 10:00:34 -0400

Dear Statalisters,

(1)
I wanted to run the loop below, where for some of the periods either
delta or L.delta does not exist. Now it seems that with the code
below, rather than executing the commands for the periods where the
necessary observations do exist and ignoring the others, what happens
is that whenever at least for one period the command cannot be
executed Stata gets stuck entirely. Is there a smart way to tell it to
just ignore those periods and continue with the next one?

. gen beta         = .
. gen se_beta      = .
. gen t_beta       = .
. forvalues i=458(1)515 {
     . reg delta L.delta                                      if period==`i'
     . replace beta        = adjust *  _b[L.delta]    if period==`i'
     . replace se_beta   = adjust * _se[L.delta]   if period==`i'
     . replace t_beta     = beta / se_beta            if period==`i'
. }

(2) What I tried is to kick out all non-feasible periods before
starting the loop, and creating a new period2-variable that contains
only those periods in which it is feasible. I tried this with the code
below, but here Stata tells me that the syntax is invalid where I
would like it to start the loop...

. save temp1.dta, replace
. sort obs_fundasset period
. keep if delta!=. & L.delta!=.
. duplicates drop period, force
. gen period2=_n
. save temp2.dta, replace
. clear all
. use temp1.dta
. merge obs_triple using temp2, sort
. summarize period2
. scalar period2_min = r(min)
. scalar period2_max = r(max)
. forvalues i=period2_min(1)period2_max{
     . reg delta L.delta                               if period2==`i'
     . replace beta          = adjust *  _b[L.delta]   if period2==`i'
     . replace se_beta       = adjust * _se[L.delta]   if period2==`i'
     . replace t_beta        = beta / se_beta          if period2==`i'
. }


Any advice much appreciated.

Thanks and all best,
Chris
*
*   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/



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