Announcement

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

  • reshape and value labels

    Hi everyone,

    I’m having trouble keeping value labels after a reshape from wide to long.
    I have an unbalanced panel: variables are not defined over the same years. I mention this because maybe this is what is causing my code´s failure. Below there is a small data with the same particularities than mine.

    Code:
    input id v1_1997 v1_1998    v1_1999    v1_2000    v2_1997    v2_1998    v2_1999    v2_2000    v3_1997    v3_1998    v4_1995    v4_1996    v4_1997    v4_1998    v4_1999    v4_2000    v4_2001
    1    1    2    2    3    1    3    2    2    1    2    1    3    1    2    3    2    1
    2    2    1    3    2    2    1    3    1    3    1    2    2    3    1    2    1    2
    3    1    2    1    1    3    2    1    3    2    3    3    1    2    3    1    3    3
    end
    
    label variable v1_1997 "VAR1 in 1997"
    label variable v1_1998 "VAR1 in 1998"
    label variable v1_1999 "VAR1 in 1999"
    label variable v1_2000 "VAR1 in 2000"
    
    
    label variable v2_1997 "VAR2 in 1997"
    label variable v2_1998 "VAR2 in 1998"
    label variable v2_1999 "VAR2 in 1999"
    label variable v2_2000 "VAR2 in 2000"
    
    label variable v3_1997 "VAR3 in 1997"
    label variable v3_1998 "VAR3 in 1998"
    
    label variable v4_1995 "VAR4 in 1995"
    label variable v4_1996 "VAR4 in 1996"
    label variable v4_1997 "VAR4 in 1997"
    label variable v4_1998 "VAR4 in 1998"
    label variable v4_1999 "VAR4 in 1999"
    label variable v4_2000 "VAR4 in 2000"
    label variable v4_2001 "VAR4 in 2001"
    
    
    label define v11 1 "text1" 2 "text2" 3 "text3"
    label define v12 1 "text1" 2 "text2" 3 "text3"
    label define v13 1 "text1" 2 "text2" 3 "text3"
    label define v14 1 "text1" 2 "text2" 3 "text3"
    
    label values v1_1997 v11
    label values v1_1998 v12
    label values v1_1999 v13
    label values v1_2000 v14
    
    label define v22 1 "text11" 2 "text22" 3 "text33"
    label define v23 1 "text11" 2 "text22" 3 "text33"
    label define v24 1 "text11" 2 "text22" 3 "text33"
    label define v25 1 "text11" 2 "text22" 3 "text33"
    
    label values v2_1997 v22
    label values v2_1998 v23
    label values v2_1999 v24
    label values v2_2000 v25
    
    label define v33 1 "text111" 2 "text222" 3 "text333"
    label define v34 1 "text111" 2 "text222" 3 "text333"
    
    label values v3_1997 v33
    label values v3_1998 v34
    
    label define v44 1 "text1111" 2 "text2222" 3 "text3333"
    label define v45 1 "text1111" 2 "text2222" 3 "text3333"
    label define v46 1 "text1111" 2 "text2222" 3 "text3333"
    label define v47 1 "text1111" 2 "text2222" 3 "text3333"
    label define v48 1 "text1111" 2 "text2222" 3 "text3333"
    label define v49 1 "text1111" 2 "text2222" 3 "text3333"
    label define v50 1 "text1111" 2 "text2222" 3 "text3333"
    
    label values v4_1995 v44
    label values v4_1996 v45
    label values v4_1997 v46
    label values v4_1998 v47
    label values v4_1999 v48
    label values v4_2000 v49
    label values v4_2001 v50
    
    //  Saving variable labels (I don´t need to save the labels of all variables since , for instance, v1_1998 and v1_1999 have the same values, although different variable labels) 
    
    local lv1 : variable label v1_2000 // the last one has all the categories
    local lv2 : variable label v2_2000 // the last one has all the categories
    local lv3 : variable label v3_1998 // the last one has all the categories
    local lv4 : variable label v4_2000 // the last one has all the categories
    
    //   Saving value labels in local macros before reshaping (in this case, all variables have values).
    
    local list "v1_2000 v2_2000 v3_1998 v4_2000"
    
    foreach var of local list{
         levelsof `var', local(`var'_levels)            
         foreach val of local `var'_levels {       
               local `var'vl`val':label(`var') `val'           
     }
     }
    
     reshape long v1_@ v2_@ v3_@ v4_@, i(id) j(year)
    
    renvars v1_  v2_  v3_ v4_, postdrop(1)
    
    local variablelist "v1 v2 v4"
    
    foreach variable of local variablelist{                 
         foreach value of local `variable'_2000_levels{            
             label variable `variable' "`l`variable'':``variable'_2000vl`value''"
         }
    }
    
    foreach value of local v3_1998_levels{
             label variable v3 "`lv3':`v3_1998vl`value''"
    }
    The code runs but it only keeps the value labels for v4. All others are gone.
    I appreciate your help.

    Celia P.

  • #2
    Your value labels are there, but not applied to the variables. There may be a more direct way to do what you want, but the complexity of your code implies that you need a general solution. My additions in red.

    Code:
    //   Saving value labels in local macros before reshaping (in this case, all variables have values).
    
    local list "v1_2000 v2_2000 v3_1998 v4_2000"
    
    foreach var of local list{
         levelsof `var', local(`var'_levels)            
         foreach val of local `var'_levels {      
               local `var'vl`val':label(`var') `val'          
     }
     }
     
    //  Save value labels for each var
    foreach var of local list {
        local `var'_lab: value label `var'
        }
        
    
    reshape long v1_@ v2_@ v3_@ v4_@, i(id) j(year)
    
    renvars v1_  v2_  v3_ v4_, postdrop(1)
    
    
    local variablelist "v1 v2 v4"
    foreach variable of local variablelist{                
         foreach value of local `variable'_2000_levels{            
             label variable `variable' "`l`variable'':``variable'_2000vl`value''"
             }
       lab val `variable' ``variable'_2000_lab'
    }
    
    foreach value of local v3_1998_levels{
             label variable v3 "`lv3':`v3_1998vl`value''"
    }
    lab val v3 `v3_1998_lab'
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Below code is serving well. The trick is temporarily converting a common available year into an over far ending.
      Code:
      ren (v#_1997) (v#_3000)
      reshape long v1_ v2_ v3_ v4_, i(id) j(year)
      replace year = 1997 if year == 3000

      Comment


      • #4
        Thanks a lot, Carole and Romalpa. Problem solved !

        Comment

        Working...
        X