Announcement

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

  • Subtracting dates to get day intervals

    Hi all, thanks again for taking the time to answer my beginner questions

    I am trying to determine the difference between hospital discharge and the date of tests for a large dataset.

    date of discharge variable = "date" and is in string format (str10)
    date of test variable = "stress_date" and is in string format (str10)
    both are in MDY format (e.g. 10/28/2021)

    I have tried the below which generated 1189 missing variables
    . generate time_to_stress = date("stress_date","MDY") - date("date","MDY")

    but then tabulating the new variable yields no observations
    . tab time_to_stress

    I cant seem to wrap my head around what Im doing wrong. When I try the codebook function on "stress_date" I'm not given examples, so maybe the problem is there. I appreciate your time and advice. Thanks


    Log:

    . generate time_to_stress = date("stress_date","MDY") - date("date","MDY")
    (1,189 missing values generated)

    . tab time_to_stress
    no observations

    . codebook date

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    date encounter date
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Type: String (str10)

    Unique values: 197 Missing "": 0/1,189

    Examples: "11/14/2021"
    "12/21/2021"
    "7/29/2021"
    "8/4/2021"

    . codebook stress_date

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    stress_date Stress Test Date
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Type: String (str10)

    Unique values: 31 Missing "": 1,157/1,189

    Examples: ""
    ""
    ""
    ""

    . tab stress_date

    Stress Test |
    Date | Freq. Percent Cum.
    ------------+-----------------------------------
    03/02/2022 | 1 3.13 3.13
    03/08/2022 | 1 3.13 6.25
    1/11/22 | 1 3.13 9.38
    10/1/2021 | 1 3.13 12.50
    10/1/21 | 1 3.13 15.63
    10/13/21 | 1 3.13 18.75
    10/19/21 | 1 3.13 21.88
    10/20/2021 | 2 6.25 28.13
    10/22/2021 | 1 3.13 31.25
    10/22/21 | 1 3.13 34.38
    10/25/2021 | 1 3.13 37.50
    10/27/2021 | 1 3.13 40.63
    10/27/21 | 1 3.13 43.75
    10/29/2021 | 1 3.13 46.88
    11/17/21 | 1 3.13 50.00
    11/2/21 | 1 3.13 53.13
    12/2/21 | 1 3.13 56.25
    2/12/2022 | 1 3.13 59.38
    2/14/22 | 1 3.13 62.50
    3/2/22 | 1 3.13 65.63
    4/1/22 | 1 3.13 68.75
    7/30/21 | 1 3.13 71.88
    8/2021 | 1 3.13 75.00
    8/25/2021 | 1 3.13 78.13
    8/27/21 | 1 3.13 81.25
    8/30/21 | 1 3.13 84.38
    9/15/21 | 1 3.13 87.50
    9/27/21 | 1 3.13 90.63
    9/28/2021 | 1 3.13 93.75
    9/28/21 | 1 3.13 96.88
    9/8/21 | 1 3.13 100.00
    ------------+-----------------------------------
    Total | 32 100.00

  • #2
    You need e.g.

    Code:
    date(stress_date,"MDY")
    not

    Code:
    date("stress_date","MDY"
    Giving the string "stress_date" as the first argument to date() is legal but not what you you want. The literal string "stress_date" doesn't contain any date information. You need to pass the variable name.

    To see this if it's a little subtle still: You could try some arbitrary text say "cats and dogs" that contains no date information and you will get missing too.

    Code:
    . di date("cats and dogs", "MDY")
    .
    In short, Stata is telling you that no date is defined by what you supplied, but your code was not in error about syntax.

    Comment


    • #3
      thank you very much

      Comment

      Working...
      X