Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Duration variable

    Good evening:

    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
    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):
    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
    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.

    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
    Any help would be greatly appreciated!
    Last edited by Keith Preble; 12 Nov 2019, 01:07. Reason: put the right variable names in the table.

  • #2
    You have duplicates on your variables country country_2 year but you seem to want them to be treated differently. Also, there is inconsistency between your code and your data example in what the variables are called.

    To help anybody else, here is dataex code to reproduce your data (FAQ Advice #12). Otherwise I can't follow what you want and why.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str2 country str4 country_2 byte(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
    end

    Comment

    Working...
    X