Announcement

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

  • reshape

    I want to change the data format from

    x country year val
    A CN 1980 .1
    A CN 1981 .2
    A US 1980 .3
    A US 1981 .4
    B CN 1980 .5
    B CN 1981 .6
    B US 1980 .7
    B US 1981 .8

    to the followng format

    country year xA xB
    CN 1980 .1 .5
    CN 1981 .2 .6
    US 1980 .3 .7
    US 1981 .4 .8

    Not sure how to use -reshape- to do it or any other Stata command? You suggestion will be highly appreciated!

    Andrew

  • #2
    Code:
    clear
    input str1 x str2 country year val
    A CN 1980 .1
    A CN 1981 .2
    A US 1980 .3
    A US 1981 .4
    B CN 1980 .5
    B CN 1981 .6
    B US 1980 .7
    B US 1981 .8
    end
    
    reshape wide val, i(country year) j(x) string
    
    // admire the result
    list, sepby(country)
    It is tradition on this list to post under our real name (first and last). We believe that that has contributed to the friendly and professional atmosphere on this list. That is why the Statalist FAQ asks you to do the same. You can change your login name by pressing the "contact us" button on the bottom right of the screen.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Andrew your data has multiple issues, it has repeat time values within panel, i have changed your data set and then applied the following code
      Code:
      x    country    year    val    i    
      A    CN         1980    .1    CN    
      A    CN         1981    .2    CN    
      B    US         1980    .3    US  
      B    US         1981    .8    US    
      
      
      encode  country, g(i)
      reshape wide val, i(i) j(year)
      
      Results
      i    val1980    val1981    country    
      CN    .1        .2          CN    
      US    .3        .8          US    
      Last edited by Attaullah Shah; 28 Sep 2014, 06:15.
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Terrific! Thanks a lot! I'll change my login name soon.

        Comment


        • #5
          Excellent piece of work Maarten
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment

          Working...
          X