Announcement

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

  • Data manipulation

    I would like to combine these variables (cyc1, cyc2 and cyc3) and generate a new variable (cyc) which is a combination of cyc1-3. i.e, how do I get from table 1 to table 2. Grateful for some help.

    Table 1
    ID cyc 1 cyc2 cyc3 abs
    2 1 2 . 2
    2 3 4 . 2
    2 5 6 . 2
    2 7 8 . 2
    3 1 2 3 3
    3 4 5 6 3
    Table 2
    ID cyc abs
    2 1 2
    2 2 2
    2 3 2
    2 4 2
    2 5 2
    2 6 2
    2 7 2
    2 8 2
    3 1 3
    3 2 3
    3 3 3
    3 4 3
    3 5 3
    3 6 3

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id cyc1 cyc2 cyc3 abs)
    2 1 2 . 2
    2 3 4 . 2
    2 5 6 . 2
    2 7 8 . 2
    3 1 2 3 3
    3 4 5 6 3
    end
    
    gen `c(obs_t)' obs_no = _n
    reshape long cyc, i(id obs_no)
    drop if missing(cyc)
    drop obs_no _j
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Also, it is the norm in this community that we use our real given and surnames as our userid, to promote collegiality and professionalism. If Grace Baby is your real name, then please accept my apologies for the reminder and read no farther. But if it is not, please click on Contact Us in the lower right corner of the page and ask the system administrator to change your userid. Thank you.

    Comment


    • #3
      Clyde Schechter thanks for the advice on inputting data into statalist - I will do it right next time. Grace is my real name but I misspelt my surname - Babey. I will amend it.

      Many thanks for the code, it worked perfectly.

      Comment


      • #4
        I don't think you can edit your name unilaterally. Use the CONTACT US button at low right to get in touch with the Statalist administrators.
        Last edited by Nick Cox; 30 Jul 2023, 11:51.

        Comment


        • #5
          Nick Cox Doing that now, thanks

          Comment

          Working...
          X