Announcement

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

  • How to expand the panel data to pairs

    Hi All,

    I am struggling to combine my long panel data to pairs and wonder if anyone can give me a hint.

    Suppose I have a long panel dataset

    Time Country Variable
    t1 a a1
    t2 a a2
    t3 a a3
    t1 b b1
    t2 b b2
    t3 b b3
    t1 c c1
    t2 c c2
    t3 c c3

    I would like to convert this dataset to following format

    Time Country Pair Variable
    t1 ab a1-b1
    t2 ab a2-b2
    t3 ab a3-b3
    t1 ac a1-c1
    t2 ac a2-c2
    t3 ac a3-c3
    .....
    .....
    t1 cb c1-b1
    t2 cb c2-b2
    t3 cb c3-b3

    I thought about first converting the long format to wide format but I have 100+ variables, and 50+ countries, so the wide format will have a lot of variables.

    Any hint or suggestions are extremely appreciated!!

    Thanks!



  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2 time str1 country str2 variable
    "t1" "a" "a1"
    "t2" "a" "a2"
    "t3" "a" "a3"
    "t1" "b" "b1"
    "t2" "b" "b2"
    "t3" "b" "b3"
    "t1" "c" "c1"
    "t2" "c" "c2"
    "t3" "c" "c3"
    end
    
    
    preserve
    tempfile copy
    ds time, not
    rename (`r(varlist)') =_2
    save `copy'
    
    restore
    joinby time using `copy'
    drop if country == country_2
    
    replace country = country + country_2
    replace variable = variable + "-" + variable_2
    drop *_2
    
    sort country time
    n the future, when showing data examples, please use the -dataex- command to do so, as I have in this response. If you are running version 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
      Originally posted by Clyde Schechter View Post
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str2 time str1 country str2 variable
      "t1" "a" "a1"
      "t2" "a" "a2"
      "t3" "a" "a3"
      "t1" "b" "b1"
      "t2" "b" "b2"
      "t3" "b" "b3"
      "t1" "c" "c1"
      "t2" "c" "c2"
      "t3" "c" "c3"
      end
      
      
      preserve
      tempfile copy
      ds time, not
      rename (`r(varlist)') =_2
      save `copy'
      
      restore
      joinby time using `copy'
      drop if country == country_2
      
      replace country = country + country_2
      replace variable = variable + "-" + variable_2
      drop *_2
      
      sort country time
      n the future, when showing data examples, please use the -dataex- command to do so, as I have in this response. If you are running version 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.

      Dear Clyde,

      Thank you so much for your help! It works perfectly. I will use dataex- in the future when I post here. Thank you again for your help and suggestions.

      Best,
      Li

      Comment

      Working...
      X