Announcement

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

  • Generating a Dummy if two variables share something in common

    Hello again. I am working on constructing an IV, and have come across a seemingly complex way to formulate the dependent dummy variable that I need. The data I have has to do with currency pegging decisions by countries. A "leader" country is one that many other countries (followers) may decide to peg their currency to. The dummy variable I need to create would = 1 when two follower countries share the same leader. In the below example, both ARG and ECU peg their currencies to the USA. Then, for ARG and ECU, they bilaterally share a common currency leader. See the example below, I would like to create the "common_leader" variable.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
    2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
    2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
    2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
    2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
    2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
    2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 1
    2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 1
    end
    Alternatively, I could create separate "peg" variables for the different leader countries, as in the below example:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg_USA peg_GBR common_leader)
    2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0 0
    2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0 0
    2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0 0
    2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0 0
    2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0 0
    2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 0 1
    2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 0 1
    end
    I suspect this second way of formatting the data would simplify the conditional requirements for generating the dummy variable. I have a tough time figuring out how to construct these complex conditional statements. I have tried my hand at creating the gen statement a few times, but I believe I need to include the client variable, the country variables, and the "peg" variable. I am not even sure if these can all actually work together, and would certainly be open to suggestions for reformatting the data so I can create the common_leader variable. Thank you!

  • #2
    A more elaborate example is below:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
    2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
    2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
    2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
    2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
    2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
    2000 "ARG" "ECU" "ARG_ECU" ""    ""    0 1
    2000 "ECU" "ARG" "ECU_ARG" ""    ""    0 1
    2000 "XXX" "JPN" "XXX_JPN" "JPN" "XXX" 1 0
    2000 "YYY" "JPN" "YYY_JPN" "JPN" "YYY" 1 0
    2000 "XXX" "YYY" "XXX_YYY" ""    ""    0 1
    end
    
    frame put leader follower, into(wanted)
    frame wanted{
          contract leader follower, nomiss
          rename follower country_exp
          bys leader: keep if _N>1
          tempfile holding
          preserve
          rename country_exp country_imp
          save `holding', replace
          restore
          joinby leader using `holding'
          bys leader: keep if country_exp!= country_imp
    }
    frlink 1:1 country_exp country_imp, frame(wanted)
    frame drop wanted
    replace wanted= !missing(wanted)
    Res.:

    Code:
    . l year country_exp country_imp leader common_leader wanted, sep(0)
    
         +---------------------------------------------------------+
         | year   count~xp   count~mp   leader   common~r   wanted |
         |---------------------------------------------------------|
      1. | 2000        USA        ARG      USA          0        0 |
      2. | 2000        ARG        USA      USA          0        0 |
      3. | 2000        GBR        CHN      GBR          0        0 |
      4. | 2000        CHN        GBR      GBR          0        0 |
      5. | 2000        USA        ECU      USA          0        0 |
      6. | 2000        ARG        ECU                   1        1 |
      7. | 2000        ECU        ARG                   1        1 |
      8. | 2000        XXX        JPN      JPN          0        0 |
      9. | 2000        YYY        JPN      JPN          0        0 |
     10. | 2000        XXX        YYY                   1        1 |
         +---------------------------------------------------------+
    
    .

    Comment


    • #3
      Hi Andrew, this is by no means a graceful formulation, but this code sample will create an indicator variable for each bilateral trade pair, based on the country which is leading in monetary policy.
      Code:
      clear
      input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
      2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
      2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
      2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
      2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
      2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
      2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 1
      2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 1
      end
      drop if leader!="JPN"
      gen id=1
      gen jpnlead=1
      save leader1.dta, replace
      clear
      input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
      2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
      2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
      2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
      2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
      2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
      2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 1
      2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 1
      end
      drop if leader!="GBR"
      gen id=2
      gen gbrlead=1
      save leader2.dta, replace
      clear
      input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
      2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
      2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
      2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
      2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
      2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
      2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 1
      2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 1
      end
      drop if leader!="USA"
      gen id=3
      gen usalead=1
      save leader3.dta, replace
      append using leader1.dta
      append using leader2.dta
      save leader_all.dta, replace

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        A more elaborate example is below:
        Thank you very much! It appears to work as expected on the sample data. Unfortunately, I have extra caveats in my actual data. The most important one being that "year" also uniquely identifies if/when currency pegging occurs or not, i.e. ARG pegged its currency to the US in 2000, dropped it in 2003 or so, and then has continued it once more since 2005. I am trying to re-work your code to account for this, but am having some difficulties in doing so. Do you have any recommendations?

        What I have done is made the following changes:

        Code:
         frame put year leader follower, into(wanted)
        frame wanted{      
                   contract year leader follower, nomiss      
                   rename follower country_exp      
                   bys leader: keep if _N>1      
                   tempfile holding       preserve      
                   rename country_exp country_imp      
                   save `holding', replace      
                   restore      
                   joinby leader using `holding'      
                   bys leader: keep if country_exp!= country_imp }
        frlink m:1 year country_exp country_imp, frame(wanted)
        frame drop wanted
        replace wanted= !missing(wanted)
        I've tried both a 1:1 match and a m:1 match and I it appears as though the variables do not uniquely identify observations in the 'wanted' frame.
        Last edited by Andrew Bernal; 13 Sep 2022, 11:02.

        Comment


        • #5
          You need to define groups by year and leader. Otherwise, provide a reproducible example if there are issues with the code below.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
          2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
          2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
          2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
          2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
          2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
          2000 "ARG" "ECU" "ARG_ECU" ""    ""    0 1
          2000 "ECU" "ARG" "ECU_ARG" ""    ""    0 1
          2000 "XXX" "JPN" "XXX_JPN" "JPN" "XXX" 1 0
          2000 "YYY" "JPN" "YYY_JPN" "JPN" "YYY" 1 0
          2000 "XXX" "YYY" "XXX_YYY" ""    ""    0 1
          2001 "EUR" "ARG" "EUR_ARG" "EUR" "ARG" 1 0
          2001 "ARG" "EUR" "ARG_EUR" "EUR" "ARG" 1 0
          2001 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
          2001 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
          2001 "EUR" "ECU" "EUR_ECU" "EUR" "ECU" 1 0
          2001 "ARG" "ECU" "ARG_ECU" ""    ""    0 1
          2001 "ECU" "ARG" "ECU_ARG" ""    ""    0 1
          2001 "XXX" "USA" "XXX_USA" "USA" "XXX" 1 0
          2001 "YYY" "USA" "YYY_USA" "USA" "YYY" 1 0
          2001 "XXX" "YYY" "XXX_YYY" ""    ""    0 1
          end
          
          frame put year leader follower, into(wanted)
          frame wanted{
                contract year leader follower, nomiss
                rename follower country_exp
                bys year leader: keep if _N>1
                tempfile holding
                preserve
                rename country_exp country_imp
                save `holding', replace
                restore
                joinby year leader using `holding'
                bys year leader: keep if country_exp!= country_imp
          }
          frlink m:1 year country_exp country_imp, frame(wanted)
          frame drop wanted
          replace wanted= !missing(wanted)
          Res.:

          Code:
          . l year country_exp country_imp leader common_leader wanted, sep(0)
          
               +---------------------------------------------------------+
               | year   count~xp   count~mp   leader   common~r   wanted |
               |---------------------------------------------------------|
            1. | 2000        USA        ARG      USA          0        0 |
            2. | 2000        ARG        USA      USA          0        0 |
            3. | 2000        GBR        CHN      GBR          0        0 |
            4. | 2000        CHN        GBR      GBR          0        0 |
            5. | 2000        USA        ECU      USA          0        0 |
            6. | 2000        ARG        ECU                   1        1 |
            7. | 2000        ECU        ARG                   1        1 |
            8. | 2000        XXX        JPN      JPN          0        0 |
            9. | 2000        YYY        JPN      JPN          0        0 |
           10. | 2000        XXX        YYY                   1        1 |
           11. | 2001        EUR        ARG      EUR          0        0 |
           12. | 2001        ARG        EUR      EUR          0        0 |
           13. | 2001        GBR        CHN      GBR          0        0 |
           14. | 2001        CHN        GBR      GBR          0        0 |
           15. | 2001        EUR        ECU      EUR          0        0 |
           16. | 2001        ARG        ECU                   1        1 |
           17. | 2001        ECU        ARG                   1        1 |
           18. | 2001        XXX        USA      USA          0        0 |
           19. | 2001        YYY        USA      USA          0        0 |
           20. | 2001        XXX        YYY                   1        1 |
               +---------------------------------------------------------+
          
          .
          Last edited by Andrew Musau; 13 Sep 2022, 12:36.

          Comment


          • #6
            Sorry for the trouble, Andrew. Thanks again for your help. I believe the key issue is that the "peg" variable is unused in determining whether or not two countries share a common currency leader. In the example below, even though XXX and YYY never peg their currency to the same leader, the "wanted" variable shows as if they do. As written, the code appears to hinge on how many times a certain pairing comes up, and whether or not two separate countries share this pair.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
            2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
            2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
            2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
            2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
            2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
            2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 1
            2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 1
            2000 "ECU" "USA" "ECU_USA" "USA" "ECU" 1 0
            2001 "USA" "ARG" "USA_ARG" "USA" "ARG" 0 0
            2001 "ARG" "USA" "ARG_USA" "USA" "ARG" 0 0
            2001 "USA" "ECU" "USA_ARG" "USA" "ECU" 1 0
            2001 "ECU" "USA" "ARG_USA" "USA" "ECU" 1 0
            2001 "ARG" "ECU" "ARG_ECU" ""    ""    0 0
            2001 "ECU" "ARG" "ECU_ARG" ""    ""    0 0
            2001 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
            2001 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
            2001 "GBR" "XXX" "GBR_XXX" "GBR" "XXX" 0 0
            2001 "XXX" "GBR" "XXX_GBR" "GBR" "XXX" 0 0
            2001 "GBR" "YYY" "GBR_YYY" "GBR" "YYY" 0 0
            2001 "YYY" "GBR" "YYY_GBR" "GBR" "YYY" 0 0
            2001 "XXX" "YYY" "XXX_YYY" ""    ""    0 0
            2001 "YYY" "XXX" "YYY_XXX" ""    ""    0 0
            end
            Code:
            frame put year leader follower, into(wanted)
            frame wanted{
                  contract year leader follower, nomiss
                  rename follower country_exp
                  bys year leader: keep if _N>1
                  tempfile holding
                  preserve
                  rename country_exp country_imp
                  save `holding', replace
                  restore
                  joinby year leader using `holding'
                  bys year leader: keep if country_exp!= country_imp
            }
            frlink m:1 year country_exp country_imp, frame(wanted)
            frame drop wanted
            replace wanted= !missing(wanted)
            Code:
            l year country_exp country_imp countries leader follower peg common_leader wanted
            
                 +-------------------------------------------------------------------------------------+
                 | year   count~xp   count~mp   countr~s   leader   follower   peg   common~r   wanted |
                 |-------------------------------------------------------------------------------------|
              1. | 2000        USA        ARG    USA_ARG      USA        ARG     1          0        0 |
              2. | 2000        ARG        USA    ARG_USA      USA        ARG     1          0        0 |
              3. | 2000        GBR        CHN    GBR_CHN      GBR        CHN     0          0        0 |
              4. | 2000        CHN        GBR    CHN_GBR      GBR        CHN     0          0        0 |
              5. | 2000        USA        ECU    USA_ECU      USA        ECU     1          0        0 |
                 |-------------------------------------------------------------------------------------|
              6. | 2000        ARG        ECU    ARG_ECU        .          .     0          1        1 |
              7. | 2000        ECU        ARG    ECU_ARG        .          .     0          1        1 |
              8. | 2000        ECU        USA    ECU_USA      USA        ECU     1          0        0 |
              9. | 2001        USA        ARG    USA_ARG      USA        ARG     0          0        0 |
             10. | 2001        ARG        USA    ARG_USA      USA        ARG     0          0        0 |
                 |-------------------------------------------------------------------------------------|
             11. | 2001        USA        ECU    USA_ARG      USA        ECU     1          0        0 |
             12. | 2001        ECU        USA    ARG_USA      USA        ECU     1          0        0 |
             13. | 2001        ARG        ECU    ARG_ECU                         0          0        1 |
             14. | 2001        ECU        ARG    ECU_ARG                         0          0        1 |
             15. | 2001        GBR        CHN    GBR_CHN      GBR        CHN     0          0        0 |
                 |-------------------------------------------------------------------------------------|
             16. | 2001        CHN        GBR    CHN_GBR      GBR        CHN     0          0        0 |
             17. | 2001        GBR        XXX    GBR_XXX      GBR        XXX     0          0        0 |
             18. | 2001        XXX        GBR    XXX_GBR      GBR        XXX     0          0        0 |
             19. | 2001        GBR        YYY    GBR_YYY      GBR        YYY     0          0        0 |
             20. | 2001        YYY        GBR    YYY_GBR      GBR        YYY     0          0        0 |
                 |-------------------------------------------------------------------------------------|
             21. | 2001        XXX        YYY    XXX_YYY                         0          0        1 |
             22. | 2001        YYY        XXX    YYY_XXX                         0          0        1 |
                 +-------------------------------------------------------------------------------------+
            Your attempts are greatly appreciated. I am unsure if this method will work, but I unfortunately can't think of any alternatives at the moment. I thought something that utilized the "peg" variable would be ideal, but not sure if that is even possible given the way the data is formatted.

            Comment


            • #7
              I have been assuming that if there is a leader, then the currency is pegged. However, the modification is then just to restrict the sample to the pegged observations. Am I missing something?

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input int year str3(country_exp country_imp) str7 countries str3(leader follower) byte(peg common_leader)
              2000 "USA" "ARG" "USA_ARG" "USA" "ARG" 1 0
              2000 "ARG" "USA" "ARG_USA" "USA" "ARG" 1 0
              2000 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
              2000 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
              2000 "USA" "ECU" "USA_ECU" "USA" "ECU" 1 0
              2000 "ARG" "ECU" "ARG_ECU" "."   "."   0 1
              2000 "ECU" "ARG" "ECU_ARG" "."   "."   0 1
              2000 "ECU" "USA" "ECU_USA" "USA" "ECU" 1 0
              2001 "USA" "ARG" "USA_ARG" "USA" "ARG" 0 0
              2001 "ARG" "USA" "ARG_USA" "USA" "ARG" 0 0
              2001 "USA" "ECU" "USA_ARG" "USA" "ECU" 1 0
              2001 "ECU" "USA" "ARG_USA" "USA" "ECU" 1 0
              2001 "ARG" "ECU" "ARG_ECU" ""    ""    0 0
              2001 "ECU" "ARG" "ECU_ARG" ""    ""    0 0
              2001 "GBR" "CHN" "GBR_CHN" "GBR" "CHN" 0 0
              2001 "CHN" "GBR" "CHN_GBR" "GBR" "CHN" 0 0
              2001 "GBR" "XXX" "GBR_XXX" "GBR" "XXX" 0 0
              2001 "XXX" "GBR" "XXX_GBR" "GBR" "XXX" 0 0
              2001 "GBR" "YYY" "GBR_YYY" "GBR" "YYY" 0 0
              2001 "YYY" "GBR" "YYY_GBR" "GBR" "YYY" 0 0
              2001 "XXX" "YYY" "XXX_YYY" ""    ""    0 0
              2001 "YYY" "XXX" "YYY_XXX" ""    ""    0 0
              end
              
              frame put year leader follower if peg, into(wanted)
              frame wanted{
                    contract year leader follower, nomiss
                    rename follower country_exp
                    bys year leader: keep if _N>1
                    tempfile holding
                    preserve
                    rename country_exp country_imp
                    save `holding', replace
                    restore
                    joinby year leader using `holding'
                    bys year leader: keep if country_exp!= country_imp
              }
              frlink m:1 year country_exp country_imp, frame(wanted)
              frame drop wanted
              replace wanted= !missing(wanted)
              Res.:

              Code:
              . l year country_exp country_imp countries leader follower peg common_leader wanted, sep(0)
              
                   +-------------------------------------------------------------------------------------+
                   | year   count~xp   count~mp   countr~s   leader   follower   peg   common~r   wanted |
                   |-------------------------------------------------------------------------------------|
                1. | 2000        USA        ARG    USA_ARG      USA        ARG     1          0        0 |
                2. | 2000        ARG        USA    ARG_USA      USA        ARG     1          0        0 |
                3. | 2000        GBR        CHN    GBR_CHN      GBR        CHN     0          0        0 |
                4. | 2000        CHN        GBR    CHN_GBR      GBR        CHN     0          0        0 |
                5. | 2000        USA        ECU    USA_ECU      USA        ECU     1          0        0 |
                6. | 2000        ARG        ECU    ARG_ECU        .          .     0          1        1 |
                7. | 2000        ECU        ARG    ECU_ARG        .          .     0          1        1 |
                8. | 2000        ECU        USA    ECU_USA      USA        ECU     1          0        0 |
                9. | 2001        USA        ARG    USA_ARG      USA        ARG     0          0        0 |
               10. | 2001        ARG        USA    ARG_USA      USA        ARG     0          0        0 |
               11. | 2001        USA        ECU    USA_ARG      USA        ECU     1          0        0 |
               12. | 2001        ECU        USA    ARG_USA      USA        ECU     1          0        0 |
               13. | 2001        ARG        ECU    ARG_ECU                         0          0        0 |
               14. | 2001        ECU        ARG    ECU_ARG                         0          0        0 |
               15. | 2001        GBR        CHN    GBR_CHN      GBR        CHN     0          0        0 |
               16. | 2001        CHN        GBR    CHN_GBR      GBR        CHN     0          0        0 |
               17. | 2001        GBR        XXX    GBR_XXX      GBR        XXX     0          0        0 |
               18. | 2001        XXX        GBR    XXX_GBR      GBR        XXX     0          0        0 |
               19. | 2001        GBR        YYY    GBR_YYY      GBR        YYY     0          0        0 |
               20. | 2001        YYY        GBR    YYY_GBR      GBR        YYY     0          0        0 |
               21. | 2001        XXX        YYY    XXX_YYY                         0          0        0 |
               22. | 2001        YYY        XXX    YYY_XXX                         0          0        0 |
                   +-------------------------------------------------------------------------------------+

              Comment


              • #8
                It looks as though this works brilliantly, thank you very much. Perhaps better variable names would be "potential leader" and "potential follower", where peg = 1 tells us that there is indeed a leader-follower relationship between the two.

                Comment

                Working...
                X