Announcement

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

  • Multiple observations for same ID grouping

    Hi Statalist

    For some individuals in my dataset I have multiple observations for a date of visit (visitdate). These are listed as this in my dataset:

    Date of visit
    ID: 1 15.05.10
    ID: 1 19.07.10
    ID: 1 23.09.10
    ID: 2 15.05.08
    ID: 3 03.05.10
    ID: 3 01.06.10

    I would like these to be encoded as visit1, visit2... so there is just one ID row for each individual.
    Is this possible? There are a lot more variables for each ID (numerics and strings) but I only want to transfer the visit dates.

  • #2
    The tableau you show is not a possible organization of any Stata data set. I have "done some surgery" on it to create a Stata data set the, I imagine, resembles what you have. From there, you can get what you want with:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id float date
    1 18397
    1 18462
    1 18528
    2 17667
    3 18385
    3 18414
    end
    format %td date
    
    by id (date), sort: gen _j = _n
    reshape wide date, i(id) j(_j)
    That said, there is a good chance you will regret doing this. For most matters involving data management or analysis in Stata, the long layout you started with is much more convenient than the wide layout you are converting to. In fact, many things cannot be done at all with wide layout. So unless you know specifically that you will be doing some things that require wide layout, you will be better off not doing this.

    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 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.

    Comment

    Working...
    X