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]

Re: st: Loop for creating a sum of previous observations in an unbalanced panal data set


From   Kaspar Dardas <[email protected]>
To   [email protected]
Subject   Re: st: Loop for creating a sum of previous observations in an unbalanced panal data set
Date   Sat, 18 Sep 2010 09:12:36 +0200

Eric,

thanks. This has helped a lot.

Best,

Kaspar

2010/9/17 Eric Booth <[email protected]>:
> <>
>
> Kaspar, here's one solution:
>
> *******************!
> clear
> //1.  create some fake date data to work with//
> set obs 1000
> g date = date("01/01/2008", "MDY") in 1
>        replace date = date[_n-1]+1 in 2/l
>        format date %td
> forval n = 1/3 {
> g value`n' =  int(((`n'*50)+1)*runiform())
>                }
>
> //2. create running sum for prev 90 days for value1 only//
> sort date
> g sum3m_value1 = .
>
> forval n = 1/`=_N' {
>        cap drop i`n'
>        g i`n' = 1 if  date[`n']>=date & (date[`n']-date) <= 90
>        egen sum3m_value1`n' = total(value1) if i`n'==1
>        replace sum3m_value1 = sum3m_value1`n' if mi(sum3m_value1)
>        drop  i`n'
>        drop sum3m_value1`n'
>        }
> *******************!
> From your description, it sounds like you may want to calculate this "value1" within a panel; if so, use a -bysort yourpanelvar- prefix in the code above to create the 90 day sum within each panel.
>
> ~ Eric
>
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> [email protected]
> Office: +979.845.6754
>
>
>
> On Sep 17, 2010, at 2:54 AM, Kaspar Dardas wrote:
>
>> Hello statalisters,
>>
>> I have an unbalanced panel dataset with about 50 Variables and 30000
>> observations between 2003 - 2010 in long format.
>> I need to create a running rum variable"3m_sum_variable" which
>> aggregates a certain variable (lets call it) "value" 3 months into the
>> past. In other words, the variable "3m_sum_variable" should be the sum
>> of all "value" variables which occurred in the last 3 months relative
>> (and including) to the current observation.
>>
>> I think I might create such a variable fairly easy with reshaping.
>> However, since I have about 50 Variable I would rather work with my
>> long format and create a loop which "goes back 3 months" into the past
>> and aggregates the previous "value" variables. Does anybody know of a
>> loop that can do this efficiently for an unbalanced panel datatset?
>>
>> Best & thanks,
>>
>> Kaspar
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>



-- 
Kaspar Dardas
Research Assistant
HCI Endowed Chair of Financial Services

EBS Universität für Wirtschaft und Recht i. Gr.
EBS Business School

Gustav-Stresemann-Ring 3
65189 Wiesbaden

GERMANY
Phone: +496990557616
[email protected]
www.ebs.edu

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


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