Announcement

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

  • generate a dummy variable

    Dear All, Suppose that I have this data set
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(id year D wanted)
    1 2010 .23 1
    1 2011 .25 1
    1 2012 .33 1
    1 2013 .41 1
    2 2010 .32 0
    2 2011 .43 0
    2 2012 .37 0
    2 2013 .29 0
    3 2010 .12 1
    3 2011 .18 1
    3 2012 .38 1
    3 2013 .25 1
    end
    For each company (id), if D is less than 0.3 in year 2011 AND D is in the range [0.3,0.4] in year 2012, the desired outcome is that wanted=1 for id=1. Otherwise, wanted=0. Any suggestions? Thanks.
    Last edited by River Huang; 14 Apr 2022, 03:35.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    if D is less than 0.3 in year 2011 AND D is in the range [0.3, 0.4]
    I've inserted a decimal point (typo, I guess) but I don't see that being less than 0.3 and in the interval 0.3 to 0.4 are compatible. Some other qualification is needed.

    Comment


    • #3
      Dear Nick, Thanks a lot. My bad. I have modified the question.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        You can specify the conditions separately:

        Code:
        bys id: egen cond1= max(D<0.3 & year==2011)
        bys id: egen cond2= max(inrange(D, 0.3, 0.4) & year==2012)
        gen wanted= cond1*cond2

        Comment


        • #5
          This works for the example:

          Code:
          egen count1 = total(year == 2011 & D < 0.3), by(id) 
          
          egen count2 = total(year == 2012 & inrange(D, 0.3, 0.4)), by(id)
          
          gen OK = count1 & count2
          But comparing with 0.3 and 0.4 is tricky if your variable is float, as often expained here.

          Code:
          .  gen foo = cond(_n == 1, 0.3, 0.4)
          
          . l foo in 1/2
          
               +-----+
               | foo |
               |-----|
            1. |  .3 |
            2. |  .4 |
               +-----+
          
          . count if foo == 0.3
            0
          
          . count if foo == float(0.3)
            1
          So, you really need to wrap your numbers inside float()

          Code:
          egen count1 = total(year == 2011 & D < float(0.3)), by(id)

          Comment


          • #6
            Dear Andrew, Thanks a lot for this helpful suggestion.
            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment


            • #7
              Dear Nick, Thanks a lot for this helpful suggestion.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment

              Working...
              X