Announcement

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

  • Creating a Dummy Variable in a Panel.

    Hello,

    I'm trying to create a dummy for the following Panel below. The data describes different companies, with their HQ Address, City and a yearly variable that goes from 2010 - 2018.

    What I'm trying to do is a dummy that equals 0 if the company has not changed HQ from the past year, and equal to 1 if there has been a change.

    I've read a few of the FAQ's about dummies and they've helped get a better understanding, but I still haven't got a clear idea how to tell stata that for a specifiec company year, to check if the value from the past year is the same.

    The dataex is below. I believe we will have no changes in this specific example, but in the bigger set there should be some.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 gvkey str88 conm str66 HQ str100 city float(year hqchange)
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2010 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2011 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2012 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2013 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2014 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2015 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2016 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2017 0
    "001004" "AAR CORP"                     "One AAR Place, 1100 North Wood Dale Road" "Wood Dale"    2018 0
    "001013" "ADC TELECOMMUNICATIONS INC"   "13625 Technology Drive"                   "Eden Prairie" 2010 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2010 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2011 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2012 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2013 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2014 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2015 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2016 0
    "001045" "AMERICAN AIRLINES GROUP INC"  "4333 Amon Carter Boulevard"               "Fort Worth"   2017 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2010 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2011 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2012 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2013 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2014 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2015 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2016 0
    "001050" "CECO ENVIRONMENTAL CORP"      "14651 North Dallas Parkway, Suite 500"    "Dallas"       2017 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2010 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2010 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2011 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2011 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2012 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2012 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2013 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2013 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2014 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2014 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2015 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2015 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2016 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2016 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2017 0
    "001062" "ASA GOLD AND PRECIOUS METALS" "400 South El Camino Real, Suite 710"      "San Mateo"    2017 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2010 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2011 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2012 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2013 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2014 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2015 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2016 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2017 0
    "001072" "AVX CORP"                     "One AVX Boulevard"                        "Fountain Inn" 2018 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2010 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2011 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2012 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2013 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2014 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2015 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2016 0
    "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2017 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2010 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2011 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2012 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2013 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2014 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2015 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2016 0
    "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2017 0
    "001078" "ABBOTT LABORATORIES"          "100 Abbott Park Road"                     "Abbott Park"  2010 0
    "001078" "ABBOTT LABORATORIES"          "100 Abbott Park Road"                     "Abbott Park"  2011 0
    "001078" "ABBOTT LABORATORIES"          "100 Abbott Park Road"                     "Abbott Park"  2012 0
    end

  • #2
    This should get you most of the way there:
    Code:
    bysort gvkey (year) : gen hqchange = (HQ != HQ[_n-1]) & (_n != 1)
    Within each gvkey, it sets the dummy equal to 1 if and only if the HQ has changed from the previous observation (i.e. the previous year) and if that observation is not the first observation.

    However I'm not sure how you want to handle duplicate observations, such as gvkey 001062 in the year 2010, which has two identical addresses.

    Comment


    • #3
      Here's a slightly different solution that doesn't include year. I shortened the data and added extra obs for AARON'S with a different address to show it works:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str6 gvkey str88 conm str66 HQ str100 city float(year hqchange)
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2010 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2011 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2012 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2013 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2014 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2015 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2016 0
      "001075" "PINNACLE WEST CAPITAL CORP"   "400 North 5th Street"                     "Phoenix"      2017 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2010 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2011 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2012 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2013 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2014 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2015 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2016 0
      "001076" "AARON'S INC"                  "400 Galleria Parkway SE, Suite 300"       "Atlanta"      2017 0
      "001076" "AARON'S INC"                  "123 XXX Road"                             "YYY"          2018 0
      "001078" "ABBOTT LABORATORIES"          "100 Abbott Park Road"                     "Abbott Park"  2010 0
      "001078" "ABBOTT LABORATORIES"          "100 Abbott Park Road"                     "Abbott Park"  2011 0
      "001078" "ABBOTT LABORATORIES"          "100 Abbott Park Road"                     "Abbott Park"  2012 0
      end
      
      
      egen address=group( gvkey HQ city) // new number for each new address
      
      *sort on address and see if the first address doesn't match last address
      *if doesn't match, code 1; if matches, code0
      bysort gvkey (address): gen change= cond( address[1]!=address[_N], 1, 0) 
      
      sort gvkey year //original sort
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Nils,

        That did the trick. It does not matter if they have repeated values for the small project I need to give back.

        However, could you explain, or lead me to a FAQ where the "n" in your code and N (in other codes) is explained? I keep seeing it repeated around here but not sure how to use it myself !

        Thanks for the help.

        Comment


        • #5
          https://stats.idre.ucla.edu/stata/se...from-_n-to-_n/

          Comment

          Working...
          X