Announcement

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

  • Reshaping Data

    Hi,

    I need to reshape my dataset wide. I've been on many forums and have used the help function in stata, yet I'm still having some issues. Here's how my data currently looks:

    Month Dr. Frequency
    Apr2022 Smith 17
    Apr2022 Nelson 84
    May2022 Smith 12
    May2022 Nelson 8
    Jun2022 Smith 100
    Jun2022 Nelson 51

    I'd like to reshape wide and format it as:

    Dr. Apr2022 May2022 Jun2022
    Smith 17 12 100
    Nelson 84 8 51

    Here's my current code:

    egen x = group(Month Dr.)

    sort x
    quietly by x: gen test=_n

    reshape wide Month Dr. Frequency, i(x) j(test)

    Any suggestions would be very help.

    Thank you

  • #2
    Data example with dataex please.

    Comment


    • #3
      Here you go. Sorry about that. I'm using Stata 17

      clear
      input str24 Dr str7 Month long Frequency
      "Alder John W" "Apr2021" 11
      "Jennings, Scott D" "Apr2021" 14
      "Bradley, David R" "Apr2021" 5
      "Alder, John W" "Aug2021" 11
      "Jennings, Scott D" "Aug2021" 18
      "Bradley, David R" "Aug2021" 14
      "Alder, John W" "Dec2021" 22
      "Jennings, Scott D" "Dec2021" 54
      "Bradley, David R" "Dec2021" 18

      Comment


      • #4
        Code:
        clear
        input str24 Dr str7 Month long Frequency
        "Alder John W" "Apr2021" 11
        "Jennings, Scott D" "Apr2021" 14
        "Bradley, David R" "Apr2021" 5
        "Alder, John W" "Aug2021" 11
        "Jennings, Scott D" "Aug2021" 18
        "Bradley, David R" "Aug2021" 14
        "Alder, John W" "Dec2021" 22
        "Jennings, Scott D" "Dec2021" 54
        "Bradley, David R" "Dec2021" 18
        end
        
        rename Frequency F
        reshape wide F, i(Dr) j(Month) string
        rename F* *
        Added: That said, be aware that nearly all Stata analysis and data management commends work best (or only) with long layout data. So unless you know for certain that you are going to be doing one of the few things with this data that will work better with wide data, my advice is to not do this at all and leave the data as you already have them.
        Last edited by Clyde Schechter; 24 Apr 2023, 13:58.

        Comment


        • #5
          Thank you so much, Clyde! And duly noted!

          Comment

          Working...
          X