Announcement

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

  • Creating a variable that gives the day from baseline based on a date variable

    Hello,
    Im kindly requesting on guidance on the issue below.
    I have data in long format ie IDs of patients with their dates of LP. Each observation indicates a different date of LP and the variable DateLp shows the difference.
    I want to be able to generate a variable DayOfLP which basically counts the number of days from the first date of LP. e.g if DateLP=12SEP2021 and DayOfLP is 1, then DateLP of 14SEP2021 would result to DayOfLP being 3.
    An example dataset is below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 PatId int DateLP
    "64-001" 19475
    "64-001" 19478
    "64-002" 19478
    "64-004" 19492
    "64-004" 19495
    "64-004" 19499
    "64-004" 19507
    "64-006" 19505
    "64-006" 19507
    "64-006" 19512
    end
    format %td DateLP
    I have tried the code
    Code:
    quiet by PatId: gen DayOfLP=(_N-_n) if DateLP!=.
    but this generates numbers that are like tags for the duplicates (starting from 0 onwards)

    I'd be grateful if some guidance is given.

    Thanks.

  • #2
    If I understand you correctly, and if no ID appears more than twice, then I think this will give what you want:
    Code:
    quiet bys PatId (DateLP): gen DayOfLP=DateLP - DateLP[_n-1] if PatId==PatId[_n-1]

    Comment


    • #3
      Code:
      by PatId (DateLP), sort: gen DayOfLP = DateLP - DateLP[1] + 1
      Added: Crossed with #2. This code will produce correct results no matter how many times the same PatID appears.

      Comment


      • #4
        Note that Clyde Schechter and I read your original post somewhat differently as he adds "1" to each difference and I don't - presumably you can choose which, if either, is actually what you want

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Code:
          by PatId (DateLP), sort: gen DayOfLP = DateLP - DateLP[1] + 1
          Added: Crossed with #2. This code will produce correct results no matter how many times the same PatID appears.
          Thanks a lot Clyde.
          This was very helpful.

          Comment


          • #6
            Originally posted by Rich Goldstein View Post
            Note that Clyde Schechter and I read your original post somewhat differently as he adds "1" to each difference and I don't - presumably you can choose which, if either, is actually what you want
            Thank you Rich. I think I can now understand the difference and its application as I have tried your code and Clyde's.

            Thanks.

            Comment

            Working...
            X