Announcement

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

  • unique pairs from panel data

    Hi, I have balanced panel data with ID and Time (and some other variables), and I want the unique ID-pairs, while preserving the time order. The suggestions I've found seem to offer much more complicated combinations. The simplest example I can think of (while still being clear) has 3 individuals and 3 time periods. In general, if the original data set is n*T obs, the transformed data set should be (n*n-1)/2*T) obs. I can do this with loops, but I'm guessing there is a clever way to do it w/out loops. Thanks for your help.

    ID1 t1
    ID1 t2
    ID1 t3
    ID2 t1
    ID2 t2
    ID2 t3
    ID3 t1
    ID3 t2
    ID3 t3

    I would like the resulting data to look like:
    ID1-ID2 t1
    ID1-ID2 t2
    ID1-ID2 t3
    ID1-ID3 t1
    ID1-ID3 t2
    ID1-ID3 t3
    ID2-ID3 t1
    ID2-ID3 t2
    ID2-ID3 t3

    If it helps, my transformed data could be as simple as the difference in the values of a variable for the different IDs for each time period.

  • #2
    You will likely want to use the joinby command.

    Comment


    • #3
      I don't know if this counts as clever, but it works:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str3 id str2 time
      "ID1" "t1"
      "ID1" "t2"
      "ID1" "t3"
      "ID2" "t1"
      "ID2" "t2"
      "ID2" "t3"
      "ID3" "t1"
      "ID3" "t2"
      "ID3" "t3"
      end
      
      tempfile copy
      save `copy'
      
      rename id id_a
      joinby time using `copy'
      keep if id > id_a
      egen pair = concat(id_a id), punct("-")
      keep pair time
      sort time pair
      In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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.

      Added: Crossed with #2 which highlights the key part of the solution.

      Comment


      • #4
        Thanks much. Very nice.

        Comment


        • #5
          Here is a tiny toot for using distinct as a key term here, not unique. For more, see section 2 in https://www.stata-journal.com/sjpdf....iclenum=dm0042

          Comment

          Working...
          X