Announcement

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

  • create simultaneous pairs for multiple variables


    I am trying to create dyads in my Stata data set for multiple variables, at the same time. Here is what I have:

    Year id Freedom
    2005 1 6.8
    2006 1 7.0
    .
    .
    2016 1 5.5
    2005 2 3.8
    2006 2 4.2
    .
    .
    2016 2 5.4

    I would like to turn it into this

    Year id_1 id_2 Freedom_1 Freedom_2
    2005 1 2 6.8 3.8
    2006 1 2 7.0 4.2
    .
    .
    2016 2 1 5.4 5.5

    where id is the number of US states and goes from 1 to 50
    Last edited by Giacomo DP; 28 Jun 2022, 01:45.

  • #2
    Code:
    clear
    input Year id Freedom
    2005 1 6.8
    2006 1 7.0
    2016 1 5.5
    2005 2 3.8
    2006 2 4.2
    2016 2 5.4
    end
    
    rename Freedom Freedom_
    
    reshape wide Freedom_, i(Year) j(id)
    
    list
    Results:

    Code:
         +----------------------------+
         | Year   Freedo~1   Freedo~2 |
         |----------------------------|
      1. | 2005        6.8        3.8 |
      2. | 2006          7        4.2 |
      3. | 2016        5.5        5.4 |
         +----------------------------+
    Having said the above, I think it's generally a bad idea to change long form into wide form. It's a lot easier to use "if" or a "foreach" loop to manage a long data than to juggle 50 variables separately.
    Last edited by Ken Chui; 28 Jun 2022, 08:03.

    Comment

    Working...
    X