Announcement

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

  • Apply variable labels after reshape from wide to long

    I found the excellent thread Stata | FAQ: How can I apply the original value and variable labels after using the reshape command?, which gives instructions on how to apply variable labels after reshaping from long to wide.
    I am trying to do the same by reshaping wide to long.

    I am working with harmonized HRS data. The data looks something like this
    HTML Code:
    r2satlifez      double  %10.0g                r2satlifez:w2 R Satisfied with life
                                                    z-score
    where the variable label is commonly described as r#varname:w# R VarDescription, or the same with S or H replacing R when not the respondent but spouse or household is interviewed.
    I would like this to take this information, remove the wave number and add to a unique local (i.e. the same local for all wave responses, meaning r1vardescription and r2vardescription should just move into a single local without the number of the survey wave).

    This should be possible, but is a bit complicated for the inexperienced programmer. Do you have any ideas?

    PS: Why is this important at all? I am reshaping data for easy use among other researchers. I aim to do this to simplify other people`s work.

    Edit: an additional complication is constituted by the fact that not for all variables there exists a variable / variable label as this variable may be missing.
    Last edited by Castor Comploj; 13 Jul 2023, 12:52.

  • #2
    The detailed solution to this is:
    HTML Code:
    ***store variable labels from -wide- format (I) to copy into -long- reshaped dataset later (II)***    
    **(I) store labels**
    display "`vrlistset'"
        loc varasstringlist ""
    loc vlabellist ""
    foreach v of local vrlistset {     /*use only the variables that actually exist*/
    local `v'label: variable label `v'    /*store the labels of these variables into "varnamer(/s/h)#"*/
    local `v'label = substr("``v'label'", strpos("``v'label'", " ") + 1, .) /*use only substring of label*/
    display "``v'label'"
        local varasstringlist `" `varasstringlist'   "``v'label'" "'
    label variable `v' "``v'label'"     /*relabel the variable with the new substring (without wave number)*/
    }
        di `"`varasstringlist'"'
        des
    
    **assign a label of a variable across waves to a single local**
    **note: in a loop, the labels of varnamer1 varnamer2, ., varnamerT are used to define a single local macro. This local macro will then be used to assign it as a label to the new variable varnamer(/s/h)
    **note: this loop could be written differently. Currently the label of the last varname available (e.g. varnamer8) is used. I you suggest a more efficient coding, let me know.
    foreach name of local vrlist { 
    forval i=1/`wavelast' {        // wavelast, needs to be adjusted for last wave of varname available
    capture confirm variable `name'`i', exact /*checks if variable exists*/
    if !_rc{
    local `name'label "``name'`i'label'" /*only use label of variable if that variable (wave) exists*/
    di "``name'label'"
    }
    }
    loc namelabellist "`namelabellist' ``name'label'"    
    }
    di "`namelabellist'"
    Then, you should assign labels to your variables one at a time, after reshaping to long
    HTML Code:
    ***reshape operation***
    **reshape 'wide' to 'long' format**
    reshape long `vrlist', i(`ID') j(wave) 
    
    **(II) apply variable labels from wide format before**
    foreach name of local vrlist{
    label variable `name' "``name'label'"
    }

    Comment

    Working...
    X