Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: FW: Aggregating values back to a certain row


From   Daniel Stefan Hain <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: FW: Aggregating values back to a certain row
Date   Wed, 19 Mar 2014 19:03:46 +0000

Dear Stata users,

I currently have a problem I find myself unable to solve.

I have longitudinal data on investments made by companies, each investment a single row. It looks sort of like:

invest_id             firm_id                 value                                     year
1                              1                              1,5                                          2001
2                              1                              2,7                                          2001
3                              1                              3,4                                          2002
4                              2                              0,9                                          1998
5                              2                              5,5                                          2003

I want to introduce a "investment experience", which aggregates the number (or value) of previous investments over the last X (lets say 5) years previous to the current one. Firms have very different investment activity, so might the one have 100, the next only one per year. I up to now tried different versions of something like


local N =_N
forvalues n=1/`N' {
                local i=1
                local t=0
       while t=0 {
                                replace firm_value_agg = firm_value_agg + value[_n - `i']) if `n'=_n
                                local i = `i' + 1
                                local t = 1             if year[_n] - 5 == year[_n - `i']
                }
}

Here I try to write a loop that goes over every row (first forvalues), and then (in theory) aggregates previous investment values in "firm_value_agg", as long till it comes to a row where the year is smaller than the current year minus 5 (since I want to aggregate the last 5 years). I tried some different versions, but always get some error messages like "no weights allowed", or the code just produces unintended results. For every piece of advice how to make this work I would be very grateful.

Best wishes


Daniel S. Hain

Ph.D. Fellow, M.Sc. Economics, Dipl.-Wirt.-Ing.
Department of Business and Management | IKE | DRUID | EIS 
T: (+45) 9940 2724  | Email: [email protected] 
Web: www.ike.aau.dk | www.eis-all.dk | www.innoresource.org 
Aalborg University | Fibigerstræde 11, Room 91 | 9220 Aalborg, Denmark


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index