Hi everyone, I have a very stupid problem that I cannot sort out.
I have a panel dataset with a variable id for every respondent, and a time variable called "wave", and I created a variable that is called "dist_next_jump" (which I think the definition is not relevant). The only thing that I want to do is to create a variable that takes value N where N is the number of zero values in a group of zeroes that is not interrupted by another number, for each id, and decreases up to 1. I make you an example.
id dist_next_jump my_var
1 4 .
1 0 3
1 0 2
1 0 1
1 2 .
1 0 2
1 0 1
2 0 4
2 0 3
2 0 2
2 0 1
2 3 .
2 3 .
I tried with the following command but it has a problem, it doesn't interrupt the counting when it finds a value that is not 0. If an id has 7 zeroes with an interruption at the fourth one, instead of creating 4 3 2 1 . 3 2 1 it makes 7 6 5 4 . 3 2 1.
DO you have any idea on how to do that? Thank you very much, I'm leaving here an example of the dataset that I have.
I have a panel dataset with a variable id for every respondent, and a time variable called "wave", and I created a variable that is called "dist_next_jump" (which I think the definition is not relevant). The only thing that I want to do is to create a variable that takes value N where N is the number of zero values in a group of zeroes that is not interrupted by another number, for each id, and decreases up to 1. I make you an example.
id dist_next_jump my_var
1 4 .
1 0 3
1 0 2
1 0 1
1 2 .
1 0 2
1 0 1
2 0 4
2 0 3
2 0 2
2 0 1
2 3 .
2 3 .
I tried with the following command but it has a problem, it doesn't interrupt the counting when it finds a value that is not 0. If an id has 7 zeroes with an interruption at the fourth one, instead of creating 4 3 2 1 . 3 2 1 it makes 7 6 5 4 . 3 2 1.
Code:
gen counter = . bysort id (wave) : replace counter = cond(dist_next_jump == 0, _N - _n, .)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str10 id float(wave dist_next_jump) "p_10000620" 31 12 "p_10000620" 43 2 "p_10000620" 45 0 "p_10000620" 46 0 "p_10000620" 47 0 "p_10000620" 48 4 "p_10000620" 52 0 "p_10000620" 53 0 "p_10000620" 54 0 "p_10000620" 55 0 "p_10000620" 56 0 "p_10000620" 57 0 "p_10000701" 24 . "p_10000701" 25 . "p_10000701" 26 . "p_10000701" 27 . "p_10000701" 28 . "p_10000701" 29 . "p_10000701" 30 . "p_10000701" 31 . "p_10000701" 32 . "p_10000701" 33 . "p_10000701" 34 . "p_10000701" 35 . "p_10000701" 36 . "p_10000701" 37 . "p_10000701" 38 . "p_10000701" 39 . "p_10000701" 40 . "p_10000701" 41 . "p_10000701" 42 . "p_10000701" 43 . "p_10000701" 44 . "p_10000701" 45 . "p_10000701" 46 . "p_10000701" 47 . "p_10001328" 4 . "p_10001328" 5 . "p_10001328" 6 . "p_10001328" 7 . "p_10001328" 8 . "p_10001328" 9 . "p_10001328" 10 . "p_10001328" 11 . "p_10001328" 12 . "p_10001328" 13 . "p_10001328" 14 . "p_10001328" 15 . "p_10001328" 16 . "p_10001328" 17 . "p_10001328" 18 . "p_10001328" 19 . "p_10001328" 20 . "p_10001328" 21 . "p_10001328" 22 . "p_10001328" 23 . "p_10001328" 24 . "p_10001328" 25 . "p_10001328" 26 . "p_10001328" 27 . "p_10001328" 28 . "p_10001328" 29 . "p_10001328" 30 . "p_10001328" 31 . "p_10001328" 32 . "p_10001600" 38 . "p_10003492" 53 3 "p_10003492" 56 0 "p_10003859" 16 . "p_10004859" 24 . "p_10004859" 25 . "p_10004859" 26 . "p_10004859" 27 . "p_10004859" 28 . "p_10004859" 29 . "p_10004859" 30 . "p_10004859" 31 . "p_10004859" 32 . "p_10004859" 33 . "p_10004859" 34 . "p_10004859" 35 . "p_10004859" 36 . "p_10004859" 37 . "p_10004859" 38 . "p_10004859" 39 . "p_10004859" 40 . "p_10004859" 41 . "p_10004859" 42 . "p_10004859" 43 . "p_10004859" 44 . "p_10004859" 45 . "p_10004859" 46 . "p_10004859" 47 . "p_10006340" 41 . "p_10006340" 42 . "p_10006340" 43 . "p_10006340" 44 . "p_10006340" 45 . "p_10006340" 46 . "p_10006340" 47 . end
Comment