Announcement

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

  • Reshaping Long data to wide data Question

    I hope all is well. I am trying to perform a reshape, and I cannot quite figure out exactly how to make this work. I am attempting to move my data from a long format to a wide format, and I need this to be done by a specific group.

    My data are as follows:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    * dataex id time dyad person condition d partnum obs_id
    clear
    input int id byte(time dyad person condition) float(d partnum obs_id)
    201 1 1 1 1  -7 1  1
    202 1 1 2 2  -8 2  1
    201 2 1 1 1  -8 1  2
    202 2 1 2 2  -1 2  2
    203 1 2 1 1   6 1  3
    204 1 2 2 2  -4 2  3
    203 2 2 1 1   1 1  4
    204 2 2 2 2  -3 2  4
    205 1 3 1 1  -9 1  5
    206 1 3 2 2  -3 2  5
    205 2 3 1 1  -6 1  6
    206 2 3 2 2   5 2  6
    207 1 4 1 1  -2 1  7
    208 1 4 2 2 -19 2  7
    207 2 4 1 1   1 1  8
    208 2 4 2 2  -8 2  8
    209 1 5 1 1  10 1  9
    210 1 5 2 2  -3 2  9
    209 2 5 1 1   8 1 10
    210 2 5 2 2   2 2 10
    end
    To assist in understanding what I am trying to do, I, manually, created a dataset to show what I need the end product to be. The dataset is as follows:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    *dataex obs_id dyad time condition1 id1 person1 d1 condition2 id2 person2 d2
    clear
    input float(obs_id dyad time condition1 id1 person1 d1 condition2 id2 person2 d2)
     1 1 1 1 201 1 -7 2 202 2  -8
     2 1 2 1 201 1 -8 2 202 2  -1
     3 2 1 1 203 1  6 2 204 2  -4
     4 2 2 1 203 1  1 2 204 2  -3
     5 3 1 1 205 1 -9 2 206 2  -3
     6 3 2 1 205 1 -6 2 206 2   5
     7 4 1 1 207 1 -2 2 208 2 -19
     8 4 2 1 207 1  1 2 208 2  -8
     9 5 1 1 209 1 10 2 210 2  -3
    10 5 2 1 209 1  8 2 210 2   2
    end
    I have tried the following code, in Stata 15.1, to accomplish this:

    Code:
     reshape wide d, i(obs_id) j(partnum)
    When I run this, these are the following errors that I am getting:
    1. variable id not constant within obs_id
    2. variable person not constant within obs_id
    3. variable condition not constant within obs_id
    I have tried some packages (e.g., dyadid and another one that I cannot think of the name right now), but they do not seem to get the data all the way restructured correctly.
    In addition, I have developed the analytics, in a specific way, that will require my data be formatted this way. If something like this has been posted elsewhere and I missed it in my search, please, point me in the correct direction. Regardless, will someone help understand how to get the reshape code correct to accomplish this? My research will be able to move forward. Thank you in advance for your time, consideration, and assistance.

  • #2
    Everything in your original command is in the right place, but you haven't included all of the necessary variables. Try:

    Code:
    reshape wide id person condition d, i(obs_id dyad time) j(partnum)

    Comment


    • #3
      Ali Atia,
      It worked exactly the way that I need it. Thank you so much for clarifying this.

      Comment


      • #4
        Code:
        reshape wide condition id person d, i(obs_id) j(partnum)
        Added: Crossed with #2 and #3. Note that the specification of dyad and time in the -i()- option of #2, although harmless, is not necessary because dyad and time are constant within obs_id.
        Last edited by Clyde Schechter; 30 Dec 2021, 18:55.

        Comment


        • #5
          Clyde,
          This is worked, and it is great as well. I see your logic here. Always nice to have options. Thank you.

          Comment

          Working...
          X