Good evening:
Below is a sample of data from my data set. The last column, NoBust, is how I want the variable to appear:
I tried to do this in Stata, but I can't get it to appear in the table above. Instead, it appears as you see in the table below -- when the event, AllBuster, is 1, it resets to zero rather than continuing the series and starting zero after the event (as in my table above):
This is the code that I came up with below, but I can't for the life of me figure out how to get it to work the way I want. The data set is a panel data set (unbalanced). I have over 11,000 observations.
Any help would be greatly appreciated!
Below is a sample of data from my data set. The last column, NoBust, is how I want the variable to appear:
year | partnercode | targetcode | AllBuster | NoBust |
1972 | UK | Cuba | 0 | 0 |
1973 | UK | Cuba | 0 | 1 |
1972 | UK | Cuba | 1 | 2 |
1973 | UK | Cuba | 0 | 0 |
1974 | UK | Cuba | 0 | 1 |
1975 | UK | Cuba | 0 | 2 |
1976 | UK | Cuba | 0 | 3 |
1977 | UK | Cuba | 0 | 4 |
year | partnercode | targetcode | AllBuster | NoBust |
1972 | UK | Cuba | 0 | 0 |
1973 | UK | Cuba | 0 | 1 |
1972 | UK | Cuba | 1 | 0 |
1973 | UK | Cuba | 0 | 2 |
1974 | UK | Cuba | 0 | 3 |
1975 | UK | Cuba | 0 | 4 |
1976 | UK | Cuba | 0 | 5 |
1977 | UK | Cuba | 0 | 6 |
Code:
by partnercode targetcode (year), sort: gen nobust = (allbuster0 == 1) by partnercode targetcode (year): replace nobust = sum(nobust) by partnercode targetcode nobust (year), sort: gen nobust0 = _n-1 if nobust >=0 drop nobust
Comment