Announcement

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

  • Help restructuring quasi-wide/quasi-long data to truly long

    Hello everyone,

    I am using v.15.1 SE.

    I need help restructuring my data to be truly long; because it is quasi-wide/long, the reshape long command , as well as others, will not work. I imagine *multiple* steps will be needed to accomplish this...

    The current data structure is as follows:
    Person Days Var1 Var2 Var3
    1.00 1.00 2.00 3.00 6.00
    1.00 2.00 3.00 3.00 4.00
    1.00 3.00 4.00 2.00 5.00
    1.00 4.00 1.00 3.00 4.00
    2.00 1.00 3.00 4.00 4.00
    2.00 2.00 5.00 7.00 7.00
    2.00 3.00 4.00 6.00 7.00
    2.00 4.00 5.00 9.00 7.00

    I would like to get it to be as follows:
    Person Days Var Response
    1.00 1.00 1 2.00
    1.00 1.00 2 3.00
    1.00 1.00 3 6.00
    1.00 2.00 1 3.00
    1.00 2.00 2 3.00
    1.00 2.00 3 4.00
    1.00 3.00 1 4.00
    1.00 3.00 2 2.00
    1.00 3.00 3 5.00
    1.00 4.00 1 1.00
    1.00 4.00 2 3.00
    1.00 4.00 3 4.00
    2.00 1.00 1 3.00
    2.00 1.00 2 4.00
    2.00 1.00 3 4.00
    2.00 2.00 1 5.00
    2.00 2.00 2 7.00
    2.00 2.00 3 7.00
    2.00 3.00 1 4.00
    2.00 3.00 2 6.00
    2.00 3.00 3 7.00
    2.00 4.00 1 5.00
    2.00 4.00 2 9.00
    2.00 4.00 3 7.00

    I don't see a way to do this using stack or reshape, as I mentioned above. I would like to use foreach along with forvalues, but am having difficulty getting these looping commands to do what I need.
    The challenge is with the Days and Var* variables. Restructuring them the way I want would require 4 (days) X 3 (variables) = 12 rows per person.

    There's is a way to do this using another software program, but I would like to automate everything, elegantly, and do it all in Stata.

    Thanks in advance,
    Saul


  • #2
    Perhaps you were missing the concept that the i() in -reshape- can take a varlist, not just a varname:
    Code:
    clear
    input Person Days Var1 Var2 Var3
    1.00 1.00 2.00 3.00 6.00
    1.00 2.00 3.00 3.00 4.00
    1.00 3.00 4.00 2.00 5.00
    1.00 4.00 1.00 3.00 4.00
    2.00 1.00 3.00 4.00 4.00
    2.00 2.00 5.00 7.00 7.00
    2.00 3.00 4.00 6.00 7.00
    2.00 4.00 5.00 9.00 7.00
    end
    //
    rename Var* Response*
    reshape long Response, i(Person Days) j(Var)
    list

    Comment


    • #3
      Mike, this did it! Thank you. I had indeed missed that.
      Is there a way to see the routines/subroutines (or workflow) for this particular command? That is, to get a behind-the-scenes look?

      Comment


      • #4
        If you issue the command
        Code:
        viewsource reshape.ado
        you will see that indeed reshape is implemented as an ado rather than a built-in command. That's the good news; the bad news is you will see all 1,875 lines of code that it takes to implement reshape.

        Comment


        • #5
          Thanks William!

          Comment

          Working...
          X