Hello Statalisters,

I am sure I am missing something obvious but I looked up on the forum and online and I haven't been able to find a solution to this seemingly elementary problem.
To illustrate what I am trying to do, below is the idea.

Code:
input id  year_test tested
1 2001 .  
1 2002 1
1 2003 .
1 2004 1
1 2005 1
1 2006 .
2 2001 .  
2 2002 .
2 2003 .
2 2004 1
2 2005 1
2 2006 .
end

bysort id: gen n=_n if tested!=.
I understand that when indexing the observations, _n will be simply counting the number of the observations from _n to _N but I need to skip counting when the observations are missing and obtain an index for those that are non missing.
Could you possibly suggest how to do this?
Thanks in advance for your help!