JBRA
JBRA

Reputation: 49

Reshaping when year and countries are both columns

I am trying to reshape some data. The issue is that usually data is either long or wide but this seems to be set up in a way that I cannot figure out how to reshape. The data looks as follows:

year   australia   canada  denmark ...
1999   10          15      20
2000   12          16      25 
2001   14          18      40

And I would like to get it into a panel format like the following

year    country    gdppc
1999    australia  10
2000    australia  12
2001    australia  14
1999    canada     16
2000    canada     18

Upvotes: 0

Views: 420

Answers (1)

Nick Cox
Nick Cox

Reputation: 37183

The problem is just in the variable names. See e.g. this FAQ for the advice that you may need rename first before you can reshape.

For more complicated variants of this problem with similar data, see e.g. this paper.

clear 
input year   australia   canada  denmark 
1999   10          15      20
2000   12          16      25 
2001   14          18      40
end 
rename (australia-denmark) gdppc= 
reshape long gdppc , i(year) string j(country) 
sort country year 
list, sepby(country) 

     +--------------------------+
     | year     country   gdppc |
     |--------------------------|
  1. | 1999   australia      10 |
  2. | 2000   australia      12 |
  3. | 2001   australia      14 |
     |--------------------------|
  4. | 1999      canada      15 |
  5. | 2000      canada      16 |
  6. | 2001      canada      18 |
     |--------------------------|
  7. | 1999     denmark      20 |
  8. | 2000     denmark      25 |
  9. | 2001     denmark      40 |
     +--------------------------+

Upvotes: 1

Related Questions