Announcement

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

  • Reshaping multiple variables in one dataset using STATA

    Good morning,

    I would like to reshape my data from wide to long. I have multiple variables within the dataset that I would like to do this for. Each variable has a different number of iterations, that is, one variable <breed> has four iterations, the variable <caontrol> has 7 iterations and so on. Please see the dataset provided below.

    I have used the following command to create an id variable
    gen id= _n
    reshape long caontrol, i(id) j(seq)

    Where "seq" is sequence
    And that reshapes one variable.

    But then also changes the <id> variable such that it cannot be used to reshape the other variables.

    My main question is: Is there a way to reshape multiple variables with different iterations, using a single command? And if not, what would you recommend please?

    Thank you in advance.




    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2(breed1 breed3 breed4) byte(caontrol1 caontrol2 caontrol3 caontrol4 caontrol5 caontrol6 caontrol7 wildcon1 wildcon2 wildcon3)
    "PB" "MB" "MB" 1 0 1 1 1 1 1 1 0 0
    "MB" "MB" "MB" 0 1 0 0 0 0 0 0 1 1
    "PB" "PB" "PB" 1 1 1 1 1 1 1 0 1 1
    "PB" "MB" "MB" 1 1 0 0 0 0 0 0 1 1
    "PB" "MB" "MB" 1 1 1 1 1 1 1 0 1 1
    end

  • #2
    Not sure I understand question completely, but is

    Code:
    reshape long breed caontrol, i(id) j(seq)
    perhaps what you want?

    Comment


    • #3
      You may try this:

      Code:
      gen id = _n
      reshape long breed caontrol wildcon, i(id) j(seq)
      Hopefully that helps.
      Best regards,

      Marcos

      Comment


      • #4
        I don't think we can safely guess at what you need. Your example has suffixes

        1 2 4 for breed

        1 2 3 4 5 6 7 for caontrol (control?)

        1 2 3 for wildcon

        Marcos Almeida is right if and only if the 1s should be matched, the 2s should be matched, and so on.

        Comment

        Working...
        X