Announcement

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

  • reshape

    Hi everyone,
    I have a long dataset and used this code to reshape it to wide format:
    Code:
    ds hhidpn wave, not
    reshape wide `r(varlist)', i(hhidpn) j(wave)
    I was wondering to know how can I reshape the data to long format once again. I tried the below code, but it did not work:
    Code:
    reshape long `r(varlist)' , i(hhidpn) j(wave)
    Thanks.
    Nader






  • #2
    After the first -reshape wide- runs, the `r(varlist)' disappears, because -reshape wide- itself leaves behind things in r(), so the previous contents of r() are gone.

    Here's what you can do:

    Code:
    ds hhidpn wave, not
    local reshape_vars `r(varlist)'
    
    reshape wide `reshape_vars', i(hhidpn) j(wave)
    
    reshape long `reshape_vars', i(hhidpn) j(wave)
    By preserving the contents of `r(varlist)' in a local macro, you can resuse it
    Actually, you don't even have to go to all that trouble. If you don't do anything between the two -reshape-s that disturbs the organization of the data, the second command can just simply be -reshape long- and Stata will remember what the variables, i(), and j() from the previous reshape were and use them.

    Comment


    • #3
      Thanks. I got an error when trying to reshape from wide to long:
      reshape long `reshape_vars', i(hhidpn) j(wave) invalid syntax r(198);

      Comment


      • #4
        My best guess is that you did not run the code all continuously from the point where local reshape_vars is defined and the point where you tried to -reshape long-. If there was any interruption in the code between those points, then -reshape_vars- would no longer exist and that would cause that problem.

        Comment

        Working...
        X