Announcement

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

  • Retain variable labels after collapsing

    Hi all,

    I have generated about 40 new variables with labels for each. At the end of this process I do a -collapse all-, without realizing it would eliminate all of those labels. I could just copy and paste each of the label commands one by one to come after the collapse command, but was hoping there might be a more efficient workaround. Thank you.

  • #2
    I think you can find the answer here: https://www.stata.com/support/faqs/d...with-collapse/

    Comment


    • #3
      Thanks, David. For some reason it didn't work when I first tried it (before I posted), but I just gave it another shot and it worked fine.

      Comment


      • #4
        But the labels can be somewhat retained in the process, (unless the variable itself was collapsed to an overall value, hence keeping the labels would be meaningless).

        Example below:

        Code:
        . use http://www.stata-press.com/data/r15/college
        
        . list
        
             +----------------------------+
             | gpa   hour   year   number |
             |----------------------------|
          1. | 3.2     30      1        3 |
          2. | 3.5     34      1        2 |
          3. | 2.8     28      1        9 |
          4. | 2.1     30      1        4 |
          5. | 3.8     29      2        3 |
             |----------------------------|
          6. | 2.5     30      2        4 |
          7. | 2.9     35      2        5 |
          8. | 3.7     30      3        4 |
          9. | 2.2     35      3        2 |
         10. | 3.3     33      3        3 |
             |----------------------------|
         11. | 3.4     32      4        5 |
         12. | 2.9     31      4        2 |
             +----------------------------+
        
        . label define myyr 1 "First" 2 "Second" 3 "Third" 4 "Fourth"
        
        . label values year myyr
        
        . list
        
             +------------------------------+
             | gpa   hour     year   number |
             |------------------------------|
          1. | 3.2     30    First        3 |
          2. | 3.5     34    First        2 |
          3. | 2.8     28    First        9 |
          4. | 2.1     30    First        4 |
          5. | 3.8     29   Second        3 |
             |------------------------------|
          6. | 2.5     30   Second        4 |
          7. | 2.9     35   Second        5 |
          8. | 3.7     30    Third        4 |
          9. | 2.2     35    Third        2 |
         10. | 3.3     33    Third        3 |
             |------------------------------|
         11. | 3.4     32   Fourth        5 |
         12. | 2.9     31   Fourth        2 |
             +------------------------------+
        
        . collapse gpa hour [fw=number], by(year)
        
        . list
        
             +------------------------------+
             |   year        gpa       hour |
             |------------------------------|
          1. |  First   2.788889   29.44444 |
          2. | Second   2.991667   31.83333 |
          3. |  Third   3.233333   32.11111 |
          4. | Fourth   3.257143   31.71428 |
             +------------------------------+
        If we check at the upper right window, the variable keeps different labels (actually, they are just "notes" to explain the code). But if we type - codebook - they are exactly those we created.
        Last edited by Marcos Almeida; 13 Jun 2018, 15:17.
        Best regards,

        Marcos

        Comment

        Working...
        X