Announcement

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

  • Creating a variable that only contains initial year of implementation

    Hello all,

    I have a quick question, and it might be straightforward. (However, I could not figure it out in an efficient way)
    My data has STATEID, YEAR, TREATMENT, and other covariates. What I wanted to do was to create a relative year. So, first, I wanted to create a variable that includes only the first start year of the treatment.
    For example, "GA" is treated from 2000 through 2020, but "FL" is treated from 2005 through 2018. Then, the new treatment variable for GA should be only equal to 1 if the year is 2000 and 2005 for FL (0 for all other years).
    May I ask how I can solve this problem?

    Thank you!

  • #2
    Code:
    bysort stateid (year): gen wanted = year if treatment
    bysort stateid (wanted): replace wanted = wanted[1]

    Comment


    • #3
      Hi,

      Thank you for your assistance! But, may I ask if I could create a variable that is equal to 1 if that is the initial treatment year and all others equal to 0?

      Comment


      • #4
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str2 stateid int year byte treatment
        "GA" 1999 0
        "GA" 2000 1
        "GA" 2001 1
        "GA" 2002 1
        "FL" 2003 0
        "FL" 2004 0
        "FL" 2005 1
        "FL" 2006 1
        end
        
        egen x = min(year) if treatment, by(stateid)
        gen wanted = x == year
        
        list stateid year treatment wanted, clean
        
               stateid   year   treatm~t   wanted  
          1.        GA   1999          0        0  
          2.        GA   2000          1        1  
          3.        GA   2001          1        0  
          4.        GA   2002          1        0  
          5.        FL   2003          0        0  
          6.        FL   2004          0        0  
          7.        FL   2005          1        1  
          8.        FL   2006          1        0

        Comment


        • #5
          @Øyvind Snilsberg is bang on here, but for a very recent thread with more discussion and links see

          https://www.statalist.org/forums/for...st-observation

          Comment


          • #6
            Thank you all! It worked and I appreciate your help again.

            Comment

            Working...
            X