Announcement

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

  • Dummy Variable with multiple conditions

    Good afternoon, I am trying to create dummy variables with two conditions 1) name of province 2) year law was passed. Laws were not passed at the same time and what I have is panel data. So far my option is to use the following code, meaning i will have to do this for all provinces. Is there a way I can run one code for all provinces?

    gen dummy=1 if province=="myprovince" & year>=1995
    replace dummy=0 if dummy==.
    tab dummy province, mi
    tab dummy year, mi


  • #2
    Well, if you have only a small number of provinces in your data set, what you have shown would be on the right track. But it would need some tweaking. With more than one province, you can't -gen dummy- repeatedly. Also, in your post you speak of creating variables. Do you really need a separate such variable for each province? I'm trying to think of a situation where you would need that, and not coming up with anything. It is more likely you will need a single variable that is 1 for each observation in which the year is greater than or equal to the year that that observation's province passed the law. (For example, if you are studying some effect of the law's being passed with a difference-in-differences estimator, this is what you would need.)

    If you only have a small number of provinces, then a modification of what you showed in #1 will work:
    Code:
    gen wanted = 0
    replace wanted = 1 if province == "my_province_1" & year >= year_1 ///
        | province == "my_province_2" & year >= year_2 ///
        | province == "my_province_3" & year >= year_3
    // etc.
    In the above, of course, replace all the italicized material with actual names of provinces and corresponding years the law passed.

    If you have a large number of provinces, that approach will be tedious, error prone, and impractical. In that case, what you need is a new Stata data set that contains exactly two variables: province and year_law_passed. Perhaps you already have such a data set: where are you even getting the information about which year the law passed in each province from? It is perhaps a source that is readily converted into a data set. Worst case, you have to create such a data set in the data editor by hand--but that's still less burdensome than writing all the code. Once you have that data set in hand, let's call it provincial_pass_dates.dta, then you open your panel data set and then run:
    Code:
    merge m:1 province using provincial_pass_dates
    gen byte wanted = year >= year_law_passed
    and you're done

    Comment


    • #3
      Thank you so much Clyde. I think what is applicable in my case is the previous code. it should not take too long because there are only 10 provinces I am evaluating. I have to manually create the treatment periods so the code you have given above is not too burdensome

      Comment

      Working...
      X