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: Weighted moving sum missing values


From   Dorian Garbe <[email protected]>
To   [email protected]
Subject   st: Weighted moving sum missing values
Date   Mon, 24 Jun 2013 18:59:12 +0100

Dear all,

I have panel data with panel variable: Id and time variable Yr which
looks as follows:

Yr Id Var1 Var2

2002 1 0 ...
2002 2 3 ...
2003 1 4 ...
2003 2 0 ...
2004 1 1 ...
2004 2 5 ...

Var 2 is supposed to be the cumulated sum of Var1, weighted and over
the past five years.
However if I just use gen by Id: Var2= 1*L1.Var1 + 0.8*L2.Var1 +
0.6*L3.Var1 + 0.4*L4.Var1 + 0.2*L5.Var1 for 2002-2006 this will
generate a missing value, because not all 5 lagged variables are
available. I would like to treat this missing lagged variables as 0 or
just ignore them, so for 2004, Id 1 the value is calculated as Var2=
1*L1.Var1 + 0.8*L2.Var1 = 1*4 + 0.8*0
I also need to do this by Id.

Could I do some sort of nested if, saying If Yr=2002 then Var2=0, if
Yr=2003 then Var2=L1.Var1, if Yr=2004 then Var2= L1.Var1 + 0.8 L2.Var1
....? I think you cannot do this in Stata!?

I have been searching for a solution all day, but could not really
figure it out. I am also an absolute beginner.

Thank you very much,
Dorian
*
*   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