Announcement

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

  • How to create a Dummy for country-year in a Panel

    Good afternoon,

    I'm quite new here and would like to know if somebody could help me with this. (I tried to find the answer prior to post but my lack of experience perhaps prevailed)

    I'm working with panel data, around 19 countries, and 20 years. I have a variable X, I need to create a dummy per country and year where the country had a value out of an interval of 3 to -3. In other words:

    X>3 or <-3 In 2001 for Argentina, then I would need a dummy ARG2001=1 only for Argentina in year 2001. And go on... I'm creating variables following these steps:

    1) I identified countries and years with X>3 or X<-3...
    2) With these commands I'm creating the dummies:

    generate ARG2001=0
    replace ARG2001=1 if countrycode=="ARG" & year==2001


    It is working but it is too laborious because I have approximately 9 different countries that repeat in some years then... Until now I have created around 30 dummies and have just finished 2 out of the 9 countries. The other countries have not many observations out of the interval but I think I could optimize my time if there were a way to do this more quickly...

    Thank you for your feedback and suggestions.







  • #2
    I can imagine some code that would do this, and could make it real if example data were provided. But this sounds like a bad idea to start with. I don't imagine that these dummy variables will ultimately be useful in Stata and I suspect there is some much better way to organize the information for future analysis. If you post back with example data and describe where you are going with this and how you were planning to use these variables, I would be happy to suggest a better approach, or, if indeed I can't think of a better approach, write code that will do what you have asked.

    When posting back with example data, please use the -dataex- command. If you are running version 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Rafael:
      you may want to consider the -group- function available from -egen-.
      Something like (sorry, but I cannot check what follows at the moment):
      Code:
      egen wanted=group(country year)
      You can also add an -if- qualifier to limit the aforementioned code.

      PS: crossed in the cyberspace with Clyde's helpful reply.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        if your variables are -country-, -year-, and -x-, then this should work:

        Code:
        levelsof country, local(clist)
        foreach C in `clist' {
            levelsof year if country=="`C'", local(yrs)
            foreach Y in `yrs' {
                  gen `C'_`Y'=cond(country=="`C'" & year==`Y',inrange(x,-3,3),.)
            }
        }
        Though I agree with Clyde that you probably don't want to do this.

        hth,
        Jeph

        Comment


        • #5
          I appreciate the feedback. I run the command dataex the data is some long and just had what you can see below. My dependent is y_growth1 and my independent is int_i. With the dummy I want to control for each country-year that int_i is out of the interval (-3, 3).
          countrycode Year y_growth1 int_i
          "ARG" 2000 . .
          "ARG" 2001 -.07730749 -6.849315
          "ARG" 2002 -.08293445 -7.352941
          "ARG" 2003 .04237716 -.9523876
          "ARG" 2004 .06515008 .9615452
          "ARG" 2005 .0575054 -3.4920676
          "ARG" 2006 .08487426 .8223715
          "ARG" 2007 .09432038 2.9363756
          "ARG" 2008 .06321697 -1.42631
          "ARG" 2009 -.04931656 -3.215431
          "ARG" 2010 .12585178 -3.322256
          "ARG" 2011 .11179031 .5154593
          "ARG" 2012 .0016870876 -8.888888
          "ARG" 2013 -.000736909 -3.564729
          "ARG" 2014 -.03787893 -8.75486
          "ARG" 2015 .030554887 10.234542
          "ARG" 2016 -.013717166 7.350099
          "ARG" 2017 .11505152 7.747745
          "ARG" 2018 -.009815574 -3.3444784
          "ARG" 2019 -.04243257 -4.844294
          "BOL" 2000 . .
          "BOL" 2001 .014602823 -2.285712
          "BOL" 2002 .02172002 -1.1695895
          "BOL" 2003 .04921305 -1.3313632
          "BOL" 2004 .06772531 -.5996996
          "BOL" 2005 .04029566 -1.659127
          "BOL" 2006 .0923805 -1.3803705
          "BOL" 2007 .036288917 -2.9549
          "BOL" 2008 .08323158 -.8012775
          "BOL" 2009 .0039487164 3.231015
          "BOL" 2010 .10421307 -.3129888
          "BOL" 2011 .09253836 -.9419143
          "BOL" 2012 .10308113 1.109353
          "BOL" 2013 .07935067 .15673593
          "BOL" 2014 .03367861 -2.3474195
          "BOL" 2015 .005511695 -2.2435877
          "BOL" 2016 .028078616 2.7868865
          "BOL" 2017 .09619202 0
          "BOL" 2018 .03841024 .4784722
          "BOL" 2019 .004780929 -.31746
          "BRA" 2000 . .
          "BRA" 2001 -.019495847 .50675225
          "BRA" 2002 .0026630075 6.050423
          "BRA" 2003 -.01251654 -3.169569
          "BRA" 2004 .04355808 2.6186554
          "BRA" 2005 .010943453 1.754388
          "BRA" 2006 .08021485 .15673593
          "BRA" 2007 .1006018 -.4694794
          "BRA" 2008 .09726756 2.2012558
          "BRA" 2009 .034971107 -.15384968
          "BRA" 2010 .14875293 2.619416
          "BRA" 2011 .10358921 .3003
          "BRA" 2012 -.011561276 -.7484987
          "BRA" 2013 .008038804 -1.960786
          "BRA" 2014 -.01235197 -1.53846
          "BRA" 2015 -.06849828 -3.1250045
          "BRA" 2016 -.05434143 4.032258
          "BRA" 2017 .00243399 1.3953513
          "BRA" 2018 .021318126 .3058101
          "BRA" 2019 .002756393 1.0670758
          "CHL" 2000 . .
          "CHL" 2001 -.014490482 2.9569864
          "CHL" 2002 .0005898625 .652744
          "CHL" 2003 .03228585 1.815822
          "CHL" 2004 .11308695 0
          "CHL" 2005 .0770928 0
          "CHL" 2006 .16803217 .25477684
          "CHL" 2007 .07729414 1.2706468
          "CHL" 2008 -.029964147 -1.0037632
          "CHL" 2009 .017062044 .12674561
          "CHL" 2010 .1459401 .50632864
          "CHL" 2011 .0858234 .12594146
          "CHL" 2012 .020574724 -1.1320714
          "CHL" 2013 .035114158 1.2722634
          "CHL" 2014 -.002488458 -.25125605
          "CHL" 2015 -.011220755 -.25188893
          "CHL" 2016 -.019854417 0
          "CHL" 2017 .0125542 0
          "CHL" 2018 .019864194 .50505
          "CHL" 2019 -.0034359465 -1.3819113
          "COL" 2000 . .
          "COL" 2001 -.021139177 2.546691
          "COL" 2002 .002518102 1.986753
          "COL" 2003 .0179171 1.1363665
          "COL" 2004 .04233582 2.407706
          "COL" 2005 .03951067 1.5673966
          "COL" 2006 .08226939 .6172833
          "COL" 2007 .08879501 .9202445
          "COL" 2008 .0729117 1.3677835
          "COL" 2009 .018102119 .1499213
          "COL" 2010 .07772259 .5988019
          "COL" 2011 .10094798 .7440505
          "COL" 2012 .02875707 1.1816828
          "COL" 2013 .01934868 -1.0218934
          "COL" 2014 .014787737 -2.2123907
          "COL" 2015 -.010194992 -.150833
          "COL" 2016 -.012155195 .9063436
          "COL" 2017 -.0004177218 0
          "COL" 2018 .0234227 .4491049
          "COL" 2019 .016002068 1.6393462

          Comment


          • #6
            Based on what you have described, I do not see any reason why you need a separate variable for each country and year. It seems to me all you need is a single indicator for whether int_i is in the -3 to 3 range.

            Code:
            gen byte out_of_range = !inrange(int_i, -3, 3)
            And whether you are doing a panel regression model, or doing separate regressions for each country, all you need to include in the regression is that one variable. For example:

            Code:
            encode countrycode, gen(n_countrycode)
            xtset n_countrycode Year
            
            xtreg y_growth1 i.out_of_range##c.int_i, fe vce(cluster n_countrycode)
            margins out_of_range, dydx(int_i)
            will give you models of the effect of c.int on y_growth1 both in country-year pairs that have -3 <= int_i <= 3 and separately those that do not, along with an estimate of any "shock" to y_growth1 associated with int_i being outside that range. (I'm assuming you have enough different countries to use cluster-robust variance in your real data.)

            Or if you don't want out_of_range to change the marginal effect of int_i, and you just want a shock from int_i -xtreg y_growth1 i.out_of_range int_i, fe vce(cluster n_countrycode)-.

            Or -xtreg y_growth1 i.out_of_range##i.n_countrycode int_i, fe vce(cluster n_countrycode)- will give you country-specific shocks to growth1 from out_of_range.

            On the other hand, if you have some analysis in mind that is specific to combinations of country-year, it seems not possible with your data. You appear to have true panel data, so each combination of country and year appears in only a single observation, and there are no models you can build with just a single observation.

            Comment


            • #7
              Code:
              g outside = int_i<-3 | int_i>3 if int_i!=.
              levelsof country, local(levelcountry)
              levelsof year, local(levelyear)
              foreach c of local levelcountry {
                  foreach y of local levelyear {
                      g d_`c'_`y' = outside
                  }
              }

              Comment

              Working...
              X