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

st: Novice Question...


From   [email protected] (Jonathan M. McGaharan)
To   [email protected]
Subject   st: Novice Question...
Date   17 May 2003 15:31:16 EDT

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index