Announcement

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

  • Creating a Treatment Variable

    I want to create a treatment variable, say "Treat1" which should assign the value 1 for all counties if they fulfill a condition

    gen Treat=1 for all counties if <some condition is true> and if <condition is false> then 0.

    The following code does not work

    foreach i in 1/3{
    gen Treat_Min_t13 = cond(Hurr_Surface`i'>25 & Hurr_Surface`i'<49,1,0)
    }

  • #2
    You are trying to -gen- a single variable, Treat_Min_t13,three times. But you can only -gen- a variable once. So when you hit the second iteration of the loop, you get an error because the variable you are asking Stata to -gen- already exists.

    Now, your question does not adequately describe what you want to do here. You say you want to
    gen Treat=1 for all counties if <some condition is true> and if <condition is false> then 0
    But what are the counties? Are there three counties: is that what i in your loop is indexing? Or are you trying to create three separate variables Treat_Min_t13_1, Treat_Min_t13_2, and Treat_Min_t13_3, corresponding, respectively to applying the condition to Hurr_Surface1, Hurr_Surface2, and Hurr_Surface3, respectively. And if it is the latter, how are the counties represented in the data set.

    You probably would have your best chance of getting a timely and helpful response to this question if you post example data (using the -dataex- command, of course), and also hand-calculate what you want for several representative cases and show what the results would be for those.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Clyde,
      I have county-wise fips codes, I want the treatment variable to have value 1 for all the counties that are ever treated.

      clear
      input double fips float(year Hurr_Surface1 Hurr_Surface2 Hurr_Surface3)
      1001 1990 0 0 0
      1001 1991 2.4840844 0 0
      1001 1992 1.657727 2.4840844 0
      1001 1993 7.086315 1.657727 2.4840844
      1001 1994 .9359791 7.086315 1.657727
      1001 1995 12.246908 .9359791 7.086315
      1001 1996 24.52252 12.246908 .9359791
      1001 1997 5.921565 24.52252 12.246908
      1001 1998 11.394767 5.921565 24.52252
      1001 1999 10.873677 11.394767 5.921565
      1001 2000 2.5537634 10.873677 11.394767
      1001 2001 7.096999 2.5537634 10.873677
      1001 2002 13.832582 7.096999 2.5537634
      1001 2003 8.882021 13.832582 7.096999
      1001 2004 8.471467 8.882021 13.832582
      1001 2005 20.98227 8.471467 8.882021
      1001 2006 18.030645 20.98227 8.471467
      1001 2007 5.218914 18.030645 20.98227
      1001 2008 6.696115 5.218914 18.030645
      1001 2009 10.39697 6.696115 5.218914
      1001 2010 10.13859 10.39697 6.696115
      1001 2015 0 0 0
      1001 2016 2.0183258 0 0
      1001 2017 9.277674 2.0183258 0
      1001 2018 12.68209 9.277674 2.0183258
      1003 1990 0 0 0
      1003 1991 3.091592 0 0
      1003 1992 1.669577 3.091592 0
      1003 1993 9.193682 1.669577 3.091592
      1003 1994 1.3138485 9.193682 1.669577
      1003 1995 17.694239 1.3138485 9.193682
      1003 1996 29.57002 17.694239 1.3138485
      1003 1997 7.392604 29.57002 17.694239
      1003 1998 27.337364 7.392604 29.57002
      1003 1999 27.40316 27.337364 7.392604
      1003 2000 4.54734 27.40316 27.337364
      1003 2001 12.470327 4.54734 27.40316
      1003 2002 17.278978 12.470327 4.54734
      1003 2003 21.183945 17.278978 12.470327
      1003 2004 10.82108 21.183945 17.278978
      1003 2005 43.92965 10.82108 21.183945
      1003 2006 32.64604 43.92965 10.82108
      1003 2007 6.160767 32.64604 43.92965
      1003 2008 7.534112 6.160767 32.64604
      1003 2009 13.23934 7.534112 6.160767
      1003 2010 14.859164 13.23934 7.534112
      1003 2014 0 0 0
      1003 2015 1.442394 0 0
      1003 2016 1.899663 1.442394 0
      1003 2017 8.43302 1.899663 1.442394
      1003 2018 21.99539 8.43302 1.899663
      1005 1990 0 0 0
      1005 1991 3.91561 0 0
      1005 1992 2.563345 3.91561 0
      1005 1993 4.5991488 2.563345 3.91561
      1005 1994 .9101417 4.5991488 2.563345
      1005 1995 15.035724 .9101417 4.5991488
      1005 1996 24.291716 15.035724 .9101417
      1005 1997 8.943632 24.291716 15.035724
      1005 1998 10.36761 8.943632 24.291716
      1005 1999 14.748757 10.36761 8.943632
      1005 2000 3.992774 14.748757 10.36761
      1005 2001 11.184711 3.992774 14.748757
      1005 2002 16.831526 11.184711 3.992774
      1005 2003 10.283 16.831526 11.184711
      1005 2004 5.722895 10.283 16.831526
      1005 2005 15.25844 5.722895 10.283
      1005 2006 17.045044 15.25844 5.722895
      1005 2007 8.095157 17.045044 15.25844
      1005 2008 7.397526 8.095157 17.045044
      1005 2009 13.7024 7.397526 8.095157
      1005 2010 11.83578 13.7024 7.397526
      1005 2014 0 0 0
      1005 2015 3.2952394 0 0
      1005 2016 2.625578 3.2952394 0
      1005 2017 15.125016 2.625578 3.2952394
      1005 2018 11.227647 15.125016 2.625578
      1007 1991 0 0 0
      1007 1992 1.428912 0 0
      1007 1994 0 9.568542 1.428912
      1007 1995 9.643056 0 9.568542
      1007 1996 16.722881 9.643056 0
      1007 1997 4.3788304 16.722881 9.643056
      1007 1998 10.141002 4.3788304 16.722881
      1007 2000 0 10.460054 10.141002
      1007 2002 0 5.765936 0
      1007 2003 9.594743 0 5.765936
      1007 2004 10.24877 9.594743 0
      1007 2005 19.3885 10.24877 9.594743
      1007 2006 15.186228 19.3885 10.24877
      1007 2007 3.9304466 15.186228 19.3885
      1007 2008 5.431924 3.9304466 15.186228
      1007 2009 10.293672 5.431924 3.9304466
      1007 2010 8.883816 10.293672 5.431924
      1007 2014 0 0 0
      1007 2015 1.6242148 0 0
      1007 2016 2.50214 1.6242148 0
      1007 2017 6.764701 2.50214 1.6242148
      1007 2018 14.145787 6.764701 2.50214
      1009 1990 0 0 0
      end
      [/CODE]

      Comment


      • #4
        Thank you for the data example. But you still have not clearly explained or shown what you want the result to be.
        1. What does treated mean? Does it mean all three of Hurr_Surf1, Hurr_Surf2, and Hurr_Surf3 are between 25 and 49 (excluding both endpoints)? Or does it mean any one of those three variables is in that range? Or does it mean something else altogether, and, if so, what?
        2. Does "I want the treatment variable to have value 1 for all the counties that are ever treated" mean that if a county is treated (whatever that turns out to mean) in any year then the variable is to be 1 in every year for that county? Or do you just want it to be 1 for the year(s) in which that county is treated?

        Comment


        • #5
          Answers:

          1. I want that Treated =1 if any one of those three variables fall in the specified range.

          2. Yes, I want Treated =1 if it was ever treated. Hence, if a county is treated (whatever that turns out to mean) in any year then the variable is to be 1 in every year for that county--This is what I want.

          Comment


          • #6
            Code:
            gen byte treated_now = 0
            forvalues i = 1/3 {
                replace treated_now = 1 if inrange(Hurr_Surface`i', 25, 49) ///
                    & !inlist(Hurr_Surface`i', 25, 49)
            }
            by fips (year), sort: egen byte treated = max(treated_now)

            Comment


            • #7
              Thanks Clyde!

              Comment

              Working...
              X