Announcement

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

  • Create consecutive days with unequal number of obs per day

    Dear Stata folks,

    I have a dataset with patient_ids, days and a measurement(outcome). Patients may have more than one measurements per day. The number is unequal between patients and days. Let's say that it is the BP measurements.
    The days are in %td format but not consecutive, since some patients may non have measured their BP a day and then measure it the next days. Patients are advised to do these measurements for 28 days but some did it for less and few others did so for more.
    I want to create a consecutive day variable so as to have 0 measurements when they have not measured their BP, instead of having only the measurements for the days that they did measure.
    Any help please? Whatever I saw in different posts doesn't work for my case.
    Thank you in advance.

  • #2
    It really isn't obvious that filling out your data with extra observations saying essentially nothing is a good idea. Usually

    Code:
    help tsset
    
    help tsfill
    is what to read next, but multiple measurements on some days imply that won't work easily for you.

    Whatever I saw in different posts doesn't work for my case.
    That really doesn't help us.

    I'd back up and tell us why you think you need this.

    Comment


    • #3
      Thank you very much Nick. I want to add these zero points because when I calculate the min number of measurements, it starts from 1 now, whereas the minimum is 0.
      I have tried asset and refill but I'll have again a look.
      This post seemed to me the most relevant https://www.statalist.org/forums/for...tes-vary-by-id and I tried this approach but merging goes wrong.



      Comment


      • #4
        Not completely convinced but use a data example to show us how you are holding identifier and date-time information.

        Comment


        • #5
          I don't care for the outcome now, it's only the measurement that I need. I don't have 0 measurements for the days with no measurements. Many thanks in advance!
          patient_id day measurement(#of measurement per day) outcome
          1 28May2019 1 10
          1 28May2019 2 11
          1 28May2019 3 9
          1 30May2019 1 8
          1 1June2019 1 12
          1 1June2019 2 11
          1 it continues like this for K days ...
          2 29May2019 1 10
          2 29May2019 2 11
          2 31May2019 1 12
          2 31May2019 2 ....
          2 31May2019 3
          2 1june2019 1
          2 1June2019 2
          2 1june2019 3
          2 2june2019 1
          2 2june2019 2
          2 2june2019 3
          2 2june2019 4

          Comment


          • #6
            You missed a key point explicit at https://www.statalist.org/forums/help#stata 12.2 that dataex really is needed to show date variables in civilised form. This code segment uses an example with similar flavour to show how to create a dataset with one observation per day and then merge with the original.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(patient_id day)
            1 21552
            1 21556
            1 21557
            1 21557
            1 21558
            1 21559
            2 21550
            2 21558
            2 21559
            end
            format %td day
            save sandbox
            collapse (min) first=day (max) last=day , by(patient_id)
            gen length = last - first + 1
            expand length
            bysort patient_id : gen day = first + _n - 1
            keep patient_id day
            merge 1:m patient_id day using sandbox

            Comment


            • #7
              It worked! Many thanks Nick! I had a mistake in the code but your example was very useful and clear!

              Comment

              Working...
              X