I have a panel dataset. I want to calculate the difference in value for a particular varlist on the condition that the variable value is equal to zero at either t or t-1. I'll give an example of what I hope to get.


X[t-1] 0 3 - -
X[t] 3 5 3 -
Y[t-1] 6 5 - -
Y[t] 4 0 - 5
Z[t-1] 6 5 - -
Z[t] 8 9 - -



So the difference is only taken for changes to and from zero i.e. D.Var_A or D.Var_B.




For changes between values other than zero, it doesn't really matter if they are represented by a - or a 0.




Does this make sense? I always find it difficult to articulate a problem like this.




What's the best way to go about this?