Announcement

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

  • Reshape data to wide format

    Hi all,

    I am trying to format my data (dta file) to wide format.

    I did try with collapse but lamentably gave me a long format, example

    Code:
    clear all
    webuse auto
    collapse (sum) mpg, by(foreign rep78)
    Click image for larger version

Name:	Sin título.png
Views:	1
Size:	5.1 KB
ID:	1525980


    My desired data format looks like:

    Code:
    table foreign rep78, c(sum mpg)
    Click image for larger version

Name:	Sin título2.png
Views:	1
Size:	4.0 KB
ID:	1525981


    Please any advice could help me.
    Thanks in advance.
    Rodrigo.

  • #2
    Try
    Code:
    clear all
    sysuse auto
    collapse (sum) mpg, by(foreign rep78)
    drop if missing(rep78)
    reshape wide mpg, i(foreign) j(rep78)
    list
    Produces:
    Code:
         +---------------------------------------------+
         |  foreign   mpg1   mpg2   mpg3   mpg4   mpg5 |
         |---------------------------------------------|
      1. | Domestic     42    153    513    166     64 |
      2. |  Foreign      .      .     70    224    237 |
         +---------------------------------------------+

    Comment


    • #3
      Thanks Sergiy Radyakin works greats!

      Regards.

      Comment

      Working...
      X