[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: How to loop only over the periods in which the commands are feasible, and automatically ignore the others?
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: How to loop only over the periods in which the commands are feasible, and automatically ignore the others?
Date
Tue, 04 Aug 2009 09:37:45 -0500
One short answer to (1) is that you can put -capture- around a block of
statements that might fail.
The code might look like this
forvalues i=458/515 {
capture {
reg delta L.delta if period==`i'
replace beta = adjust * _b[L.delta] if period==`i'
...
}
}
Another approach is something like
forvalues i = 458/515 {
qui count if period == `i' & delta != .
if r(N) > 0 {
...
}
}
where both tests
if period == `i' & delta != .
if r(N) > 0
should be replaced by any test more appropriate.
In terms of (2), the most obvious error is
forvalues i=period2_min(1)period2_max{
as -forval- requires explicit values. That said, getting the approach in
(2) exactly right is in my experience harder work than other solutions
sketched above.
Nick
Stata Chris wrote:
(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'
. }
*
* 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–2024 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |