Hi everyone!
I have Problems with writing an ado file that gets as Input the following 6 local macros defined in stata (`q',`gg',`a2',`a3',`zeta',`delta'), does some very simple matrix calculations and passes back the result to Stata in the local macro called: `size_avg_estab_mata'.
The error message I get:
1 invalid name
r(198);
What is the correct way of passing on macros to the program and then the result back to stata?
In my stata file I do this:
local q = 1
local gg = 2
local a2 = 3
local a3 = 4
local zeta = 5
local delta= 6
matsum(`q',`gg',`a2',`a3',`zeta',`delta')
And my matsum.ado file Looks like this:
program matsum
set trace on
version 12
syntax varname [if] [in]
marksample touse
mata: calcsum("`varlist'", "`touse'")
display as txt " factor = " as res r(sum)
end
version 12
mata:
mata set matastrict on
void calcsum(string scalar varname, string scalar touse)
{
real colvector x
real matrix z
real scalar size_avg_estab
q_m = st_local(q)
gg_m = st_local(gg)
a2_m = st_local(a2)
a3_m = st_local(a3)
zeta_m = st_local(zeta)
delta_m = st_local(delta)
for (s=1; s<=`q_m'; s++) {
z[`s'+1,2] = ((`gg_m'^`s')/(1+`a2_m'*(`a3_m'^`s')))^`zeta_m'
z[`s'+1,1] = `delta_m'*(1-`delta_m')^`s'
}
x = z[.,1] :* z[.,2]
size_avg_estab_mata = colsum(x)/colsum(z[.,1])
st_numscalar("r(sum)",size_avg_estab_mata)
}
end
Thanks for any help,
Eniko
I have Problems with writing an ado file that gets as Input the following 6 local macros defined in stata (`q',`gg',`a2',`a3',`zeta',`delta'), does some very simple matrix calculations and passes back the result to Stata in the local macro called: `size_avg_estab_mata'.
The error message I get:
1 invalid name
r(198);
What is the correct way of passing on macros to the program and then the result back to stata?
In my stata file I do this:
local q = 1
local gg = 2
local a2 = 3
local a3 = 4
local zeta = 5
local delta= 6
matsum(`q',`gg',`a2',`a3',`zeta',`delta')
And my matsum.ado file Looks like this:
program matsum
set trace on
version 12
syntax varname [if] [in]
marksample touse
mata: calcsum("`varlist'", "`touse'")
display as txt " factor = " as res r(sum)
end
version 12
mata:
mata set matastrict on
void calcsum(string scalar varname, string scalar touse)
{
real colvector x
real matrix z
real scalar size_avg_estab
q_m = st_local(q)
gg_m = st_local(gg)
a2_m = st_local(a2)
a3_m = st_local(a3)
zeta_m = st_local(zeta)
delta_m = st_local(delta)
for (s=1; s<=`q_m'; s++) {
z[`s'+1,2] = ((`gg_m'^`s')/(1+`a2_m'*(`a3_m'^`s')))^`zeta_m'
z[`s'+1,1] = `delta_m'*(1-`delta_m')^`s'
}
x = z[.,1] :* z[.,2]
size_avg_estab_mata = colsum(x)/colsum(z[.,1])
st_numscalar("r(sum)",size_avg_estab_mata)
}
end
Thanks for any help,
Eniko
Comment