Originally posted by Richard Williams
View Post
In the following example I have an individual identifier (i) and a time variable (t). I want to create var4 which counts the number of non
missing observations of x by I and t, but I want it to start counting when a missing value appears on x.
Code:
sort i t gen var4=0 replace var4=1 if i>i[_n-1] & x<. replace var4=1 if i==i[_n-1] & x[_n-1]==. replace var4=1 if _n==1 replace var4=0 if x==. replace var4=var4[_n-1]+1 if i==i[_n-1] & x<. recode var4 (0=.)
Code:
i t x var4 ------------------ 1 1 1 1 1 2 0 2 1 3 1 3 1 4 . . 1 5 1 1 ------------------ 2 1 1 1 2 2 1 2 2 3 . . 2 4 0 1 2 5 0 2
However, my var4 doesn't start counting from 1 after (i) changes
Comment