Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Novice Question...


From   SR Millis <[email protected]>
To   [email protected]
Subject   Re: st: Novice Question...
Date   Sat, 17 May 2003 15:39:37 -0400

It appears that you need Stata Ver 8 to run this program.

SR Millis

Jonathan M. McGaharan wrote:

I recently downloaded the following program to compute moving averages. I have never used a nonstandard stata program before and cannot get it to work. I created a folder c:\ado\personal and copied the code into a file named mavg.ado. I have version 7 of stata if that makes a difference. I get an error when stata tries to load the command. Any ideas?


thanks!

mavg <varname>, generate(<newvarname>) t(n) Where n is the number of preceding observations you want to average.

/* mavg begins here */

program mavg
version 8.0
syntax varname(numeric) , Generate(string) t(string) [missing]

capture confirm new variable `generate'
if _rc {
di as err "`generate' invalid new variable name"
exit 198
}
capture confirm integer number `t'
if _rc {
di as err "t() must be an integer"
exit 198
}

local k = `t' - 1
local N = _N
tempvar copyvar sumvar
qui gen double `copyvar' = `varlist'
if "`missing'" == "" {
qui replace `copyvar' = 0 if `copyvar' >= .
}
quietly gen double `sumvar' = .

*-------
* handle _n >= t

forvalues obs = 1/`N' {
local sum = 0
forvalues i = 0/`k' {
local sum = `copyvar'[`obs'-`i'] + `sum'
}
quietly replace `sumvar' = `sum' in `obs'
}

quietly generate `generate' = `sumvar' / `t'

*-------
* handle observations 1 - `t'

forvalues j = 1/`k' {
local sum = 0
forvalues i = 1/`j' {
local sum = `copyvar'[`i'] + `sum'
}
quietly replace `sumvar' = `sum' in `j'
quietly replace `generate' = `sumvar' / `j' in `j'
}
end


/* mavg.ado ends here */

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/


--
~~~~~~~~~~
Scott R. Millis, PhD, ABPP (CN & RP)
Director, Office of Clinical Trials
Kessler Medical Rehabilitation Research & Education Corp
1199 Pleasant Valley Way
West Orange, NJ 07052

tel: 973-243-6976
fax: 973-243-6990
email: [email protected]


*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* 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