Announcement

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

  • keep variable labels when using reshape command

    Hi everyone!

    I am wondering if I can somehow keep the variable labels when I use the reshape command. My dataset looks currently like this (is in the long format) and I want to reshape it to the wide format using this command: reshape wide b02 b03, i(ID) j(livestock).
    ID livestock b02 b03
    2 Cow 1 234
    2 Duck 7 234
    1 Cow 4 134
    5 Chicken 4 984
    5 Duck 4 34
    The variables b02 and b03 are labeled and I want that after the reshaping command the variable b021, b022,.. have the labels of the variable b02 and the variables b031,b032 have the labels of the variable b03. Does anyone has a great idea on how to do this? Because otherwise it will be super timeconsuming to relabel all those variables again as my dataset has over 1000 variables.

    Hope someone can help!

    Greetings,
    Kathrin







  • #2
    Hello Katrin,

    Welcome to the Stata Forum,

    Now I cannot test it out, by you may wish to fiddle with @.

    Hopefully that helps,

    Best,

    Marcos
    Best regards,

    Marcos

    Comment


    • #3
      Note that you can apply a label to a set of variables in one go

      Code:
      label define b02label 1 "xxx" 7 "yyy" 4 "zzz"
      label define b03label 234 "xxx" 134 "yyy" 984 "zzz" 34 "aaa"
      label values b021-b0299 b02label
      label values b031-b0399 b03label

      Comment


      • #4
        My answer is different. Why do you think that reshape wide is a good idea here? The dataset looks in good shape for almost all Stata analyses. Can you give examples of calculations that you think require a wide layout?

        That said, this seems soluble.

        Code:
        clear
        input ID str7 livestock    b02    b03
        2    Cow    1    234
        2    Duck    7    234
        1    Cow    4    134
        5    Chicken    4    984
        5    Duck    4    34
        end
        
        label var b02 "something"
        label var b03 "else"
        
        foreach v of var b02 b03 {
            local lbl`v' "`: var label `v''"
        }
        
        sort ID, stable
        by ID : gen which = _n
        reshape wide livestock b02 b03, i(ID) j(which)
        
        foreach s in b02 b03 {
            foreach v of var `s'* {
                label var `v' "`lbl`s''"
            }
        }
        
        describe
        
        Contains data
          obs:             3                          
         vars:             7                          
         size:           102                          
        ------------------------------------------------------------------------------------------
                      storage   display    value
        variable name   type    format     label      variable label
        ------------------------------------------------------------------------------------------
        ID              float   %9.0g                
        livestock1      str7    %9s                   1 livestock
        b021            float   %9.0g                 something
        b031            float   %9.0g                 else
        livestock2      str7    %9s                   2 livestock
        b022            float   %9.0g                 something
        b032            float   %9.0g                 else
        ------------------------------------------------------------------------------------------
        Sorted by: ID

        Comment


        • #5
          Jorrit: I am presuming that "variable labels" was intended by Kathrin, not value labels.

          Comment


          • #6
            Ah. That is what she says, so you might be right.

            Comment


            • #7
              OK; but you can also collapse, contract, whatever. Those are ways of reducing to one observation for each identifier.

              Your dataset is probably just made up, but a silly example should do:

              Code:
              . clear
              
              . input ID str7 livestock    b02    b03
              
                          ID  livestock        b02        b03
                1. 2    Cow    1    234
                2. 2    Duck    7    234
                3. 1    Cow    4    134
                4. 5    Chicken    4    984
                5. 5    Duck    4    34
                6. end
              
              . 
              . egen distinct = tag(ID livestock) 
              
              . 
              . collapse (sum) distinct b02 b03, by(ID) 
              
              . 
              . list 
              
                   +----------------------------+
                   | ID   distinct   b02    b03 |
                   |----------------------------|
                1. |  1          1     4    134 |
                2. |  2          2     8    468 |
                3. |  5          2     8   1018 |
                   +----------------------------+

              Comment


              • #8
                NB for anyone else stumbling across this thread while trying to keep their variable labels when using reshape, I recommend instead using the user-created command sreshape (https://journals.sagepub.com/doi/pdf...867X1601600305) which you can find in Stata by typing
                Code:
                 search sreshape

                Comment


                • #9
                  Use greshape instead of reshape.

                  Code:
                  ssc install gtools

                  Comment

                  Working...
                  X