Announcement

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

  • #16
    Yes. You have to save all the labels from the original set in a temporary do-file and then run that after the reshape. Here's the entire code sequence from beginning to end:

    Code:
    ds ce_* cg_*
    local stublist `r(varlist)'
    local stublist: subinstr local stublist "ce_" "@", all
    local stublist: subinstr local stublist "cg_" "@", all
    local stublist: list uniq stublist
    
    foreach s of local stublist {
        local t = strtoname(substr(`"`s'"', 2, .))
        local stublist: subinstr local stublist `"`s'"' `"@`t'"'
    }
    
    tempfile labeler
    label save _all using `labeler'
    
    reshape long `stublist', i(pidp) j(_j) string
    
    gen int date = cond(_j == "ce_", tm(2021m1), tm(2020m9))
    format date %tmMonth_CCYY
    
    drop _j
    
    run `labeler'

    Comment


    • #17
      Originally posted by Ana Vasconcelos View Post
      When I use the commands above to reshape the data I miss all the information of the labels of the variables. It only appear the numbers without any other identification (label). Is it possible to keep the label information and reshape the data at the same time? Thank you very mcuh on advance.
      That happened in old versions of Stata (before version 12). It usually happened when reshape-ing to wide format. Anyway, I cannot reproduce the problem running the example in #9. I am using Stata 16.1 now, and I get

      Code:
      (output omitted)
      
      . reshape long `stublist', i(pidp) j(_j) string
      (note: j = ce_ cg_)
      
      Data                               wide   ->   long
      -----------------------------------------------------------------------------
      Number of obs.                      100   ->     200
      Number of variables                  15   ->      12
      j variable (2 values)                     ->   _j
      xij variables:
                              ce_semp cg_semp   ->   semp
                ce_parent5plus cg_parent5plus   ->   parent5plus
                          ce_couple cg_couple   ->   couple
                          ce_sex_cv cg_sex_cv   ->   sex_cv
      -----------------------------------------------------------------------------
      
      . describe
      
      Contains data
        obs:           200                          
       vars:            12                          
      -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    storage   display    value
      variable name   type    format     label      variable label
      -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      pidp            long    %12.0g                
      psu             long    %12.0g     psu        
      strata          int     %8.0g      strata     
      i_hidp          long    %12.0g                
      j_hidp          long    %12.0g                
      k_hidp          long    %12.0g                
      semp            byte    %23.0g     cg_semp    
      parent5plus     byte    %8.0g      cg_parent5plus
                                                    
      couple          byte    %8.0g      cg_couple
                                                    
      sex_cv          byte    %8.0g      cg_sex_cv
                                                    
      i_qfhigh        byte    %68.0g     i_qfhigh   
      date            int     %tm..                 
      -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Sorted by: pidp
           Note: Dataset has changed since last saved.
      
      . label list
      cg_semp:
                -8 inapplicable
                 2 Yes, self-employed only
                 4 No
      ce_semp:
                -8 inapplicable
                 1 Yes, employed only
                 2 Yes, self-employed only
                 3 Both employed and self-employed
                 4 No
      cg_parent5plus:
                 1 Yes
                 2 No
      ce_parent5plus:
                 1 Yes
                 2 No
      ce_couple:
                 1 Yes
                 2 No
      cg_couple:
                 1 Yes
                 2 No
      ce_sex_cv:
                 1 Male
                 2 Female
      cg_sex_cv:
                 1 Male
                 2 Female
      i_qfhigh:
                -8 inapplicable
                15 Other school (inc. school leaving exam certificate or matriculation)
      with all value labels neatly defined and attached.


      I think Clyde's code in this (outdated) FAQ).

      Comment


      • #18
        That's interesting. Before posting #16, I ran the earlier code with O.P.'s example data, and I did reproduce her problem. I'm running version 17 MP4 on Windows.

        Specifically, after reshaping, the reshaped variables had labels attached, but the labels were undefined.

        Comment


        • #19
          Originally posted by Clyde Schechter View Post
          That's interesting. Before posting #16, I ran the earlier code with O.P.'s example data, and I did reproduce her problem. I'm running version 17 MP4 on Windows.

          Specifically, after reshaping, the reshaped variables had labels attached, but the labels were undefined.
          Interesting indeed. I only have access to Stata 17 in a remote environment that will not allow me to copy and paste the dataex examples so I cannot quickly reproduce this behavior. However, keeping the label attached and losing the definitions sounds like a bug to me; especially given that Stata 16.1 handles the situation perfectly well.

          Comment

          Working...
          X