Reputation: 15458
I am trying to run a program inside a loop, but I am getting
-Break--
r(1);
Here is my script:
clear all
forvalues i=1/5{
sysuse auto if rep78==`i'
capture program drop testpro
program define testpro,eclass
preserve
tabstat price mpg headroom,stat(mean) save
mat avg=r(StatTotal)
ereturn post avg
restore
end
}
--Break--
r(1);
end of do-file
--Break--
r(1);
Any help in this regard will be highly appreciated.
Upvotes: 2
Views: 1716
Reputation: 9460
There are several issues here. For one, you can't sysuse
with an if
clause, unlike with use
.
There's no need to re-define a program with each iteration of the loop. Define it, then loop.
I am also not sure why you need a program, but this is a matter of taste or perhaps this is merely a pedagogical example.
The command estadd
in the code below is part of estout
suite from SCC.
clear all
capture program drop testpro
program define testpro, eclass
tabstat price mpg headroom, stat(mean) save
quietly estadd matrix avg = r(StatTotal), replace
end
forvalues i=1/5 {
sysuse auto, clear
keep if rep78==`i'
testpro
matrix list e(avg)
}
Upvotes: 3