Announcement

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

  • If function with Monthly dates

    Hi,

    I have a panel data set with dates formatted as "YM" in stata. The time period is monthly from year 2003 onwards (2003m1,2003m2.....). I want to keep or extract the observations for the entire year 2007 only (starting from 2007m1 till 2007m12). When I give code as:

    Keep if Time_Period==2007m1, 2007m2....

    It says invalid syntax or stata does-not recognize 2007m1....

    Although my Time_period is stata formatted dates(not string), but I don't know how to refer to a particular date (monhtly in my case) with if function.

    Can Somebody help please?

    Regards

  • #2
    To correct your syntax
    Code:
    Keep if Time_Period==2007m1, 2007m2....
    you can use the tm() function to conveniently get a Stata monthly date constant, and instead of listing 12 monthly values, you can use the inrange() function.
    Code:
    keep if inrange(Time_Period,tm(2007m1),tm(2007m12))
    The other way is to use the dofm() function to convert Time_Period to a daily date (the first of the month) and then use the year() function to extract the year from the daily date.
    Code:
    keep if year(dofm(Time_Period))==2013
    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.

    Comment

    Working...
    X