Page 1 of 1

Count number of dummy variables

Posted: Wed Aug 02, 2023 10:14 am
by Andrew_Gillich
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

Re: Count number of dummy variables

Posted: Thu Aug 03, 2023 11:20 am
by EViews Matt
Hello,

Try these...

Code: Select all

series cum = @recode(@isna(below50), 0, @nan(cum(-1), 0) + 1)
series cum2 = @recode(@eqna(below50,below50(-1)), @nan(cum2(-1), 0), 0) + @recode(@isna(below50), -1, 1)