Announcement

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

  • Problem with -reshape long- applying value label to j values

    Hello,

    I'm reshaping a dataset from wide to long, and the - reshape long - command is applying a random previously undefined value label to my new j variable. This happens regardless of whether running a do file or using the menu. I'm using Stata 14 MP.

    Here's a sample of what the data looks like and the code i am using. It's the same problem whether I use my entire dataset or the reduced version I show here.


    br
    hhid x1 y1 x2 y2 x3 y3 x4 y4 x5 y5
    001 3.945205 No 6.082192 No 9.008219 No 11.90137 No 17.95069 Yes
    002 3.912329 No 6.049315 No 9.008219 No 11.96712 No 17.91781 Yes
    003 3.945205 No 5.983562 No 8.975343 No 12.23014 No 17.95069 Yes

    reshape clear
    reshape long x y, i(hhid) j(t)

    br
    hhid t x y
    001 Books 3.945205 No
    001 Pots 6.082192 No
    001 Plates from clay /plastic/tin 9.008219 No
    001 Cutlery 11.90137 No
    001 Containers to carry water (clay pot/jar, basin) 17.95069 Yes
    002 Books 3.912329 No
    002 Pots 6.049315 No
    002 Plates from clay /plastic/tin 9.008219 No
    002 Cutlery 11.96712 No
    002 Containers to carry water (clay pot/jar, basin) 17.91781 Yes
    003 Books 3.945205 No
    003 Pots 5.983562 No
    003 Plates from clay /plastic/tin 8.975343 No
    003 Cutlery 12.23014 No
    003 Containers to carry water (clay pot/jar, basin) 17.95069 Yes

    desc t // the j variable now has a value and a variable label

    storage display value
    variable name type format label variable label
    -----------------------------------------------------------------------
    t byte %47.0g item Variable Name



    If I try dropping the item value label before reshaping, I get an error that it does not exist.
    . lab drop item
    value label item not found



    Has anyone encountered this before? I've never seen reshape act this way before. It's an easy fix but I wonder what is causing the issue.

    Thanks,

    Lily

  • #2
    I found myself wondering about the same issue recently, and wanted to post what I found in case it's helpful to others. Here's an example that mimics the behavior that you're seeing. In this example, the random variables "k1" and "k2" inherit the value label from the "foreign" variable.

    Code:
    sysuse auto, clear
    split make, gen(company)
    collapse (mean) mean_mpg = mpg , by(company1        foreign )
    reshape wide     mean_mpg    , i(company1) j(    foreign )
    char list                /* note: _dta[__JValLab]: 0 `"Domestic"'  1 `"Foreign"' is causing the unexpected labeling         */
    reshape clear
    char list                 /* some characteristics have been removed, but **not** _dta[__JValLab]                 */
    keep company1
    gen k1 = runiform()
    gen k2 = runiform()
    reshape long     k@, i(company1) j(    rand)
    des *
    list * if _n<=5

    In the code above I added the -reshape clear- command because I was hoping it might remove the "JValLab" characteristic, but it did not. One ugly hack is to "manually" reset the characteristic:
    Code:
    char _dta[__JValLab] something_here
    But perhaps others have a better solution!

    Note: this issue could be especially baffling if you never did the initial -reshape wide- yourself (for example, you're using the data set "temp.dta" that was provided to you). In the code below I show how merging the data set "temp.dta" causes "rand" to be labeled with the value label from the variable "foreign."

    Code:
    sysuse auto, clear
    split make, gen(company)
    collapse (mean) mean_mpg = mpg , by(company1        foreign )
    reshape wide     mean_mpg  , i(company1) j(    foreign )
    reshape clear
    keep company1
    gen k1 = runiform()
    gen k2 = runiform()
    sa      "temp", replace   /* assume you don't know how this data set was constructed and you didn't bother to check the characteristics */
    
    sysuse auto, clear
    split make, gen(company)
    collapse (mean) mean_mpg = mpg , by(company1 )
    mer 1:1 company using "temp"
    reshape long     k@, i(company1) j(    rand)
    des *
    list * if _n<=5
    Hope this is useful for others!

    My system:
    Stata/MP 15.1 for Windows (64-bit x86-64)
    Revision 08 May 2018

    Comment

    Working...
    X