Announcement

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

  • Condition on switching to new dummy variable

    Dear all,

    I am trying to create dummy variables under certain conditions. Every time that a certain variable takes value 1 I want to start a new dummy category, until this variable takes value 1 again. My data looks the following. The variables Number and R are the ones that I have in my data right now, the dummies are the ones that I wish to create. Every time the variable R takes value1 I want to switch to a new dummy. I really can't figure out how to get this done so any help would be highly appreciated!!
    Number R dummy1 dummy2 dummy3 dummy4
    1 0 1 0 0 0
    2 0 1 0 0 0
    3 0 1 0 0 0
    4 0 1 0 0 0
    5 1 0 1 0 0
    6 0 0 1 0 0
    7 1 0 0 1 0
    8 0 0 0 1 0
    9 0 0 0 1 0
    10 1 0 0 0 1

  • #2
    here is a two-step solution (please read the FAQ for future posts)
    Code:
    gen sum=sum(R)
    ta sum, gen(dummy)
    if you had told us why you wanted those, a better solution might be possible

    Comment


    • #3
      Thank you very much for the quick reply. I'm sorry I'm new to stata and it is my first time on this forum. I'll use the dummies as selection correction terms to control for selectivity in my dataset (I'll be using a semiparametric estimation approach). Since my dataset is rather large the above solution leads to too many dummies. Preferably I would put a restriction on the intervals that a new interval only starts when at least 20% of the observations in the interval are retired (namely R==1). I've been trying to figure out how to do this all day so if you have any suggestions I would be happy to hear them.

      thanks in advance

      Comment

      Working...
      X