Announcement

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

  • Gen indicator variable if greater than a particular date

    Hi. I am trying to generate an indicator variable (platform=1) that is conditional on a string variable (campus) and if the date is greater than 16 February 2024. My date variable is a float, format %td (data below). I've tried
    Code:
    gen int platform=1 if campus=="MTC" & appt_date==>date("16022024","DMY")
    , but that doesn't work.

    Any help will be much appreciated


    Code:
    input str23 campus float appt_date
    "MTC"  22646
    "KAR"  22646
    "MTC"  22646
    "KAR"  22646
    "KVC"  22646
    "KVC"  22646
    "MTC"  22646
    "KAR"  22646
    "KVC"  22646
    "GMRV" 22646
    "KVC"  22646
    "KAR"  22646
    "KVC"  22646
    "KVC"  22646
    "KAR"  22646
    "MTC"  22646
    "KVC"  22646
    "KVC"  22646
    "KAR"  22646
    "KAR"  22646
    "KAR"  22646
    "MTC"  22646
    "GMRV" 22646
    "KVC"  22646
    "KAR"  22646
    "GMRV" 22646
    "MTC"  22646
    "KVC"  22646
    "KAR"  22646
    "KVC"  22646
    "KVC"  22646
    "KAR"  22646
    "GMRV" 22646
    "KVC"  22646
    "KVC"  22646
    "KAR"  22646
    "KVC"  22646
    "KAR"  22646
    "MTC"  22646

  • #2
    Try changing ==> to >= .
    Code:
    generate byte platform = campus == "MTC" & appt_date >= date("2024-02-16", "YMD")

    Comment


    • #3
      In the spirit of Joseph Coveney's helpful suggestion, know that

      Code:
      help operators
      does what it claims. I would go

      Code:
      generate byte platform = campus == "MTC" & appt_date >= mdy(2, 16, 2024)
      myself on the small grounds that giving the code a numeric argument beats giving it a string argument and then obliging Stata to convert. I am not recommending using 23422, as anyone reading the code later then has to puzzle out what that means.

      Comment


      • #4
        Originally posted by Scott Rick View Post
        . . . if the date is greater than 16 February 2024.
        In light of that, make the inequality strict.

        Following Nick's suggestion,
        Code:
        generate byte platform = campus == "MTC" & appt_date > mdy(2, 16, 2024)

        Comment


        • #5
          Thanks Nick Cox and Joseph Coveney. That worked perfectly!

          Comment

          Working...
          X