Hi all,
I have a question in regards to implementing a counter variable that is contingent on 3 other variables and a filter. The counter should start at 1 for every new observation in A and increase if the next observation in B has the same value. The count should start from the oldest year and only count if the filter does not equal 1. To illustrate this, please refer to the following data example.
A B Year Filter
1 1 94 1
1 1 93 .
1 2 96 .
1 3 97 1
1 1 95 .
1 2 99 .
2 4 91 .
2 2 93 .
2 2 92 1
2 3 95 .
3 2 90 .
3 2 90 .
The count values from top to bottom should be: ., 1, 1, ., 2, 2, 1, 1, 1, 1, 2
I am using this code but this does not properly take into account the filter.
sort A B Year, stable
by A B: gen Counter = _n if Filter != 1
I would appreciate any kind of help.
Thank you very much.
I have a question in regards to implementing a counter variable that is contingent on 3 other variables and a filter. The counter should start at 1 for every new observation in A and increase if the next observation in B has the same value. The count should start from the oldest year and only count if the filter does not equal 1. To illustrate this, please refer to the following data example.
A B Year Filter
1 1 94 1
1 1 93 .
1 2 96 .
1 3 97 1
1 1 95 .
1 2 99 .
2 4 91 .
2 2 93 .
2 2 92 1
2 3 95 .
3 2 90 .
3 2 90 .
The count values from top to bottom should be: ., 1, 1, ., 2, 2, 1, 1, 1, 1, 2
I am using this code but this does not properly take into account the filter.
sort A B Year, stable
by A B: gen Counter = _n if Filter != 1
I would appreciate any kind of help.
Thank you very much.
Comment