Announcement

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

  • How to rearrange a dataset partly and generate a new variable

    Dear Stata experts,

    I currently work with the CEPII gravity model dataset which can be downloaded here: http://www.cepii.fr/CEPII/en/bdd_mod..._item.asp?id=8. The dataset consists of 4.4 mio observations of tradeflows for 257 countries between 1948 and 2019.
    I am trying to add a new variable which gives the sum of imports from and exports to China for each country. The following command specifically gives the exports to China:

    "gen expo_china= tradeflow_baci if iso3num_d == 156"

    Similarly: "gen impo_from_china= tradeflow_baci if iso3num_o == 156" generates an extra column witht all Chinese exports. The problem is that I can't create a sum of them because the exports to China appear at the end of the row of each country the tradeflow has originated from while the data for the imports from China appear at the end of the row of China as the originating country (since from the Chinese perspective they are exports).

    Is there any way these two informations can be matched and then summed up?

    Kind regards
    Michael


  • #2
    I was hoping to respond to this post. But after trying to download the data set via the link you provided, I was left with an invalid zip file that could not be opened. Since it appears that you already have the data set, I suggest that you open it in Stata, trim it down to a relatively small subset of the data involving just a couple of countries and only the relevant variables, and then use the -dataex- command to post that example here in the Forum. 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.

    Comment


    • #3
      Dear Mr. Schechter,

      Thank you very much for the suggestion, I didn't know dataex before but I can see it's very useful.

      Below you can see a part of the dataset. The full dataset includes much more variables which can be used as explanatory variables in the gravity model, like a dummy for common language or the distance between the capitals. Here you can only see the year, a short identification number for the country of origin and destination of the tradeflow. In this subsample you see trade from Aruba (ABW) to Afghanistan (AFG) and Angola (AGO). In most years these nations have no trade, but the values here dont matter for the problem I guess.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int year str3(iso3_o iso3_d) int(iso3num_o iso3num_d) double tradeflow_baci
      1948 "ABW" "ABW" 533 533                  .
      1949 "ABW" "ABW" 533 533                  .
      1950 "ABW" "ABW" 533 533                  .
      1951 "ABW" "ABW" 533 533                  .
      1952 "ABW" "ABW" 533 533                  .
      1953 "ABW" "ABW" 533 533                  .
      1954 "ABW" "ABW" 533 533                  .
      1955 "ABW" "ABW" 533 533                  .
      1956 "ABW" "ABW" 533 533                  .
      1957 "ABW" "ABW" 533 533                  .
      1958 "ABW" "ABW" 533 533                  .
      1959 "ABW" "ABW" 533 533                  .
      1960 "ABW" "ABW" 533 533                  .
      1961 "ABW" "ABW" 533 533                  .
      1962 "ABW" "ABW" 533 533                  .
      1963 "ABW" "ABW" 533 533                  .
      1964 "ABW" "ABW" 533 533                  .
      1965 "ABW" "ABW" 533 533                  .
      1966 "ABW" "ABW" 533 533                  .
      1967 "ABW" "ABW" 533 533                  .
      1968 "ABW" "ABW" 533 533                  .
      1969 "ABW" "ABW" 533 533                  .
      1970 "ABW" "ABW" 533 533                  .
      1971 "ABW" "ABW" 533 533                  .
      1972 "ABW" "ABW" 533 533                  .
      1973 "ABW" "ABW" 533 533                  .
      1974 "ABW" "ABW" 533 533                  .
      1975 "ABW" "ABW" 533 533                  .
      1976 "ABW" "ABW" 533 533                  .
      1977 "ABW" "ABW" 533 533                  .
      1978 "ABW" "ABW" 533 533                  .
      1979 "ABW" "ABW" 533 533                  .
      1980 "ABW" "ABW" 533 533                  .
      1981 "ABW" "ABW" 533 533                  .
      1982 "ABW" "ABW" 533 533                  .
      1983 "ABW" "ABW" 533 533                  .
      1984 "ABW" "ABW" 533 533                  .
      1985 "ABW" "ABW" 533 533                  .
      1986 "ABW" "ABW" 533 533                  .
      1987 "ABW" "ABW" 533 533                  .
      1988 "ABW" "ABW" 533 533                  .
      1989 "ABW" "ABW" 533 533                  .
      1990 "ABW" "ABW" 533 533                  .
      1991 "ABW" "ABW" 533 533                  .
      1992 "ABW" "ABW" 533 533                  .
      1993 "ABW" "ABW" 533 533                  .
      1994 "ABW" "ABW" 533 533                  .
      1995 "ABW" "ABW" 533 533                  .
      1996 "ABW" "ABW" 533 533                  .
      1997 "ABW" "ABW" 533 533                  .
      1998 "ABW" "ABW" 533 533                  .
      1999 "ABW" "ABW" 533 533                  .
      2000 "ABW" "ABW" 533 533                  .
      2001 "ABW" "ABW" 533 533                  .
      2002 "ABW" "ABW" 533 533                  .
      2003 "ABW" "ABW" 533 533                  .
      2004 "ABW" "ABW" 533 533                  .
      2005 "ABW" "ABW" 533 533                  .
      2006 "ABW" "ABW" 533 533                  .
      2007 "ABW" "ABW" 533 533                  .
      2008 "ABW" "ABW" 533 533                  .
      2009 "ABW" "ABW" 533 533                  .
      2010 "ABW" "ABW" 533 533                  .
      2011 "ABW" "ABW" 533 533                  .
      2012 "ABW" "ABW" 533 533                  .
      2013 "ABW" "ABW" 533 533                  .
      2014 "ABW" "ABW" 533 533                  .
      2015 "ABW" "ABW" 533 533                  .
      2016 "ABW" "ABW" 533 533                  .
      2017 "ABW" "ABW" 533 533                  .
      2018 "ABW" "ABW" 533 533                  .
      2019 "ABW" "ABW" 533 533                  .
      1948 "ABW" "AFG" 533   4                  .
      1949 "ABW" "AFG" 533   4                  .
      1950 "ABW" "AFG" 533   4                  .
      1951 "ABW" "AFG" 533   4                  .
      1952 "ABW" "AFG" 533   4                  .
      1953 "ABW" "AFG" 533   4                  .
      1954 "ABW" "AFG" 533   4                  .
      1955 "ABW" "AFG" 533   4                  .
      1956 "ABW" "AFG" 533   4                  .
      1957 "ABW" "AFG" 533   4                  .
      1958 "ABW" "AFG" 533   4                  .
      1959 "ABW" "AFG" 533   4                  .
      1960 "ABW" "AFG" 533   4                  .
      1961 "ABW" "AFG" 533   4                  .
      1962 "ABW" "AFG" 533   4                  .
      1963 "ABW" "AFG" 533   4                  .
      1964 "ABW" "AFG" 533   4                  .
      1965 "ABW" "AFG" 533   4                  .
      1966 "ABW" "AFG" 533   4                  .
      1967 "ABW" "AFG" 533   4                  .
      1968 "ABW" "AFG" 533   4                  .
      1969 "ABW" "AFG" 533   4                  .
      1970 "ABW" "AFG" 533   4                  .
      1971 "ABW" "AFG" 533   4                  .
      1972 "ABW" "AFG" 533   4                  .
      1973 "ABW" "AFG" 533   4                  .
      1974 "ABW" "AFG" 533   4                  .
      1975 "ABW" "AFG" 533   4                  .
      1976 "ABW" "AFG" 533   4                  .
      1977 "ABW" "AFG" 533   4                  .
      1978 "ABW" "AFG" 533   4                  .
      1979 "ABW" "AFG" 533   4                  .
      1980 "ABW" "AFG" 533   4                  .
      1981 "ABW" "AFG" 533   4                  .
      1982 "ABW" "AFG" 533   4                  .
      1983 "ABW" "AFG" 533   4                  .
      1984 "ABW" "AFG" 533   4                  .
      1985 "ABW" "AFG" 533   4                  .
      1986 "ABW" "AFG" 533   4                  .
      1987 "ABW" "AFG" 533   4                  .
      1988 "ABW" "AFG" 533   4                  .
      1989 "ABW" "AFG" 533   4                  .
      1990 "ABW" "AFG" 533   4                  .
      1991 "ABW" "AFG" 533   4                  .
      1992 "ABW" "AFG" 533   4                  .
      1993 "ABW" "AFG" 533   4                  .
      1994 "ABW" "AFG" 533   4                  .
      1995 "ABW" "AFG" 533   4                  .
      1996 "ABW" "AFG" 533   4                  .
      1997 "ABW" "AFG" 533   4                  .
      1998 "ABW" "AFG" 533   4                  .
      1999 "ABW" "AFG" 533   4                  .
      2000 "ABW" "AFG" 533   4                  .
      2001 "ABW" "AFG" 533   4                  .
      2002 "ABW" "AFG" 533   4                  .
      2003 "ABW" "AFG" 533   4                  .
      2004 "ABW" "AFG" 533   4                  .
      2005 "ABW" "AFG" 533   4                  .
      2006 "ABW" "AFG" 533   4                  .
      2007 "ABW" "AFG" 533   4                  .
      2008 "ABW" "AFG" 533   4                  .
      2009 "ABW" "AFG" 533   4                  .
      2010 "ABW" "AFG" 533   4                  .
      2011 "ABW" "AFG" 533   4                  .
      2012 "ABW" "AFG" 533   4                  .
      2013 "ABW" "AFG" 533   4                  .
      2014 "ABW" "AFG" 533   4                  .
      2015 "ABW" "AFG" 533   4                  .
      2016 "ABW" "AFG" 533   4                  .
      2017 "ABW" "AFG" 533   4                  .
      2018 "ABW" "AFG" 533   4 2030.2830000000001
      2019 "ABW" "AFG" 533   4                  .
      1948 "ABW" "AGO" 533  24                  .
      1949 "ABW" "AGO" 533  24                  .
      1950 "ABW" "AGO" 533  24                  .
      1951 "ABW" "AGO" 533  24                  .
      1952 "ABW" "AGO" 533  24                  .
      1953 "ABW" "AGO" 533  24                  .
      1954 "ABW" "AGO" 533  24                  .
      1955 "ABW" "AGO" 533  24                  .
      1956 "ABW" "AGO" 533  24                  .
      1957 "ABW" "AGO" 533  24                  .
      1958 "ABW" "AGO" 533  24                  .
      1959 "ABW" "AGO" 533  24                  .
      1960 "ABW" "AGO" 533  24                  .
      1961 "ABW" "AGO" 533  24                  .
      1962 "ABW" "AGO" 533  24                  .
      1963 "ABW" "AGO" 533  24                  .
      1964 "ABW" "AGO" 533  24                  .
      1965 "ABW" "AGO" 533  24                  .
      1966 "ABW" "AGO" 533  24                  .
      1967 "ABW" "AGO" 533  24                  .
      1968 "ABW" "AGO" 533  24                  .
      1969 "ABW" "AGO" 533  24                  .
      1970 "ABW" "AGO" 533  24                  .
      1971 "ABW" "AGO" 533  24                  .
      1972 "ABW" "AGO" 533  24                  .
      1973 "ABW" "AGO" 533  24                  .
      1974 "ABW" "AGO" 533  24                  .
      1975 "ABW" "AGO" 533  24                  .
      1976 "ABW" "AGO" 533  24                  .
      1977 "ABW" "AGO" 533  24                  .
      1978 "ABW" "AGO" 533  24                  .
      1979 "ABW" "AGO" 533  24                  .
      1980 "ABW" "AGO" 533  24                  .
      1981 "ABW" "AGO" 533  24                  .
      1982 "ABW" "AGO" 533  24                  .
      1983 "ABW" "AGO" 533  24                  .
      1984 "ABW" "AGO" 533  24                  .
      1985 "ABW" "AGO" 533  24                  .
      1986 "ABW" "AGO" 533  24                  .
      1987 "ABW" "AGO" 533  24                  .
      1988 "ABW" "AGO" 533  24                  .
      1989 "ABW" "AGO" 533  24                  .
      1990 "ABW" "AGO" 533  24                  .
      1991 "ABW" "AGO" 533  24                  .
      1992 "ABW" "AGO" 533  24                  .
      1993 "ABW" "AGO" 533  24                  .
      1994 "ABW" "AGO" 533  24                  .
      1995 "ABW" "AGO" 533  24                  .
      1996 "ABW" "AGO" 533  24                  .
      1997 "ABW" "AGO" 533  24                  .
      1998 "ABW" "AGO" 533  24                  .
      1999 "ABW" "AGO" 533  24                  .
      2000 "ABW" "AGO" 533  24                  .
      2001 "ABW" "AGO" 533  24                  .
      2002 "ABW" "AGO" 533  24                  .
      2003 "ABW" "AGO" 533  24                  .
      2004 "ABW" "AGO" 533  24                  .
      2005 "ABW" "AGO" 533  24                  .
      2006 "ABW" "AGO" 533  24                  .
      2007 "ABW" "AGO" 533  24 4.0440000000000005
      2008 "ABW" "AGO" 533  24                  .
      2009 "ABW" "AGO" 533  24               .483
      2010 "ABW" "AGO" 533  24          49810.867
      2011 "ABW" "AGO" 533  24 .14200000000000002
      2012 "ABW" "AGO" 533  24               .333
      2013 "ABW" "AGO" 533  24                 25
      2014 "ABW" "AGO" 533  24             53.711
      2015 "ABW" "AGO" 533  24                  .
      2016 "ABW" "AGO" 533  24                  .
      2017 "ABW" "AGO" 533  24                  .
      2018 "ABW" "AGO" 533  24                  .
      2019 "ABW" "AGO" 533  24                  .
      end

      Comment


      • #4
        Given the organization of your data, I do not see any logical way to do exactly what you ask. While it is possible to calculate total trade with China (sum of imports and exports) for each country and make a separate data set out of that, I cannot visualize how to combine those results with the original data, because each observation in the original data represents two countries (and, in most cases, neither of them is China) so it isn't clear which of those countries should get the pairing.

        Here is code to create a new data set that contains, for each country and year (you didn't say you wanted to do this by year, but I assume that was intended) three variables: exports to china, imports from china, and total china trade:

        Code:
        local china = 156    
        preserve
        tempfile to_china
        keep if !missing(tradeflow_baci) & iso3num_d == `china'
        keep year iso3*o tradeflow_baci
        rename (iso3*_o) iso3*
        collapse (sum) exports_to_china = tradeflow_baci, by(iso3* year)
        save `to_china'
        
        restore
        keep if !missing(tradeflow_baci) & iso3num_o == `china'
        keep year iso3*d tradeflow_baci
        rename (iso3*_d) iso3*
        collapse (sum) imports_from_china = tradeflow_baci, by(iso3* year)
        merge 1:1 iso3* year using `to_china'
        egen total_china_trade = rowtotal(exports_to_china imports_from_china)
        You can, if you like, save this data set in another tempfile and then merge this data set back with the original data in whatever way makes sense to you. Do note that any such -merge- will be 1:m or m:1, depending on which data set you start from, and will require you to first rename iso3* back to iso3*_d or iso3*_o in the total trade with china data set in order to match with the appropriate country.

        Note: Because the example data did not contain any observations involving China, and there was no other country for which there were both imports and exports, this code is not thoroughly tested. I believe it will work, but if it does not, please post back with new example data that includes problematic cases.

        Comment


        • #5
          Thank you very much. I will keep working on it and try for myself.

          Comment


          • #6
            Dear Professor Schechter,

            after a while I got back to this project and have a slightly easier but similar problem. Do you think it is possible to just generate the sum of all exports and imports of a country and add this number up? The aim behind this would be to divide the sum of exports and imports by the gdp of the country and use this as a trade openess indicator.
            Creating a new variable containing the "tradeflow_baci" variable for each origin country to obtain total sum of exports and doing the same for each destination country for the imports doesn't seem that diffucult, but how can these two values be connected to each other?

            Kind regards
            Michael

            Comment


            • #7
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input int year str3(iso3_o iso3_d) int(iso3num_o iso3num_d) double tradeflow_baci
              1948 "ABW" "ABW" 533 533                  .
              1949 "ABW" "ABW" 533 533                  .
              1950 "ABW" "ABW" 533 533                  .
              1951 "ABW" "ABW" 533 533                  .
              1952 "ABW" "ABW" 533 533                  .
              1953 "ABW" "ABW" 533 533                  .
              1954 "ABW" "ABW" 533 533                  .
              1955 "ABW" "ABW" 533 533                  .
              1956 "ABW" "ABW" 533 533                  .
              1957 "ABW" "ABW" 533 533                  .
              1958 "ABW" "ABW" 533 533                  .
              1959 "ABW" "ABW" 533 533                  .
              1960 "ABW" "ABW" 533 533                  .
              1961 "ABW" "ABW" 533 533                  .
              1962 "ABW" "ABW" 533 533                  .
              1963 "ABW" "ABW" 533 533                  .
              1964 "ABW" "ABW" 533 533                  .
              1965 "ABW" "ABW" 533 533                  .
              1966 "ABW" "ABW" 533 533                  .
              1967 "ABW" "ABW" 533 533                  .
              1968 "ABW" "ABW" 533 533                  .
              1969 "ABW" "ABW" 533 533                  .
              1970 "ABW" "ABW" 533 533                  .
              1971 "ABW" "ABW" 533 533                  .
              1972 "ABW" "ABW" 533 533                  .
              1973 "ABW" "ABW" 533 533                  .
              1974 "ABW" "ABW" 533 533                  .
              1975 "ABW" "ABW" 533 533                  .
              1976 "ABW" "ABW" 533 533                  .
              1977 "ABW" "ABW" 533 533                  .
              1978 "ABW" "ABW" 533 533                  .
              1979 "ABW" "ABW" 533 533                  .
              1980 "ABW" "ABW" 533 533                  .
              1981 "ABW" "ABW" 533 533                  .
              1982 "ABW" "ABW" 533 533                  .
              1983 "ABW" "ABW" 533 533                  .
              1984 "ABW" "ABW" 533 533                  .
              1985 "ABW" "ABW" 533 533                  .
              1986 "ABW" "ABW" 533 533                  .
              1987 "ABW" "ABW" 533 533                  .
              1988 "ABW" "ABW" 533 533                  .
              1989 "ABW" "ABW" 533 533                  .
              1990 "ABW" "ABW" 533 533                  .
              1991 "ABW" "ABW" 533 533                  .
              1992 "ABW" "ABW" 533 533                  .
              1993 "ABW" "ABW" 533 533                  .
              1994 "ABW" "ABW" 533 533                  .
              1995 "ABW" "ABW" 533 533                  .
              1996 "ABW" "ABW" 533 533                  .
              1997 "ABW" "ABW" 533 533                  .
              1998 "ABW" "ABW" 533 533                  .
              1999 "ABW" "ABW" 533 533                  .
              2000 "ABW" "ABW" 533 533                  .
              2001 "ABW" "ABW" 533 533                  .
              2002 "ABW" "ABW" 533 533                  .
              2003 "ABW" "ABW" 533 533                  .
              2004 "ABW" "ABW" 533 533                  .
              2005 "ABW" "ABW" 533 533                  .
              2006 "ABW" "ABW" 533 533                  .
              2007 "ABW" "ABW" 533 533                  .
              2008 "ABW" "ABW" 533 533                  .
              2009 "ABW" "ABW" 533 533                  .
              2010 "ABW" "ABW" 533 533                  .
              2011 "ABW" "ABW" 533 533                  .
              2012 "ABW" "ABW" 533 533                  .
              2013 "ABW" "ABW" 533 533                  .
              2014 "ABW" "ABW" 533 533                  .
              2015 "ABW" "ABW" 533 533                  .
              2016 "ABW" "ABW" 533 533                  .
              2017 "ABW" "ABW" 533 533                  .
              2018 "ABW" "ABW" 533 533                  .
              2019 "ABW" "ABW" 533 533                  .
              1948 "ABW" "AFG" 533   4                  .
              1949 "ABW" "AFG" 533   4                  .
              1950 "ABW" "AFG" 533   4                  .
              1951 "ABW" "AFG" 533   4                  .
              1952 "ABW" "AFG" 533   4                  .
              1953 "ABW" "AFG" 533   4                  .
              1954 "ABW" "AFG" 533   4                  .
              1955 "ABW" "AFG" 533   4                  .
              1956 "ABW" "AFG" 533   4                  .
              1957 "ABW" "AFG" 533   4                  .
              1958 "ABW" "AFG" 533   4                  .
              1959 "ABW" "AFG" 533   4                  .
              1960 "ABW" "AFG" 533   4                  .
              1961 "ABW" "AFG" 533   4                  .
              1962 "ABW" "AFG" 533   4                  .
              1963 "ABW" "AFG" 533   4                  .
              1964 "ABW" "AFG" 533   4                  .
              1965 "ABW" "AFG" 533   4                  .
              1966 "ABW" "AFG" 533   4                  .
              1967 "ABW" "AFG" 533   4                  .
              1968 "ABW" "AFG" 533   4                  .
              1969 "ABW" "AFG" 533   4                  .
              1970 "ABW" "AFG" 533   4                  .
              1971 "ABW" "AFG" 533   4                  .
              1972 "ABW" "AFG" 533   4                  .
              1973 "ABW" "AFG" 533   4                  .
              1974 "ABW" "AFG" 533   4                  .
              1975 "ABW" "AFG" 533   4                  .
              1976 "ABW" "AFG" 533   4                  .
              1977 "ABW" "AFG" 533   4                  .
              1978 "ABW" "AFG" 533   4                  .
              1979 "ABW" "AFG" 533   4                  .
              1980 "ABW" "AFG" 533   4                  .
              1981 "ABW" "AFG" 533   4                  .
              1982 "ABW" "AFG" 533   4                  .
              1983 "ABW" "AFG" 533   4                  .
              1984 "ABW" "AFG" 533   4                  .
              1985 "ABW" "AFG" 533   4                  .
              1986 "ABW" "AFG" 533   4                  .
              1987 "ABW" "AFG" 533   4                  .
              1988 "ABW" "AFG" 533   4                  .
              1989 "ABW" "AFG" 533   4                  .
              1990 "ABW" "AFG" 533   4                  .
              1991 "ABW" "AFG" 533   4                  .
              1992 "ABW" "AFG" 533   4                  .
              1993 "ABW" "AFG" 533   4                  .
              1994 "ABW" "AFG" 533   4                  .
              1995 "ABW" "AFG" 533   4                  .
              1996 "ABW" "AFG" 533   4                  .
              1997 "ABW" "AFG" 533   4                  .
              1998 "ABW" "AFG" 533   4                  .
              1999 "ABW" "AFG" 533   4                  .
              2000 "ABW" "AFG" 533   4                  .
              2001 "ABW" "AFG" 533   4                  .
              2002 "ABW" "AFG" 533   4                  .
              2003 "ABW" "AFG" 533   4                  .
              2004 "ABW" "AFG" 533   4                  .
              2005 "ABW" "AFG" 533   4                  .
              2006 "ABW" "AFG" 533   4                  .
              2007 "ABW" "AFG" 533   4                  .
              2008 "ABW" "AFG" 533   4                  .
              2009 "ABW" "AFG" 533   4                  .
              2010 "ABW" "AFG" 533   4                  .
              2011 "ABW" "AFG" 533   4                  .
              2012 "ABW" "AFG" 533   4                  .
              2013 "ABW" "AFG" 533   4                  .
              2014 "ABW" "AFG" 533   4                  .
              2015 "ABW" "AFG" 533   4                  .
              2016 "ABW" "AFG" 533   4                  .
              2017 "ABW" "AFG" 533   4                  .
              2018 "ABW" "AFG" 533   4 2030.2830000000001
              2019 "ABW" "AFG" 533   4                  .
              1948 "ABW" "AGO" 533  24                  .
              1949 "ABW" "AGO" 533  24                  .
              1950 "ABW" "AGO" 533  24                  .
              1951 "ABW" "AGO" 533  24                  .
              1952 "ABW" "AGO" 533  24                  .
              1953 "ABW" "AGO" 533  24                  .
              1954 "ABW" "AGO" 533  24                  .
              1955 "ABW" "AGO" 533  24                  .
              1956 "ABW" "AGO" 533  24                  .
              1957 "ABW" "AGO" 533  24                  .
              1958 "ABW" "AGO" 533  24                  .
              1959 "ABW" "AGO" 533  24                  .
              1960 "ABW" "AGO" 533  24                  .
              1961 "ABW" "AGO" 533  24                  .
              1962 "ABW" "AGO" 533  24                  .
              1963 "ABW" "AGO" 533  24                  .
              1964 "ABW" "AGO" 533  24                  .
              1965 "ABW" "AGO" 533  24                  .
              1966 "ABW" "AGO" 533  24                  .
              1967 "ABW" "AGO" 533  24                  .
              1968 "ABW" "AGO" 533  24                  .
              1969 "ABW" "AGO" 533  24                  .
              1970 "ABW" "AGO" 533  24                  .
              1971 "ABW" "AGO" 533  24                  .
              1972 "ABW" "AGO" 533  24                  .
              1973 "ABW" "AGO" 533  24                  .
              1974 "ABW" "AGO" 533  24                  .
              1975 "ABW" "AGO" 533  24                  .
              1976 "ABW" "AGO" 533  24                  .
              1977 "ABW" "AGO" 533  24                  .
              1978 "ABW" "AGO" 533  24                  .
              1979 "ABW" "AGO" 533  24                  .
              1980 "ABW" "AGO" 533  24                  .
              1981 "ABW" "AGO" 533  24                  .
              1982 "ABW" "AGO" 533  24                  .
              1983 "ABW" "AGO" 533  24                  .
              1984 "ABW" "AGO" 533  24                  .
              1985 "ABW" "AGO" 533  24                  .
              1986 "ABW" "AGO" 533  24                  .
              1987 "ABW" "AGO" 533  24                  .
              1988 "ABW" "AGO" 533  24                  .
              1989 "ABW" "AGO" 533  24                  .
              1990 "ABW" "AGO" 533  24                  .
              1991 "ABW" "AGO" 533  24                  .
              1992 "ABW" "AGO" 533  24                  .
              1993 "ABW" "AGO" 533  24                  .
              1994 "ABW" "AGO" 533  24                  .
              1995 "ABW" "AGO" 533  24                  .
              1996 "ABW" "AGO" 533  24                  .
              1997 "ABW" "AGO" 533  24                  .
              1998 "ABW" "AGO" 533  24                  .
              1999 "ABW" "AGO" 533  24                  .
              2000 "ABW" "AGO" 533  24                  .
              2001 "ABW" "AGO" 533  24                  .
              2002 "ABW" "AGO" 533  24                  .
              2003 "ABW" "AGO" 533  24                  .
              2004 "ABW" "AGO" 533  24                  .
              2005 "ABW" "AGO" 533  24                  .
              2006 "ABW" "AGO" 533  24                  .
              2007 "ABW" "AGO" 533  24 4.0440000000000005
              2008 "ABW" "AGO" 533  24                  .
              2009 "ABW" "AGO" 533  24               .483
              2010 "ABW" "AGO" 533  24          49810.867
              2011 "ABW" "AGO" 533  24 .14200000000000002
              2012 "ABW" "AGO" 533  24               .333
              2013 "ABW" "AGO" 533  24                 25
              2014 "ABW" "AGO" 533  24             53.711
              2015 "ABW" "AGO" 533  24                  .
              2016 "ABW" "AGO" 533  24                  .
              2017 "ABW" "AGO" 533  24                  .
              2018 "ABW" "AGO" 533  24                  .
              2019 "ABW" "AGO" 533  24                  .
              end
              
              preserve
              keep iso3*_o year tradeflow_baci
              collapse (sum) exports = tradeflow_baci, by(iso3*_o year)
              rename iso3*_o iso3*
              tempfile holding
              save `holding'
              restore
              keep iso3*_d year tradeflow_baci
              collapse (sum) imports = tradeflow_baci, by(iso3*_d year)
              rename iso3*_d iso3*
              merge 1:1 iso3* year using `holding'
              gen total_trade = exports + imports
              Your example data does not have a GDP variable, so calculating the index you ask for cannot be done. But assuming your real data does have such a variable, you can modify the above code by adding it to the -keep- commands and adding -(first) name_of_gdp_variable- to the -collapse- commands (before the comma), and calculating -gen index = total_trade/name_of_gdp_variable- at the end of the code.

              Comment


              • #8
                Dear Professor Schechter,

                thank you very much. With all the information I was able to estimate all variables I was looking for.

                kind regards
                Michael

                Comment

                Working...
                X