Announcement

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

  • Date Arithmetic

    If you need to add (or subtract) a period measured in days to a date, it is straightforward to do so. Just remember to format all new date variables as dates with %td:

    Code:
    gen a = td(19oct2005) - 28
    format %td a
    My question is that, in all cases, I don't need a variable. I only need to know the outcome. Even if I generate a new variable, it's useless for me and I will drop it. Thus, I am thinking that just generate a scalar. But then I realize that format %td a will not work for a scalar.

  • #2
    you say you "only need to know the outcome" - in that case, -display- will do it for you;
    Code:
    di td(19oct2005) - 28
    if you want it in a local; just do:
    Code:
    local junk= td(19oct2005) - 28
    just replace "junk" with whatever you want to call the local; you can check the result via:
    Code:
    mac li _junk
    Last edited by Rich Goldstein; 10 Mar 2020, 07:47.

    Comment


    • #3
      As a small variant on @Rich Goldstein's answer I find mdy() convenient too.

      Comment


      • #4
        I need to do this code:
        Code:
        keep if (date<td(19oct2005)) & (date>=td(21sep2005))
        But I need to show how to get 21sep2005. So I did:
        Code:
        generate a = td(19oct2005) - 28
        format %td a
        drop a

        Comment


        • #5
          So, did you want

          Code:
          keep if inrange(date, mdy(10, 18, 2005) - 27, mdy(10, 18, 2005)
          or

          Code:
          keep if inrange(mdy(10, 18, 2005) - date, 0, 27)

          Comment

          Working...
          X