Announcement

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

  • Comparing Dates, Create new variable

    Hi there,

    Essentially, what I'm trying to do is compare two dates: 'Treatment Date 1' and 'Treatment Date 2'.
    I would like to create a new variable if Date 1>Date 2.

    I tried the very basic code below. However, when there is a missing value for Date 1 it is creating the new variable as if Date 1>Date 2.

    gen byte txendrecent=1 if Date1>Date 2


    Any suggestions greatly appreciated.

    Jonathan Edwin
    Epidemiologist, Population and Public Health Dept.
    Government of Northwest Territories

  • #2
    To note, I tried re-formatting the dates, but the same issue with use of missing dates.

    Comment


    • #3
      The output of help missing tells us
      Code:
          Stata has 27 numeric missing values:
      
              ., the default, which is called the "system missing value" or sysmiss
      
          and
      
              .a, .b, .c, ..., .z, which are called the "extended missing values".
      
          Numeric missing values are represented by large positive values.  The ordering is
      
                          all nonmissing numbers < . < .a < .b < ... < .z
      
          Thus, the expression age > 60 is true if variable age is greater than 60 or
          missing.
      
          To exclude missing values, ask whether the value is less than ".".  For instance,
      
              . list if age > 60 & age < .
      
          To specify missing values, ask whether the value is greater than or equal to ".".
          For instance,
      
              . list if age >=.
      Note however that in your code, txendrecent will be missing when it is not 1. A better formulation would be
      Code:
      generate byte txendrecent=0
      replace txendrecent=1 if Date1>Date2 & Date1<.
      which can be shortened to
      Code:
      generate byte txendrecent = (Date1>Date2) & (Date1<.)
      These both will generate txendrecent as 0 or 1, consistent with Stata treating 0 as false and all other values, including missing values, as true.
      Last edited by William Lisowski; 11 Apr 2018, 18:33.

      Comment


      • #4
        That nuance about missing values is all coming back to me now. Thanks William!

        Best,
        Jonathan

        Comment

        Working...
        X