Hello everyone, I have a do file that basically does this:
clear all
set obs 10000
gen x = rnormal()
gen y = .
forval x =1/`=_N'{
qui summ x if _n>= `x'-2 & _n <= `x'
qui replace y = r(sum) in `x'
}
I'm trying to run this code for huge datasets and is really slow. I think that perhaps if I write a faster program this problem would be reduced, so I wrote the following mata code:
cap mata mata drop csum()
mata
void csum(string scalar varlist, string scalar touse)
{
real scalar sum
sum = sum(tokens(varlist))
st_numscalar("sum", sum)
}
end
cap program drop csum
program define csum, rclass
syntax varlist [if] [in]
marksample touse
mata: csum("`varlist'", "`touse'")
return scalar sum = sum
end
clear all
set obs 10000
gen x = rnormal()
gen y = .
forval x =1/`=_N'{
csum x if _n>= `x'-2 & _n <= `x'
qui replace y = r(sum) in `x'
}
Which is actually slower than STATA summarize program. I have two questions:
1) How could I access to the summarize ado file? (I've already search in C:\ado and \stata\ado)
2) Do you have any idea of how could I make the first or the second code faster?
Thank you everyone
clear all
set obs 10000
gen x = rnormal()
gen y = .
forval x =1/`=_N'{
qui summ x if _n>= `x'-2 & _n <= `x'
qui replace y = r(sum) in `x'
}
I'm trying to run this code for huge datasets and is really slow. I think that perhaps if I write a faster program this problem would be reduced, so I wrote the following mata code:
cap mata mata drop csum()
mata
void csum(string scalar varlist, string scalar touse)
{
real scalar sum
sum = sum(tokens(varlist))
st_numscalar("sum", sum)
}
end
cap program drop csum
program define csum, rclass
syntax varlist [if] [in]
marksample touse
mata: csum("`varlist'", "`touse'")
return scalar sum = sum
end
clear all
set obs 10000
gen x = rnormal()
gen y = .
forval x =1/`=_N'{
csum x if _n>= `x'-2 & _n <= `x'
qui replace y = r(sum) in `x'
}
Which is actually slower than STATA summarize program. I have two questions:
1) How could I access to the summarize ado file? (I've already search in C:\ado and \stata\ado)
2) Do you have any idea of how could I make the first or the second code faster?
Thank you everyone
Comment