Announcement

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

  • Reshape wide returns j variable not found

    Hi,

    I am trying to reshape a long data set to wide (one observation per patient id). However, I keep getting a 'variable xxxx not found' error. This is especially confusing because prior there was a duplicate of j within i and this was identified, but once the duplicate observation was deleted the variable not found error appeared. Stata12 is up to date and I receive the same error if I try to reshape in Stata13. I also tried using other variables for j and get the same error. Any ideas of where I am going wrong or ideas for a work around would be helpful.

    Thank you,
    Kerry

    reshape wide _all, i(demo_ptid) j(monthmerge)
    (note: j = 0 12)
    variable monthmerge not found
    r(111);

    reshape wide _all, i(demo_ptid) j(sc_visitcode)
    (note: j = 1000 1120 2000 2120 2121)
    variable sc_visitcode not found
    r(111);


  • #2
    With many Stata commands that require a varlist, _all is interpreted as a wildcard meaning all variables. You would not expect this to affect reshape because it expects stubnames but with a reshape wide, the stubnames are actual variable names in the data. Consider the following example

    Code:
    clear
    input id year a b
    1 1990 1 2
    1 1991 3 4
    1 1992 5 6
    2 1990 7 8
    2 1991 9 10
    2 1992 11 12
    end
    
    reshape wide a b, i(id) j(year)
    will result in

    Code:
    . list
    
         +----------------------------------------------------+
         | id   a1990   b1990   a1991   b1991   a1992   b1992 |
         |----------------------------------------------------|
      1. |  1       1       2       3       4       5       6 |
      2. |  2       7       8       9      10      11      12 |
         +----------------------------------------------------+
    If you use instead

    Code:
    clear
    input id year a b
    1 1990 1 2
    1 1991 3 4
    1 1992 5 6
    2 1990 7 8
    2 1991 9 10
    2 1992 11 12
    end
    
    reshape wide _all, i(id) j(year)
    you get the following error message

    Code:
    . reshape wide _all, i(id) j(year)
    (note: j = 1990 1991 1992)
    variable year not found
    r(111);

    Comment


    • #3
      Thank you, I ended up reordering my variables so that i and j were the first two variables and not included the variables specified for reshaping.

      reshape wide couplenum-pout2_statusmeasurementtype, i(demo_ptid) j(monthmerge)

      Comment


      • #4
        Indeed. Another way to specify all variables except those used in i() and j() is

        Code:
        ds id year, not
        reshape wide `r(varlist)', i(id) j(year)

        Comment

        Working...
        X