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: Weighted moving sum missing values
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Weighted moving sum missing values
Date
Mon, 24 Jun 2013 19:27:41 +0100
The presumption that missing means zero is a very strong one but I am
trusting that there is a very, very strong defence.
That said you just need terms such as
cond(missing(L1.var1), 0, L1.var1)
rather than
L1.var1
Alternatively, why not work with a clone
clonevar fudged_var1 = var1
replace fudged_var1 = 0 if missing(var1)
Nick
[email protected]
On 24 June 2013 18:59, Dorian Garbe <[email protected]> wrote:
> 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.
>
*
* 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/