Count number of dummy variables
Posted: Wed Aug 02, 2023 10:14 am
Hi,
I have a dummy variable that is either 1 or NA called "below50". I want to create a series that counts how many consecutive 1's there are and returns to 0 when it encounters an NA.
EX:
below50 CUM
NA 0
NA 0
NA 0
1 1
1 2
1 3
1 4
NA 0
NA 0
NA 0
1 1
NA 0
As a second job I would also like to see this CUM series count in reverse so that if below50 is NA, it counts how many consecutive NAs there are but incrementing from -1, -2, -3 etc...
Ex:
EX:
below50 CUM2
NA -1
NA -2
NA -3
1 1
1 2
1 3
1 4
NA -1
NA -2
NA -3
1 1
NA -1
I have a dummy variable that is either 1 or NA called "below50". I want to create a series that counts how many consecutive 1's there are and returns to 0 when it encounters an NA.
EX:
below50 CUM
NA 0
NA 0
NA 0
1 1
1 2
1 3
1 4
NA 0
NA 0
NA 0
1 1
NA 0
As a second job I would also like to see this CUM series count in reverse so that if below50 is NA, it counts how many consecutive NAs there are but incrementing from -1, -2, -3 etc...
Ex:
EX:
below50 CUM2
NA -1
NA -2
NA -3
1 1
1 2
1 3
1 4
NA -1
NA -2
NA -3
1 1
NA -1