stata

retaining the variable label in reshape


I would like to retain the variable labels after reshaping the dataset from long to wide. I have a problem in inputting the dataset (I keyed them in Excel then imported).

clear
set obs 7

input id    a1  a2  a3
"s001"  "John"  23  "Primary"
"s002"  "Mary"  32  "Secondary"
"s002"  "Anna"  23  "Tertiary"
"s003"  "Joseph"    34  "Secondary"
"s003"  "Oganyo"    23  "Primary"
"s004"  "Manyoya"   34  "Tertiary"
"s005"  "Makbuti"   45  "Primary"
end


*======= Label the variables

label var a1 "partners name"
label var a2 "partners age"
label var a3 "partners education"

foreach variable of varlist a*{
    
    local varlabel : variable label `variable'
    di "`varlabel'"

bys id: gen index = _n

renvars a* , postfix(_)


reshape wide a*, i(id) j(index)

label var `variable'* "`varlabel'"

}



Solution

  • The code here is very confused and just won't work well without major surgery.

    At the outset your input statement fails to declare string variables when needed.

    The set obs 7 is compatible with an end to the input.

    A big problem is that you are looping over a bunch of commands including reshape, but there is just one reshape to carry out.

    Your code for saving variable labels needs to save then separately.

    renvars is from the Stata Journal (2005), and doesn't seem needed here any way. As from Stata 12 (2011!) it should still work but is essentially superseded by extensions to rename.

    Here's my best guess at the example you should have given and the code you need.

    clear
    
    input str4 id   str7 a1  a2  str9 a3
    "s001"  "John"  23  "Primary"
    "s002"  "Mary"  32  "Secondary"
    "s002"  "Anna"  23  "Tertiary"
    "s003"  "Joseph"    34  "Secondary"
    "s003"  "Oganyo"    23  "Primary"
    "s004"  "Manyoya"   34  "Tertiary"
    "s005"  "Makbuti"   45  "Primary"
    end
    
    label var a1 "partner's name"
    label var a2 "partner's age"
    label var a3 "partner's education"
    
    local j = 0 
    foreach variable of varlist a* { 
        local ++j 
        local varlabel`j' : variable label `variable'
        di "`varlabel`j''"
    } 
    
    bys id: gen index = _n 
    
    reshape wide a*, i(id) j(index)
    
    forval j = 1/3 { 
        foreach v of var a`j'* { 
            label var `v' "`varlabel`j''"
        }
    }
    
    list 
    
    describe 
    

    Here's the output of the last two commands.

    . list 
    
         +------------------------------------------------------------+
         |   id       a11   a21         a31      a12   a22        a32 |
         |------------------------------------------------------------|
      1. | s001      John    23     Primary              .            |
      2. | s002      Mary    32   Secondary     Anna    23   Tertiary |
      3. | s003    Joseph    34   Secondary   Oganyo    23    Primary |
      4. | s004   Manyoya    34    Tertiary              .            |
      5. | s005   Makbuti    45     Primary              .            |
         +------------------------------------------------------------+
    
    . 
    . describe 
    
    Contains data
     Observations:             5                  
        Variables:             7                  
    ------------------------------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------------------------
    id              str4    %9s                   
    a11             str7    %9s                   partner's name
    a21             float   %9.0g                 partner's age
    a31             str9    %9s                   partner's education
    a12             str7    %9s                   partner's name
    a22             float   %9.0g                 partner's age
    a32             str9    %9s                   partner's education
    ------------------------------------------------------------------------------------------------------------------
    Sorted by: id
        
    

    Note: reshape wide makes most things more difficult in Stata.

    Note: Please use dataex to create reproducible data examples in Stata.