Announcement

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

  • Help on reshaping from wide to long format

    Hi all,

    This should be an easy task for someone who is familiar with Stata but unfortunately not for me.
    I have panel data (say, log CO2 emissions) for 60 countries covering more than 40 years. A brief part of the data is something like below. I am trying to use the reshape command to convert the data to a long format. The problem I am having is that the country names do not begin with the same stubname. What syntax should I use to reshape the data to long format?
    I really appreciate any help.
    Year Australia Austria Belgium Canada Germany ...
    2000 0.55 0.35 0.45 0.47 . .
    2001 0.56 0.36 0.44 0.50 . .
    2002 0.56 0.33 0.48 0.53 . .
    2003 0.59 0.38 0.52 0.55 . .
    2004 0.60 0.39 0.53 0.59 . .
    2005 0.61 0.38 0.56 0.62 . .
    2006 0.65 0.37 0.58 0.63 . .
    2007 0.68 0.40 0.59 0.68 . .
    2008 0.69 0.41 0.60 0.69 . .

  • #2
    Please don't use table to show data. In future, use command dataex, like this:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(Year Australia Austria Belgium Canada Germany)
    2000 .55 .35 .45 .47 .
    2001 .56 .36 .44  .5 .
    2002 .56 .33 .48 .53 .
    2003 .59 .38 .52 .55 .
    2004  .6 .39 .53 .59 .
    2005 .61 .38 .56 .62 .
    2006 .65 .37 .58 .63 .
    2007 .68  .4 .59 .68 .
    2008 .69 .41  .6 .69 .
    end
    It can be more quickly read into Stata.

    As for the question, just use a loop to add a prefix to the country names:

    Code:
    foreach x of varlist Australia - Germany{
        rename `x' co2`x'
    }
    
    reshape long co2, i(Year) j(country, string)

    Comment


    • #3
      Ken Chui Thank you so much. Works like a charm.

      Comment

      Working...
      X