Announcement

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

  • replacing VALUE labels after reshape

    Hello,
    Can anyone suggest how to automate the saving of value label definitions and reapplication of them to variables after reshaping from wide to long (using Stata 19)?

    I successfully followed instructions in this old Stata FAQ (https://www.stata.com/support/faqs/d...after-reshape/) to replace my variable labels. But I can't get the FAQ's code for saving value labels to work even when I use the FAQ's example dataset and example code, nor does the FAQ explain how to re-apply value labels if you do successfully save them before the reshape. (StataCorp has put a note at the top of this FAQ that says the solution doesn't apply versions after Stata 11, so I'm assuming this is why I can't get the example code for value labels to work.) I have found hundreds of StataList suggestions for replacing variable labels after reshaping, but I haven't been able to find posts about replacing value labels.

    Many thanks in advance for your help,
    Melissa

  • #2
    Here's how I would do this in the example given at the link you show:

    Code:
    //  SAVE THE LABELS IN A TEMPORARY FILE
    tempfile labeler
    label save _all using `labeler'
    
    // RESHAPE THE DATA
    reshape wide inc answer, i(id) j(year)
    
    //  BRING THE SAVED LABELS INTO THE NEW DATA SET
    run `labeler'
    
    //  APPLY THE LABELS TO THE VARIABLES
    label values answer* answer
    Important: because this code uses a temporary file, it must be run from beginning to end without interruption. It will not work properly if you try to do it line by line or in chunks.

    Note also that the procedure is the same whether you are -reshape-ing from wide to long or long to wide. However, if you are going from wide to long, it is crucial that the variables that end up together in a single variable all have the same value label at the start. If that is not true, the code will fail, and for good reason, because it would be impossible to know which of the labels applied to the answer* variables should be used for the combined single answer variable. In fact, it is likely that none of them would be appropriate in such a circumstance.

    Comment


    • #3
      This thread seems relevant.

      Comment


      • #4
        Playing around with this, I believe the labels are lost when reshape_favor is set to speed. , which I believe is the default setting in Stata 19 (to which I don't have access yet). I'm not sure whether losing labels is intended behavior with the speed setting; it's not documented but it probably speeds things up (at least a tiny bit).

        Anyway, type
        Code:
        set reshape_flavor memory
        to preserve labels (but slow down reshape).

        Comment


        • #5
          Clyde and Daniel,
          Thank you for all these suggestions. I tried the easiest solution first, which was to add the single command to change the reshape settings.
          Code:
          set reshape_favor memory
          This worked! When I reshaped from wide to long, my value labels were preserved. I really appreciate your help.

          Comment


          • #6
            reshape with favor(speed) should keep value labels of the xij variables. We hope to fix this very soon.

            Comment

            Working...
            X