Announcement

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

  • Drop observations for date A if > 90 days before or after date B

    Please help! How can I write a command in Stata so that..... for each individual ID_number, I uniformly drop the "HbA1c_date" (the date when a lab test was done / study participants) if the "HbA1c_date" was obtained > 90 days AFTER or BEFORE the "ADQresponse_date" (the date when a child or parent in the study filled out a questionnaire called the ADQ)? Thank you in advance for your sage advice.


    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 ID_number long ADQresponsedate float(meanADQscore__child meanADQscore_parent HbA1c_date HbA1c_mmolmol)
    "0001" 18032 . 5.0 18057 57.3
    "0002" 18058 . 4.7 18063 47.5
    "0003" 18024 . 4.3 18042 55.2
    "0004" 18065 . 4.5 18081 73.7
    "0005" 18036 . 4.8 . 18064 63.9
    "0005" 17951 5.0 5.0 17952 51.9
    "0006" 18026 5.5 4.7 18029 59.5
    "0007" 18025 5.9 4.3 18031 51.9
    "0008" 18044 5.7 . 18030 54.1
    "0009" 18087 . 4.7 18087 53.0
    "0010" 18084 4.7 4.6 18059 61.7

  • #2
    The body of your post asks a different question from the title. The question in the post is how to drop the date when it falls outside of a range. That is not possible. In Stata you cannot -drop- a variable in some observations and not others. You could replace it with a missing value in some observations but not others, but that is probably not a useful thing to do in most situations.

    The question asked in your title has a simple solution. It is straightforward to eliminate entire observations based on a condition like that:
    Code:
    keep if inrange(ADQresponsedate - HbA1c_date, -90, 90)

    Comment


    • #3
      Dear Clyde, your comments were spot on. Thank you so much for giving me a simple solution to my fairly basic question. The code you provided worked like a charm. I was able to determine exactly how many participants would be dropped from the total sample if the HbA1c test date was > 90 versus > 180 days after or before the questionnaire's completion date.

      Comment

      Working...
      X