Announcement

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

  • Variable and value labels with reshape

    Hello, I have some data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(hhid mem dc01) long(dc02 dc03)
    2 51 2001 2 9
    3  5 1990 2 7
    3 12 1997 2 9
    4  9 2001 3 9
    4 10 2000 3 9
    end
    label values dc02 dc02
    label def dc02 2 "male", modify
    label def dc02 3 "female", modify
    label values dc03 dc03
    label def dc03 7 "nephew/niece", modify
    label def dc03 9 "grandchild", modify

    I am trying to reshape the data to wide. I do:
    Code:
    by hhid, sort: gen number= _n
    reshape wide dc*, i(hhid) j(number)
    This works fine, but I lose the value and variable labels. I've tried following http://www.stata.com/support/faqs/da...after-reshape/, but it doesn't seem to work even with their example data. The variable labels end up as " : 80" instead of "year: 80"

    Ultimately, I would like to take the variable labels such as "Gender: Number *" where * is the corresponding number from the number variable I generated. This seems to be exactly what the above link is doing, but is not working. Thanks for any help.

  • #2
    This works fine
    Actually, it doesn't.

    Code:
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input double(hhid mem dc01) long(dc02 dc03)
    
    hhid mem dc01 dc02 dc03
    1. 2 51 2001 2 9
    2. 3 5 1990 2 7
    3. 3 12 1997 2 9
    4. 4 9 2001 3 9
    5. 4 10 2000 3 9
    6. end
    
    . label values dc02 dc02
    
    . label def dc02 2 "male", modify
    
    . label def dc02 3 "female", modify
    
    . label values dc03 dc03
    
    . label def dc03 7 "nephew/niece", modify
    
    . label def dc03 9 "grandchild", modify
    
    .
    . des
    
    Contains data
    obs: 5
    vars: 5
    size: 160
    --------------------------------------------------------------------------------------------------------------------------------------------
    storage display value
    variable name type format label variable label
    --------------------------------------------------------------------------------------------------------------------------------------------
    hhid double %10.0g
    mem double %10.0g
    dc01 double %10.0g
    dc02 long %12.0g dc02
    dc03 long %12.0g dc03
    --------------------------------------------------------------------------------------------------------------------------------------------
    Sorted by:
    Note: Dataset has changed since last saved.
    
    . list, noobs clean
    
    hhid mem dc01 dc02 dc03
    2 51 2001 male grandchild
    3 5 1990 male nephew/niece
    3 12 1997 male grandchild
    4 9 2001 female grandchild
    4 10 2000 female grandchild
    
    .
    . by hhid, sort: gen number= _n
    
    . reshape wide dc*, i(hhid) j(number)
    (note: j = 1 2)
    variable mem not constant within hhid
    Your data are currently long. You are performing a reshape wide. You typed something like
    
    . reshape wide a b, i(hhid) j(number)
    
    There are variables other than a, b, hhid, number in your data. They must be constant within hhid because that is the only way they
    can fit into wide data without loss of information.
    
    The variable or variables listed above are not constant within hhid. Perhaps the values are in error. Type reshape error for a list
    of the problem observations.
    
    Either that, or the values vary because they should vary, in which case you must either add the variables to the list of xij variables
    to be reshaped, or drop them.
    r(9);
    Except for the -des- and -list- commands, all of those commands are copy/pasted directly from your post to Stata's do-file editor. So, either the mem variable is incorrect and needs to be made constant within hhid, or you need to include mem in the list of variables to be "widened."

    That said, let's just take mem out of the picture altogether to illustrate the approach. I also note that you have no variable labels to lose here, so I've added some, again to illustrate. Your situation is a bit more complicated than the one in the link you show, because you want to get variable labels that incorporate the value of "number." But it's just a little wrinkle:

    Code:
    set more off
    clear*
    
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(hhid dc01) long(dc02 dc03)
    2 2001 2 9
    3 1990 2 7
    3 1997 2 9
    4 2001 3 9
    4 2000 3 9
    end
    label var dc01 "Year"
    label var dc02 "Sex"
    label values dc02 dc02
    label def dc02 2 "male", modify
    label def dc02 3 "female", modify
    label var dc03 "Relationship"
    label values dc03 dc03
    label def dc03 7 "nephew/niece", modify
    label def dc03 9 "grandchild", modify
    
    des
    
    ds dc0*
    local dc_vars `r(varlist)'
    foreach v of varlist `dc_vars' {
        local var_label_`v': var label `v'
    }
    
    by hhid, sort: gen number= _n
    reshape wide dc*, i(hhid) j(number)
    
    foreach d of local dc_vars {
        foreach v of varlist `d'* {
            local number: subinstr local v "`d'" ""
            label var `v' `"`var_label_`d'' `number'"'
        }
    }
    That said, if this were my data, rather than labeling these variables, or, in addition to labeling them, I would rename them descriptively. Later on when you use these variables you're going to be having to remember, or refer back somewhere, which dc is year, which is sex, and which is relationship. So I would just rename them that way before doing the -reshape-, and then I would have mnemonic names from that point forward; you might not even need to label them at all.

    Comment


    • #3
      Thanks, Clyde. I made a mistake writing my code, as I did include mem in the reshape command. My data has some other variables which are less clear (meaning there is a need for variable labels) but I will take your advice and change the variable names as well.

      Comment

      Working...
      X