Announcement

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

  • Generate a flag if a date falls within a certain time period

    I have data with a variable that records the date of an event (DD/MM/YYY)
    I would like to create a new variable (quarter: 1-4) if the date of an event falls within a specific quarter of the year.
    Could someone help with the code that will create this new variable identifying which quarter the event took place?

    Thanks

  • #2
    Welcome to Statalist.

    First, a piece of Statalist advice. Your question is unclear, because it lacks a good description of your data. Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    With that said, the phrasing of your question leads me to think your date may be stored as a string variable. If instead it is already stored as a Stata Internal Format daily date, you can skip the next two paragraphs. Otherwise, you need to start by converting your date to a SIF daily date. And for that, more advice.

    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    OK, at this point we assume your date is now stored as a SIF daily date. Then the function quarter() documented in the output of help datetime will return the quarter corresponding to that date.

    Comment

    Working...
    X