Announcement

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

  • Creating New Observations in Panel Data with Multiple Identifying Variables

    Hello all. I am facing the following issue in my research. I have panel data for sector level international trade flows, for approx. 90 sectors/industries. The issue is, for country-pairs which do not have any recorded sector-level trade, the sector is simply ommitted from the data. I would like to have all sectors available for all country-pairs, and for sectors in which there is no recorded trade data, to have a 0 as the "value" of the trade flow. Below is a sample of the data as-is:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str3(exp imp) byte s int v
    2000 "USA" "ARG" 1 1000
    2000 "USA" "ARG" 2 2000
    2000 "USA" "ARG" 3   50
    2000 "AUS" "NED" 2   25
    end
    Ideally, the AUS-NED trade flow would have sectors 1 and 3 added for the year 2000, with v equal to 0. Thank you!

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str3(exp imp) byte s int v
    2000 "USA" "ARG" 1 1000
    2000 "USA" "ARG" 2 2000
    2000 "USA" "ARG" 3   50
    2000 "AUS" "NED" 2   25
    end
    
    fillin year exp imp s
    replace v = 0 if _fillin
    Note: The above code may create exp-imp pairs that you do not want. For example, AUS-ARG and USA-NED will be created by the above code. If that's not what you intend:
    Code:
    egen stratum = group(year exp imp)
    tsset stratum s
    tsfill, full
    replace v = 0 if v == .
    by stratum (year), sort: replace year = year[1]
    by stratum (year), sort: replace exp = exp[1]
    by stratum (year): replace imp = imp[1]
    will fill in the missing sectors, but preserve only the year-exp-imp combinations found in the original data.
    Last edited by Clyde Schechter; 18 Jan 2023, 11:47.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      [code]

      Code:
      egen stratum = group(year exp imp)
      tsset stratum s
      tsfill, full
      replace v = 0 if v == .
      by stratum (year), sort: replace year = year[1]
      by stratum (year), sort: replace exp = exp[1]
      by stratum (year): replace imp = imp[1]
      will fill in the missing sectors, but preserve only the year-exp-imp combinations found in the original data.
      Brilliant. This appears to be exactly what I have been after. Thanks very much.

      Comment

      Working...
      X