Announcement

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

  • Identifying top trading partners in bilateral trade data

    I am using bilateral export data, which ranges from 1980 to 2020 and includes 150 countries. Now, I want to identify the top 50 trade partners of India and keep data of only those bilateral pairs where either exporter or importer is among India’s top 50 list. The structure of the data is as follows:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3(exporter importer) int year double export float fta1
    "AFG" "AFG" 1966                  0 0
    "AFG" "AFG" 1967                  0 0
    "AFG" "AFG" 1968                  0 0
    "AFG" "AFG" 1969                  0 0
    "AFG" "AFG" 1970                  0 0
    "AFG" "AFG" 1971                  0 0
    "AFG" "AFG" 1972                  0 0
    "AFG" "AFG" 1973                  0 0
    "AFG" "AFG" 1974                  0 0
    "AFG" "AFG" 1975                  0 0
    "AFG" "AFG" 1976                  0 0
    "AFG" "AFG" 1977                  0 0
    "AFG" "AFG" 1978                  0 0
    "AFG" "AFG" 1979                  0 0
    "AFG" "AFG" 1980                  0 0
    "AFG" "AFG" 1981                  0 0
    "AFG" "AFG" 1982                  0 0
    "AFG" "AFG" 1983                  0 0
    "AFG" "AFG" 1984                  0 0
    "AFG" "AFG" 1985                  0 0
    "AFG" "AFG" 1986                  0 0
    "AFG" "AFG" 1987                  0 0
    "AFG" "AFG" 1988                  0 0
    "AFG" "AFG" 1989                  0 0
    "AFG" "AFG" 1990                  0 0
    "AFG" "AFG" 1991                  0 0
    "AFG" "AFG" 1992                  0 0
    "AFG" "AFG" 1993                  0 0
    "AFG" "AFG" 1994                  0 0
    "AFG" "AFG" 1995                  0 0
    "AFG" "AFG" 1996                  0 0
    "AFG" "AFG" 1997                  0 0
    "AFG" "AFG" 1998                  0 0
    "AFG" "AFG" 1999                  0 0
    "AFG" "AFG" 2000                  0 0
    "AFG" "AFG" 2001                  0 0
    "AFG" "AFG" 2002  5.281832218170166 0
    "AFG" "AFG" 2003 1.8597965240478516 0
    "AFG" "AFG" 2004 13.610607385635376 0
    "AFG" "AFG" 2005  97.39748096466064 0
    "AFG" "AFG" 2006 110.46726298332214 0
    "AFG" "AFG" 2007 124.25252437591553 0
    "AFG" "AFG" 2008 103.32507467269897 0
    "AFG" "AFG" 2009 27.440624237060547 0
    "AFG" "AFG" 2010 123.29490756988525 0
    "AFG" "AFG" 2011  164.2770435810089 0
    "AFG" "AFG" 2012 149.87529373168945 0
    "AFG" "AFG" 2013 119.20826768875122 0
    "AFG" "AFG" 2014  124.9428905248642 0
    "AFG" "AFG" 2015 122.30015563964844 0
    "AFG" "AFG" 2016  97.29210996627808 0
    "AFG" "AFG" 2017 103.71893119812012 0
    "AFG" "AFG" 2018  97.30349189043045 0
    "AFG" "AFG" 2019 109.55547404289246 0
    "AFG" "AFG" 2020                  0 0
    "AFG" "AGO" 1966                  0 0
    "AFG" "AGO" 1967                  0 0
    "AFG" "AGO" 1968                  0 0
    "AFG" "AGO" 1969                  0 0
    "AFG" "AGO" 1970                  0 0
    "AFG" "AGO" 1971                  0 0
    "AFG" "AGO" 1972                  0 0
    "AFG" "AGO" 1973                  0 0
    "AFG" "AGO" 1974                  0 0
    "AFG" "AGO" 1975                  0 0
    "AFG" "AGO" 1976                  0 0
    "AFG" "AGO" 1977                  0 0
    "AFG" "AGO" 1978                  0 0
    "AFG" "AGO" 1979                  0 0
    "AFG" "AGO" 1980                  0 0
    "AFG" "AGO" 1981                  0 0
    "AFG" "AGO" 1982                  0 0
    "AFG" "AGO" 1983                  0 0
    "AFG" "AGO" 1984                  0 0
    "AFG" "AGO" 1985                  0 0
    "AFG" "AGO" 1986                  0 0
    "AFG" "AGO" 1987                  0 0
    "AFG" "AGO" 1988                  0 0
    "AFG" "AGO" 1989                  0 0
    "AFG" "AGO" 1990                  0 0
    "AFG" "AGO" 1991                  0 0
    "AFG" "AGO" 1992                  0 0
    "AFG" "AGO" 1993                  0 0
    "AFG" "AGO" 1994                  0 0
    "AFG" "AGO" 1995                  0 0
    "AFG" "AGO" 1996                  0 0
    "AFG" "AGO" 1997                  0 0
    "AFG" "AGO" 1998                  0 0
    "AFG" "AGO" 1999                  0 0
    "AFG" "AGO" 2000                  0 0
    "AFG" "AGO" 2001                  0 0
    "AFG" "AGO" 2002                  0 0
    "AFG" "AGO" 2003                  0 0
    "AFG" "AGO" 2004                  0 0
    "AFG" "AGO" 2005                  0 0
    "AFG" "AGO" 2006                  0 0
    "AFG" "AGO" 2007  .1325079697835463 0
    "AFG" "AGO" 2008                  0 0
    "AFG" "AGO" 2009  2.070388127322076 0
    "AFG" "AGO" 2010  .6213041225710185 0
    end
    hope someone will guide me here.

  • #2
    Who are the "top trading partners of India?" Are they the ones who have the largest amount of exports to India, or the ones with the largest amount of imports from India, or the ones with the largest total of both imports from and exports to India?

    How do you want to proceed if two or more countries are tied for 50th place?

    Do you want the top 50 identified separately for each year or a single list of 50 trading partners for all 50 years combined?

    What does variable fta1 mean, and is it relevant to this problem?

    Would I be correct in assuming that the variable named export is the volume of exports from the country named in variable exporter to the country named in importer in the year named in variable year?



    Comment


    • #3
      Dear Clyde Schechter, yes, variable export is the volume of export from the country named in variable exporter to the country named in importer in the year named in variable year. top trading partners are those with which India has the largest trade (India's export + import, sum over all the years). I want to keep all bilateral pairs for all the years where either importer or exporter is among India's top 50 partners. fta variable is a dummy for free trade agreement and I want to keep this variable. given the nature of the data, I think there is less chance of a tie.

      Comment


      • #4
        This code is not thoroughly tested because the example data includes no observations including India, let alone enough of them to identify a top 50. I also assume that the contents of variables exporter and importer are ISO-3 country codes--hence I refer to India as "IND".

        As you think ties are unlikely, but the code must nevertheless do something about them if they occur, this code will retain all the ties, so that the "top 50" might turn out to be the "top N" for some N slightly greater than 50 if there are ties. This has the advantage of making the code's output reproducible, as any other resolution requires some random or arbitrary selection of one among several candidates for the 50th position.

        Code:
        //    FIND TOP 50 TRADING PARTNERS
        frame put exporter importer export, into(top_50)
        frame top_50 {
            keep if inlist("IND", exporter, importer) & !missing(export)
            assert exporter != importer
            gen partner = cond(exporter == "IND", importer, exporter)
            collapse (sum) export, by(partner)
            sort export
            keep if export >= export[_N-49]
            keep partner
        }
        
        frlink m:1 exporter, frame(top_50 partner)
        rename top_50 match1
        frlink m:1 importer, frame(top_50 partner)
        rename top_50 match2
        keep if !missing(match1) | !missing(match2)

        Comment


        • #5
          Dear Clyde Schechter Thank You. You have written it perfectly. I forgot to mention that there are cases where the exporter is the same as an exporter, and I have to keep those observations, and also when India is both an exporter and importer. I tried it, with little change on a small sample for identifying the top two trade partners of India, and it is working fine.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str3(exporter importer) int(year export) byte fta
          "IND" "IND" 1990 200 1
          "AFG" "AFG" 1990  50 0
          "USA" "USA" 1990 500 1
          "BGD" "BGD" 1990 100 1
          "IND" "IND" 1991 300 1
          "AFG" "AFG" 1991  70 0
          "USA" "USA" 1991 700 1
          "BGD" "BGD" 1991 120 1
          "IND" "AFG" 1990  15 0
          "AFG" "IND" 1990   5 0
          "IND" "AFG" 1991  20 0
          "AFG" "IND" 1991   6 0
          "IND" "USA" 1990 100 1
          "USA" "IND" 1990  90 1
          "USA" "IND" 1991  95 1
          "IND" "USA" 1991 110 1
          "IND" "BGD" 1990  20 1
          "BGD" "IND" 1990  12 1
          "IND" "BGD" 1991  25 1
          "BGD" "IND" 1991  15 1
          "AFG" "USA" 1990   5 0
          "USA" "AFG" 1990   1 0
          "AFG" "USA" 1991   6 0
          "USA" "AFG" 1991   2 0
          "AFG" "BGD" 1990   8 0
          "BGD" "AFG" 1990  12 0
          "AFG" "BGD" 1991   5 0
          "BGD" "AFG" 1991  14 0
          "USA" "BGD" 1990 100 1
          "BGD" "USA" 1990  50 1
          "USA" "BGD" 1991 150 1
          "BGD" "USA" 1991  40 1
          end


          Code:
          // FIND TOP 2 TRADING PARTNERS
          frame put exporter importer export, into(top_2)
          frame top_2 {
              keep if inlist("IND", exporter, importer) & !missing(export)
              *assert exporter != importer
              gen partner = cond(exporter == "IND", importer, exporter)
              collapse (sum) export, by(partner)
              sort export
              keep if export >= export[_N-2]
              keep partner
          }
          
          frlink m:1 exporter, frame(top_2 partner)
          rename top_2 match1
          frlink m:1 importer, frame(top_2 partner)
          rename top_2 match2
          *keep if !missing(match1) | !missing(match2)
          keep if !missing(match1) & !missing(match2)
          Last edited by Tariq Masood; 28 Sep 2024, 12:05.

          Comment


          • #6
            Dear Clyde Schechter. I am facing one more issue, I need to create a dummy =1 if either exporter or importer has fta with India (list of countries will be identified by variable fta_ind =1). For this, I am running this code, but there seems to be some problem. kindly have a look.

            Code:
            *idetifying pairs involving countries that have FTA with India
            * Step 1: Identify countries involved in any FTA with India
            preserve
            keep if fta_ind == 1
            egen exporter_fta = tag(exporter)
            
            egen importer_fta = tag(importer)
            
            * Step 2: Create lists of exporters and importers involved in FTAs with India
            levelsof exporter if exporter_fta, local(fta_exporters)
            levelsof importer if importer_fta, local(fta_importers)
            
            * Combine the lists into one
            local fta_countries : list fta_exporters | fta_importers
            
            restore
            
            * Step 3: Generate a dummy variable for countries involved in any FTA India
            gen exporter_in_fta = inlist(exporter, `fta_countries')
            gen importer_in_fta = inlist(importer, `fta_countries')
            
            * Step 4: Create the final dummy variable
            gen fta_dummy = exporter_in_fta | importer_in_fta
            
            * Drop intermediate variables
            drop exporter_in_fta importer_in_fta

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input str3(exporter importer) int(year export) byte fta
            "IND" "IND" 1990 200 0
            "AFG" "AFG" 1990  50 0
            "USA" "USA" 1990 500 0
            "BGD" "BGD" 1990 100 0
            "IND" "IND" 1991 300 0
            "AFG" "AFG" 1991  70 0
            "USA" "USA" 1991 700 0
            "BGD" "BGD" 1991 120 0
            "IND" "AFG" 1990  15 1
            "AFG" "IND" 1990   5 1
            "IND" "AFG" 1991  20 1
            "AFG" "IND" 1991   6 1
            "IND" "USA" 1990 100 0
            "USA" "IND" 1990  90 0
            "USA" "IND" 1991  95 0
            "IND" "USA" 1991 110 0
            "IND" "BGD" 1990  20 1
            "BGD" "IND" 1990  12 1
            "IND" "BGD" 1991  25 1
            "BGD" "IND" 1991  15 1
            "AFG" "USA" 1990   5 0
            "USA" "AFG" 1990   1 0
            "AFG" "USA" 1991   6 0
            "USA" "AFG" 1991   2 0
            "AFG" "BGD" 1990   8 0
            "BGD" "AFG" 1990  12 0
            "AFG" "BGD" 1991   5 0
            "BGD" "AFG" 1991  14 0
            "USA" "BGD" 1990 100 0
            "BGD" "USA" 1990  50 0
            "USA" "BGD" 1991 150 0
            "BGD" "USA" 1991  40 0
            end
            Last edited by Tariq Masood; 28 Sep 2024, 14:12.

            Comment


            • #7
              There are a couple of problems with your code. The first problem is with the line -keep if fta_ind == 1-. This immediately breaks because there is no variable named fta_ind in the example data you show. You do have a variable named fta, which, I presume, is the variable you intended to refer to here. So you can fix this problem by either renaming the variable to fta_ind or changing that command to -keep if fta == 1-. I also note that the example data you showed in #1 of this post contains a variable called fta1, which is described as doing the same thing. So I don't know if you are switching among data sets with the same code but failing to first harmonize the names of the variables in the data sets, or if you are just having difficulty remembering what you called your variable when you try to refer to it in code.

              The second problem I find is with the commands
              Code:
              gen exporter_in_fta = inlist(exporter, `fta_countries')
              gen importer_in_fta = inlist(importer, `fta_countries')
              The problem is that -inlist- requires that all of its arguments be separated by commas. But the local macro fta_countries has no commas. So -inlist() doesn't actually recognize any exporter or importer as being in the list except the one that appears first. You can fix this by adding the -separate(",")- option to the -levelsof- commands.

              There is a third problem, perhaps. Because, as one might expect, a country that has a free trade agreement with India probably actually trades with India, India itself will show up as a country that has an FTA with India. And that, in turn, means that every observation that includes India will be flagged as involving an FTA country. Perhaps that is what you want, and not being a trade economist, I can't really assert to you that it isn't. But it doesn't really make sense to me, because of its implications. So I think the other thing we need to do is exclude India from the list of FTA countries. So, the following modification of your code will do the job:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input str3(exporter importer) int(year export) byte fta_ind
              "IND" "IND" 1990 200 0
              "AFG" "AFG" 1990  50 0
              "USA" "USA" 1990 500 0
              "BGD" "BGD" 1990 100 0
              "IND" "IND" 1991 300 0
              "AFG" "AFG" 1991  70 0
              "USA" "USA" 1991 700 0
              "BGD" "BGD" 1991 120 0
              "IND" "AFG" 1990  15 1
              "AFG" "IND" 1990   5 1
              "IND" "AFG" 1991  20 1
              "AFG" "IND" 1991   6 1
              "IND" "USA" 1990 100 0
              "USA" "IND" 1990  90 0
              "USA" "IND" 1991  95 0
              "IND" "USA" 1991 110 0
              "IND" "BGD" 1990  20 1
              "BGD" "IND" 1990  12 1
              "IND" "BGD" 1991  25 1
              "BGD" "IND" 1991  15 1
              "AFG" "USA" 1990   5 0
              "USA" "AFG" 1990   1 0
              "AFG" "USA" 1991   6 0
              "USA" "AFG" 1991   2 0
              "AFG" "BGD" 1990   8 0
              "BGD" "AFG" 1990  12 0
              "AFG" "BGD" 1991   5 0
              "BGD" "AFG" 1991  14 0
              "USA" "BGD" 1990 100 0
              "BGD" "USA" 1990  50 0
              "USA" "BGD" 1991 150 0
              "BGD" "USA" 1991  40 0
              end
              
              *idetifying pairs involving countries that have FTA with India
              * Step 1: Identify countries involved in any FTA with India
              preserve
              keep if fta_ind == 1
              egen exporter_fta = tag(exporter)
              
              egen importer_fta = tag(importer)
              
              * Step 2: Create lists of exporters and importers involved in FTAs with India
              levelsof exporter if exporter_fta & exporter != "IND", local(fta_exporters) separate(",")
              levelsof importer if importer_fta & importer != "IND", local(fta_importers) separate(",")
              
              * Combine the lists into one
              local fta_countries : list fta_exporters | fta_importers
              
              restore
              
              * Step 3: Generate a dummy variable for countries involved in any FTA India
              gen exporter_in_fta = inlist(exporter, `fta_countries')
              gen importer_in_fta = inlist(importer, `fta_countries')
              
              * Step 4: Create the final dummy variable
              gen fta_dummy = exporter_in_fta | importer_in_fta
              
              * Drop intermediate variables
              drop exporter_in_fta importer_in_fta
              That said, this is not how I would go about it. -inlist- has a limitation on the number of arguments. Specifically, for string comparisons, a total of 10 arguments is allowed, which means that if there are more than 9 countries that have FTAs with India, this code will fail. While you could work around that by -encode-ing exporter and importer, that gets complicated because you have to be careful to assure that they are both encoded exactly the same way--which is doable but finicky.

              So here's a different approach that does not have this limitation and should work under all circumstances:
              Code:
              frame put exporter importer if fta_ind == 1, into(fta_countries)
              frame  fta_countries {
                  stack exporter importer, into(country) clear
                  drop _stack
                  drop if country == "IND"
                  duplicates drop
              }
              gen wanted = 0
              foreach v of varlist exporter importer {
                  frlink m:1 `v', frame(fta_countries country)
                  replace wanted = 1 if !missing(fta_countries)
                  drop fta_countries
              }
              It uses some less familiar commands, but overall its structure is simpler and, to my mind, clearer. It also produces the same results as the corrected version of your code I have shown above in the example data. And it will work properly even if there are more than 9 countries that have FTAs with India.

              Comment


              • #8
                Dear Clyde Schechter, it works fine. but I am not able to achieve my final purpose, which I thought would be achieved by combining the codes you suggested. actually, in the end,after identifying the top trading partner of India by running this code:

                Code:
                // FIND TOP 50 TRADING PARTNERS
                frame put exporter importer export, into(top_2)
                frame top_2 {
                    keep if inlist("IND", exporter, importer) & !missing(export)
                    *assert exporter != importer
                    gen partner = cond(exporter == "IND", importer, exporter)
                    collapse (sum) export, by(partner)
                    sort export
                    keep if export >= export[_N-2]
                    keep partner
                }
                
                frlink m:1 exporter, frame(top_2 partner)
                rename top_2 match1
                frlink m:1 importer, frame(top_2 partner)
                rename top_2 match2
                keep if !missing(match1) & !missing(match2)

                next is to keep all bilateral pairs involving India with either of these top partners or bilateral pairs involving these top partners and also to keep those countries that have FTA with India even though they are not among the top partners.

                In this data example, there are five countries (AFG, BGD, IND, NPL, and USA). The code identifies the top two partners of India, the USA and BGD. Next, I want to keep NPL also ( as it has an FTA with India), its bilateral flows with IND, and the top two, i.e., USA and BGD.



                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input str3(exporter importer) int(year export) byte fta_ind
                "AFG" "AFG" 1990  50 0
                "AFG" "AFG" 1991  70 0
                "AFG" "BGD" 1990   8 0
                "AFG" "BGD" 1991   5 0
                "AFG" "IND" 1990   5 0
                "AFG" "IND" 1991   6 0
                "AFG" "NPL" 1990   1 0
                "AFG" "NPL" 1991   1 0
                "AFG" "USA" 1990   5 0
                "AFG" "USA" 1991   6 0
                "BGD" "AFG" 1990  12 0
                "BGD" "AFG" 1991  14 0
                "BGD" "BGD" 1990 100 0
                "BGD" "BGD" 1991 120 0
                "BGD" "IND" 1990  12 1
                "BGD" "IND" 1991  15 1
                "BGD" "NPL" 1990   2 0
                "BGD" "NPL" 1991   3 0
                "BGD" "USA" 1990  50 0
                "BGD" "USA" 1991  40 0
                "IND" "AFG" 1990  15 0
                "IND" "AFG" 1991  20 0
                "IND" "BGD" 1990  20 1
                "IND" "BGD" 1991  25 1
                "IND" "IND" 1990 200 0
                "IND" "IND" 1991 300 0
                "IND" "NPL" 1990   4 1
                "IND" "NPL" 1991   5 1
                "IND" "USA" 1990 100 0
                "IND" "USA" 1991 110 0
                "NPL" "AFG" 1990   2 0
                "NPL" "AFG" 1991   2 0
                "NPL" "BGD" 1990   3 0
                "NPL" "BGD" 1991   4 0
                "NPL" "IND" 1990   5 1
                "NPL" "IND" 1991   6 1
                "NPL" "NPL" 1990  10 0
                "NPL" "NPL" 1991  12 0
                "NPL" "USA" 1990   1 0
                "NPL" "USA" 1991   1 0
                "USA" "AFG" 1990   1 0
                "USA" "AFG" 1991   2 0
                "USA" "BGD" 1990 100 0
                "USA" "BGD" 1991 150 0
                "USA" "IND" 1990  90 0
                "USA" "IND" 1991  95 0
                "USA" "NPL" 1990   2 0
                "USA" "NPL" 1991   2 0
                "USA" "USA" 1990 500 0
                "USA" "USA" 1991 700 0
                end
                In the end, as USA and BGD are the top two partners and NPL has FTA with IND, there is a need to drop all observations where AFG is either an exporter or an importer as it neither has FTA with IND nor a top trader. finally, the output for this example data must look like this:


                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input str3(exporter importer) int(year export) byte fta
                "IND" "IND" 1990 200 0
                "USA" "USA" 1990 500 0
                "BGD" "BGD" 1990 100 0
                "IND" "IND" 1991 300 0
                "USA" "USA" 1991 700 0
                "BGD" "BGD" 1991 120 0
                "IND" "USA" 1990 100 0
                "USA" "IND" 1990  90 0
                "USA" "IND" 1991  95 0
                "IND" "USA" 1991 110 0
                "IND" "BGD" 1990  20 1
                "BGD" "IND" 1990  12 1
                "IND" "BGD" 1991  25 1
                "BGD" "IND" 1991  15 1
                "USA" "BGD" 1990 100 0
                "BGD" "USA" 1990  50 0
                "USA" "BGD" 1991 150 0
                "BGD" "USA" 1991  40 0
                "NPL" "NPL" 1990  10 0
                "NPL" "NPL" 1991  12 0
                "NPL" "IND" 1990   5 1
                "IND" "NPL" 1990   4 1
                "NPL" "IND" 1991   6 1
                "IND" "NPL" 1991   5 1
                "NPL" "BGD" 1990   3 0
                "BGD" "NPL" 1990   2 0
                "NPL" "BGD" 1991   4 0
                "BGD" "NPL" 1991   3 0
                "NPL" "USA" 1990   1 0
                "USA" "NPL" 1990   2 0
                "NPL" "USA" 1991   1 0
                "USA" "NPL" 1991   2 0
                end
                Last edited by Tariq Masood; 29 Sep 2024, 09:47.

                Comment


                • #9
                  It is not clear to me what you want. On the one hand you say in words that you want to retain observations where either the exporter or the importer has among the top 2 trading partners with IND, or if the exporter or importer has an FTA with IND. But in the example you show for what the desired results you should look like, it seems you require that either both the exporter and importer be in the top 2, or one of them has an FTA with IND. So you need to be clearer about exactly what you are trying to capture here.

                  That said, there is an error in your code for the top 2.
                  Code:
                  keep if export >= export[_N-2]
                  
                  should be
                  
                  keep if export >= export[_N-1]
                  Using _N-2 gives you the top 3, not the top 2.

                  Comment


                  • #10
                    In my dataset, there are more than 150 countries, some of which have a Free Trade Agreement (FTA) with India. My goal is twofold:
                    1. Keep India's major trading partners: The previous code identifies these major trading partners by selecting the countries with the largest trade volumes with India.
                    2. Keep countries that have an FTA with India: Even if these FTA countries are not among the top trading partners, I want to keep them as well. This involves keeping all their bilateral trade flows with India and with India’s top trading partners.
                    Suppose we have 5 countries: Afghanistan (AFG), Bangladesh (BGD), India (IND), Nepal (NPL), and the United States (USA). For two time periods, this gives a total of 50 bilateral trade pairs, including domestic flows within each country.
                    In this case:
                    • India has an FTA with two countries: Bangladesh (BGD) and Nepal (NPL).
                    • Bangladesh (BGD) is one of India’s top trading partners, while Nepal (NPL) is not a top partner but has an FTA with India.
                    • USA is another top trading partner.
                    For my purpose:
                    • I want to keep Bangladesh (BGD) and USA because they are top trading partners of India.
                    • I also want to keep Nepal (NPL) because it has an FTA with India.
                    By keeping these four countries (India, Bangladesh, USA, and Nepal), the dataset will include all bilateral trade flows among these four countries. This results in a total of 32 trade pairs (since bilateral flows between four countries over two time periods equals 32 pairs).

                    and with keep if export >= export[_N-2] and am able to retain India and two top trading partners.

                    Comment


                    • #11
                      Dear Clyde Schechter. Finally, I get the desired outcome by running this.

                      Code:
                      frame put exporter if fta_ind == 1, into(fta_countries)
                      frame fta_countries {
                      drop if exporter == "IND"
                      duplicates drop
                      }
                      gen exp_fta1 = 0
                      foreach v of varlist exporter {
                      frlink m:1 `v', frame(fta_countries exporter)
                      replace exp_fta1 = 1 if !missing(fta_countries)
                      drop fta_countries
                      }
                      
                      frame drop fta_countries
                      frame put importer if fta_ind == 1, into(fta_countries)
                      frame fta_countries {
                      drop if importer== "IND"
                      duplicates drop
                      }
                      gen imp_fta1 = 0
                      foreach v of varlist importer {
                      frlink m:1 `v', frame(fta_countries importer)
                      replace imp_fta1 = 1 if !missing(fta_countries)
                      drop fta_countries
                      }
                      
                      
                      // FIND TOP 2 TRADING PARTNERS
                      frame put exporter importer export, into(top_2)
                      frame top_2 {
                      keep if inlist("IND", exporter, importer) & !missing(export)
                      *assert exporter != importer
                      gen partner = cond(exporter == "IND", importer, exporter)
                      collapse (sum) export, by(partner)
                      sort export
                      keep if export >= export[_N-2]
                      keep partner
                      }
                      
                      frlink m:1 exporter, frame(top_2 partner)
                      rename top_2 match1
                      frlink m:1 importer, frame(top_2 partner)
                      rename top_2 match2
                      
                      drop if ( match1==. & match2==. ) & (exp_fta1==0 | imp_fta1==0)
                      drop if ( match1==. | match2==. ) & (exp_fta1==0 & imp_fta1==0)
                      drop if imp_fta1==0 & match2==.
                      drop if exp_fta1==0 & match1==.

                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      clear
                      input str3(exporter importer) int(year export) byte fta_ind
                      "IND" "IND" 1990 200 0
                      "AFG" "AFG" 1990  50 0
                      "USA" "USA" 1990 500 0
                      "BGD" "BGD" 1990 100 0
                      "IND" "IND" 1991 300 0
                      "AFG" "AFG" 1991  70 0
                      "USA" "USA" 1991 700 0
                      "BGD" "BGD" 1991 120 0
                      "IND" "AFG" 1990  15 0
                      "AFG" "IND" 1990   5 0
                      "IND" "AFG" 1991  20 0
                      "AFG" "IND" 1991   6 0
                      "IND" "USA" 1990 100 0
                      "USA" "IND" 1990  90 0
                      "USA" "IND" 1991  95 0
                      "IND" "USA" 1991 110 0
                      "IND" "BGD" 1990  20 1
                      "BGD" "IND" 1990  12 1
                      "IND" "BGD" 1991  25 1
                      "BGD" "IND" 1991  15 1
                      "AFG" "USA" 1990   5 0
                      "USA" "AFG" 1990   1 0
                      "AFG" "USA" 1991   6 0
                      "USA" "AFG" 1991   2 0
                      "AFG" "BGD" 1990   8 0
                      "BGD" "AFG" 1990  12 0
                      "AFG" "BGD" 1991   5 0
                      "BGD" "AFG" 1991  14 0
                      "USA" "BGD" 1990 100 0
                      "BGD" "USA" 1990  50 0
                      "USA" "BGD" 1991 150 0
                      "BGD" "USA" 1991  40 0
                      "NPL" "NPL" 1990  10 0
                      "NPL" "NPL" 1991  12 0
                      "NPL" "IND" 1990   5 1
                      "IND" "NPL" 1990   4 1
                      "NPL" "IND" 1991   6 1
                      "IND" "NPL" 1991   5 1
                      "NPL" "BGD" 1990   3 0
                      "BGD" "NPL" 1990   2 0
                      "NPL" "BGD" 1991   4 0
                      "BGD" "NPL" 1991   3 0
                      "NPL" "AFG" 1990   2 0
                      "AFG" "NPL" 1990   1 0
                      "NPL" "AFG" 1991   2 0
                      "AFG" "NPL" 1991   1 0
                      "NPL" "USA" 1990   1 0
                      "USA" "NPL" 1990   2 0
                      "NPL" "USA" 1991   1 0
                      "USA" "NPL" 1991   2 0
                      end

                      thanks for all the help.
                      Last edited by Tariq Masood; 30 Sep 2024, 05:00.

                      Comment

                      Working...
                      X