Announcement

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

  • Grouping repeated observations in one variable

    Dear Colleagues


    I have a variable with the following values:

    1
    1
    1
    0
    0
    1
    1
    1
    1
    0
    1
    1
    1
    1
    0
    0
    1
    1

    I want stata to identify the longest spell/s (i.e. 4 consecutive 1s) and just keep those 1s and replace the other 1s (in the groups of 3 1s and 2 1s) with zeros.
    I can also achieve that by creating a new variable where it keeps the 1s with the longest spell, i.e. just the groups of 1s with the longest spells, and doesn't have the other groups of 1s. That is:

    0
    0
    0
    0
    0
    1
    1
    1
    1
    0
    1
    1
    1
    1
    0
    0
    0
    0

    After that, I would like to be estimate the probability of being in a spell of 4 1s, without conflating spells - thus I don't want to include the two sets of 4 1s. I can either ask Stata to delete the 'extra' set of 4 1s, or I can ask Stata to just use one set of 4 1s to estimate the probability.

    I'd be very grateful for your advice how to achieve this.

    Many thanks Sanghamitra

  • #2
    Sanghamitra, try this code.

    Code:
    clear
    input float varname
    1
    1
    0
    0
    0
    0
    1
    1
    1
    0
    1
    1
    1
    1
    0
    1
    1
    1
    1
    0
    0
    end
    
    gen group= varname != varname[_n-1]
    replace group = sum(group)
    bys group: gen spell =_N if varname == 1
    sum spell, meanonly
    gen varname_mod = spell==`r(max)'
    gen check = sum(varname_mod)
    gen varname_mod_f2= (varname_mod ==1) & (check <= `r(max)')

    Comment


    • #3
      Thank you so much! It works! best wishes, Sanghamitra

      Comment

      Working...
      X