Announcement

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

  • Generating Multiple products for bilateral pairs for 21 years

    Hi all,

    Relatively new to stata. I'm using Stata 18 on windows. I'm working on bilateral pairs within free trade agreements. I've created these country pairs (both ways) with some information contained in several variables (EnvProvs_Total EnvProvs_Sub FTA) I will like to create multiple export products (about 160) for each country pair for each of the 21years (ranging from 2000-2020.) For each of this newly generated rows, I will like to keep the same information, depending on the variable Year_Force. For those years before 2000, I will assume they take effect in 2000 and retain the information in those variables (EnvProvs_Total EnvProvs_Sub FTA) but for those after 2000, I'd like the information retained only from the year they took effect. For the previous years, id like to equate them to zero as well as edit some of them to specific values.

    Please find below the example dataset;



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str44 Exporter int ExpCode str3 ExpISO str44 Importer int ImpCode str3 ImpISO str12 Year_Force byte(EnvProvs_Total EnvProvs_Sub FTA)
    "ANTIGUA AND BARBUDA"               28 "ATG" "ESTONIA"        233 "EST" "2008" 23 7 1
    "BONAIRE, SINT EUSTATIUS AND SABA" 535 "BES" "IRELAND"        372 "IRL" "1971" 13 5 1
    "BULGARIA"                         100 "BGR" "UKRAINE"        804 "UKR" "2014" 33 7 1
    "CYPRUS"                           196 "CYP" "PORTUGAL"       620 "PRT" "2014" 34 7 1
    "CYPRUS"                           196 "CYP" "SWITZERLAND"    756 "CHE" "1973"  1 1 1
    "ESTONIA"                          233 "EST" "UNITED KINGDOM" 826 "GBR" "2014" 34 7 1
    "FINLAND"                          246 "FIN" "NICARAGUA"      558 "NIC" "2013" 32 7 1
    "FRANCE"                           250 "FRA" "MOLDOVA"        498 "MDA" "2014" 34 7 1
    "INDIA"                            356 "IND" "MALAYSIA"       458 "MYS" "2011"  4 2 1
    "INDIA"                            356 "IND" "URUGUAY"        858 "URY" "2009"  4 3 1
    "ISRAEL"                           376 "ISR" "SWITZERLAND"    756 "CHE" "1993"  3 3 1
    "KENYA"                            404 "KEN" "BURUNDI"        108 "BDI" "1994" 24 7 1
    "MADAGASCAR"                       450 "MDG" "CONGO DR"       180 "COD" "1994" 24 7 1
    "PALESTINE"                        275 "PSE" "AUSTRIA"         40 "AUT" "1997" 12 5 1
    "PERU"                             604 "PER" "SINGAPORE"      702 "SGP" "2017" 34 7 1
    "SAINT VINCENT AND THE GRENADINES" 670 "VCT" "AUSTRIA"         40 "AUT" "2008" 23 7 1
    "SERBIA"                           688 "SRB" "NETHERLANDS"    528 "NLD" "2010" 17 6 1
    "SUDAN"                            729 "SDN" "BAHRAIN"         48 "BHR" "1998"  2 2 1
    "SWITZERLAND"                      756 "CHE" "GREECE"         300 "GRC" "1973"  1 1 1
    "ZAMBIA"                           894 "ZMB" "LIBYA"          434 "LBY" "1994" 24 7 1
    end


    Please is anyone able to help out?

    Many thanks.

  • #2
    I'm really not sure I understand what you want to do here, but maybe it's something like this:
    Code:
    isid ExpCode ImpCode, sort
    destring Year_Force, replace
    
    expand 2020-2000+1
    by ExpCode ImpCode , sort: gen year = 1999 + _n
    
    expand 160    // REPLACE 160 BY EXACT NUMBER OF PRODUCTS
    by ExpCode ImpCode year, sort: gen product_num = _n
    
    foreach v of varlist EnvProvs_Total EnvProvs_Sub FTA {
        replace `v' = 0 if Year_Force > 2000 & year < Year_Force
    }
    For those years before 2000, I will assume they take effect in 2000 and retain the information in those variables (EnvProvs_Total EnvProvs_Sub FTA) but for those after 2000, I'd like the information retained only from the year they took effect. For the previous years, id like to equate them to zero as well as edit some of them to specific values.
    These instructions do not say what is to be done when year == 2000. In the code above, I assume that in this situation those variables retain their original values. If you intend to change them to 0, then change -Year_Force > 2000- to -Year_Force >= 2000-.

    As for editing some to specific values, with no information provided about which ones to edit and what specific values to change them to, I can't offer any advice how to proceed.

    Comment


    • #3
      Hi Clyde,

      Thanks for your response. The codes work perfectly.



      These instructions do not say what is to be done when year == 2000. In the code above, I assume that in this situation those variables retain their original values. If you intend to change them to 0, then change -Year_Force > 2000- to -Year_Force >= 2000-. .
      Sorry for not being so clear. I actually intended to change them to 0 so I used the second option you provided.



      As for editing some to specific values, with no information provided about which ones to edit and what specific values to change them to, I can't offer any advice how to proceed.
      Permit me to be more specific with this. Some bilateral pairs have earlier agreements that came into force before what is currently stated in the variable Year_Force. However, I selected the agreement with the highest values in the varlist (EnvProvs_Total EnvProvs_Sub FTA) using the following commands;

      Code:
      sort Exporter Importer
      quietly by Exporter Importer : gen dup = cond(_N==1,0,_n)
      bysort Exporter Importer (EnvProvs_Total EnvProvs_Sub FTA) : keep if _n == _N


      For some of the previous agreements, I need to capture the information with lower values up until the years with the highest values. I'd cite an instance from the example dataset below;



      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int ID str44 Exporter int ExpCode str3 ExpISO str44 Importer int ImpCode str3 ImpISO int Year_Force byte(EnvProvs_Total EnvProvs_Sub FTA)
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "BAHAMAS"                           44 "BHS" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "BAHAMAS"                           44 "BHS" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "BAHAMAS"                           44 "BHS" 2008 23 7 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "BARBADOS"                          52 "BRB" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "BARBADOS"                          52 "BRB" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "BARBADOS"                          52 "BRB" 2008 23 7 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "BELIZE"                            84 "BLZ" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "BELIZE"                            84 "BLZ" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "BELIZE"                            84 "BLZ" 2008 23 7 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "DOMINICA"                         212 "DMA" 1973 14 6 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "DOMINICA"                         212 "DMA" 1973 15 4 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "DOMINICA"                         212 "DMA" 2008 23 7 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "GRENADA"                          308 "GRD" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "GRENADA"                          308 "GRD" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "GRENADA"                          308 "GRD" 2008 23 7 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "GUYANA"                           328 "GUY" 1973 14 6 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "GUYANA"                           328 "GUY" 1973 15 4 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "GUYANA"                           328 "GUY" 2008 23 7 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "HAITI"                            332 "HTI" 1973 14 6 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "JAMAICA"                          388 "JAM" 1973 14 6 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "JAMAICA"                          388 "JAM" 1973 15 4 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "JAMAICA"                          388 "JAM" 2008 23 7 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "MONTSERRAT"                       500 "MSR" 1973 14 6 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "MONTSERRAT"                       500 "MSR" 1973 15 4 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT KITTS AND NEVIS"            659 "KNA" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT KITTS AND NEVIS"            659 "KNA" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT KITTS AND NEVIS"            659 "KNA" 2008 23 7 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT LUCIA"                      662 "LCA" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT LUCIA"                      662 "LCA" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT LUCIA"                      662 "LCA" 2008 23 7 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT VINCENT AND THE GRENADINES" 670 "VCT" 1973 15 4 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT VINCENT AND THE GRENADINES" 670 "VCT" 1973 14 6 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "SAINT VINCENT AND THE GRENADINES" 670 "VCT" 2008 23 7 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "SURINAME"                         740 "SUR" 1973 14 6 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "SURINAME"                         740 "SUR" 1973 15 4 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "SURINAME"                         740 "SUR" 2008 23 7 1
        9 "ANTIGUA AND BARBUDA" 28 "ATG" "TRINIDAD AND TOBAGO"              780 "TTO" 1973 14 6 1
      320 "ANTIGUA AND BARBUDA" 28 "ATG" "TRINIDAD AND TOBAGO"              780 "TTO" 1973 15 4 1
      154 "ANTIGUA AND BARBUDA" 28 "ATG" "TRINIDAD AND TOBAGO"              780 "TTO" 2008 23 7 1
       19 "ARGENTINA"           32 "ARG" "ALGERIA"                           12 "DZA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "ALGERIA"                           12 "DZA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "ARGENTINA"                         32 "ARG" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "ARGENTINA"                         32 "ARG" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BANGLADESH"                        50 "BGD" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BANGLADESH"                        50 "BGD" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BENIN"                            204 "BEN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BENIN"                            204 "BEN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BOLIVIA"                           68 "BOL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BOLIVIA"                           68 "BOL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BRAZIL"                            76 "BRA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "BRAZIL"                            76 "BRA" 1989  0 0 1
       21 "ARGENTINA"           32 "ARG" "BRAZIL"                            76 "BRA" 1991  3 3 1
       19 "ARGENTINA"           32 "ARG" "CAMEROON"                         120 "CMR" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "CAMEROON"                         120 "CMR" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "CHILE"                            152 "CHL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "CHILE"                            152 "CHL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "COLOMBIA"                         170 "COL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "COLOMBIA"                         170 "COL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "CUBA"                             192 "CUB" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "CUBA"                             192 "CUB" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "ECUADOR"                          218 "ECU" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "ECUADOR"                          218 "ECU" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "EGYPT"                            818 "EGY" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "EGYPT"                            818 "EGY" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "GHANA"                            288 "GHA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "GHANA"                            288 "GHA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "GUINEA"                           324 "GIN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "GUINEA"                           324 "GIN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "GUYANA"                           328 "GUY" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "GUYANA"                           328 "GUY" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "INDIA"                            356 "IND" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "INDIA"                            356 "IND" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "INDONESIA"                        360 "IDN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "INDONESIA"                        360 "IDN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "IRAN"                             364 "IRN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "IRAN"                             364 "IRN" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "IRAQ"                             368 "IRQ" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "IRAQ"                             368 "IRQ" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "KOREA"                            410 "KOR" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "KOREA"                            410 "KOR" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "MACEDONIA"                        807 "MKD" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "MACEDONIA"                        807 "MKD" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "NICARAGUA"                        558 "NIC" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "NICARAGUA"                        558 "NIC" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "NIGERIA"                          566 "NGA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "NIGERIA"                          566 "NGA" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "NORTH KOREA"                      408 "PRK" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "NORTH KOREA"                      408 "PRK" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PAKISTAN"                         586 "PAK" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PAKISTAN"                         586 "PAK" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PARAGUAY"                         600 "PRY" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PARAGUAY"                         600 "PRY" 1989  0 0 1
       21 "ARGENTINA"           32 "ARG" "PARAGUAY"                         600 "PRY" 1991  3 3 1
       19 "ARGENTINA"           32 "ARG" "PERU"                             604 "PER" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PERU"                             604 "PER" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PHILIPPINES"                      608 "PHL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "PHILIPPINES"                      608 "PHL" 1989  0 0 1
       19 "ARGENTINA"           32 "ARG" "SINGAPORE"                        702 "SGP" 1989  0 0 1


      1. For Antigua and Barbuda/Bahamas bilateral pair, the highest value of the variable EnvProvs_Total is 23, therefore the system picks the year 2008 as the Year_Force. Since they are signed up to previous agreements, I need to replace the value 15 and 6 for the variables EnvProvs_Total & EnvProvs_Sub respectively, for the years 2000 - 2007.

      2. This is recurrent within all pairs in the agreement. Each agreement is marked with an ID. For the example above, the agreement ID is 52.

      3. For some of the agreements, I'd have to start with zero values for the varlist, then fill in specific values as in number 1, before the highest value already preselected.

      4. For others, I will have to end with zero values for the varlist, as the agreements elapse before 2020.

      5. Some bilateral pairs will need me to make the adjustment twice before the selected Year_Force. For example, different values for the varlist for the years 2000 - 2003 & 2004 - 2007


      Many thanks.
      Last edited by Tony Ekere; 05 Jan 2024, 00:57.

      Comment


      • #4
        OK. It looks like these are mostly idiosyncratic situations, sometimes contingent on factors not evident in the data. So I think you will have to make these adjustments ad hoc using -replace- commands as appropriate.

        Comment


        • #5
          Thanks again Clyde. You have been very helpful.

          Comment

          Working...
          X