Announcement

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

  • looping across data

    My data has multiple observations per id. There are two examination times: examdate and injedtdate. I want to create a new variable to indicate '1' where examdate=injectdate in same id, and otherwise '0' where examdate in not equal injectdate in same id.

    Can someone please suggest a looping code I can apply to achieve this. An example data is below. Thanks

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id float(examdate injectdate)
    101 19079 19075
    101 19050 19045
    101 19079 19063
    101 19050 19039
    102 19044 19019
    102 19072 19044
    102 19072 19059
    103 19001 18997
    103 19127 19085
    104 19127 19106
    104 19037 19032
    105 19114 19051
    105 19051 19024
    105 19051 18997
    107 19127 19072
    107 19127 19106
    108 19037 19023
    110 19116 19085
    110 19157 19120
    110 19157 19116
    end
    format %tdDD_Mon_CCYY examdate
    format %tdDD_Mon_CCYY injectdate

  • #2
    Code:
    gen samedate =0
    replace samedate=1 if examdate==injectdate
    So no need for a loop. With your data in this (proper) format, you dont need to instruct Stata to also regard dates 'per id'.
    Last edited by Jorrit Gosens; 05 Jun 2018, 01:01.

    Comment


    • #3
      Jorrit's helpful code can be condensed:

      Code:
       
       gen samedate = examdate==injectdate
      See https://www.stata.com/support/faqs/d...rue-and-false/ for more discussion.

      Comment


      • #4
        Thank Jorrit and Nick for responding.

        However, I actually did consider the option you suggested before posting my question. Jorrits' case will require that the matched dates for my two dates per patient's multiple observations (id) stays on same row.

        No matter how I sort these, i cant achieve such format to do it in the simple way as suggested. I still think some sort of looping could help out, but I haven't worked that out yet.

        Comment


        • #5
          Do you mean you want to have a var==1 if examdate is equal to any of the injectdate for that same id?

          Comment


          • #6
            Yes exactly

            Comment


            • #7
              Code:
              gen datesmatch=0
              levelsof id, local(ids)
              foreach id of local ids{
                  levelsof injectdate if `id'==id, local(injectdates)
                  foreach date of local injectdates{
                      replace datesmatch=1 if `date'==examdate & `id'==id
                  }
              }
              Use it on your example data to see if we understand each other and this is what you want

              Edited, was wrong way around.
              Last edited by Jorrit Gosens; 05 Jun 2018, 03:53.

              Comment


              • #8
                Thanks Jorrit, exactly what I was looking for. It worked
                Cheers

                Comment


                • #9
                  Actually, please look at the code in #7 again. I forgot a selection (if `id'==id) in the levelsof injectdate line.
                  Without it Stata will look at injectdates for all id's, even if its is inside the 'foreach id' loop.

                  Comment


                  • #10
                    If there are no missing dates, you can also do this with rangestat (from SSC):
                    Code:
                    rangestat (count) wanted=injectdate, interval(injectdate examdate examdate) by(id)
                    replace wanted = 0 if mi(wanted)
                    The code counts the number of observations where injectdate is within an interval that starts and ends with examdate (within each id by-group)

                    Comment

                    Working...
                    X