Jun 03

Format dates in Stata

Tag: stataadmin @ 1:10 pm

I tend to pass dates to Stata in the form “January 1, 2005″ because it gives me flexibility to get date information with the daily function. Transforming these dates into Stata’s daily date format type:

gen date = daily(original_date, "mdy");
format date %td

Now the date is of the form “01Jan2005.” Then, we can get years, days and months with the year, day and month functions. Stata’s full list of date functions is found here.

We can calculate the number of days, years or months between two dates using the reformatted dates and simple subtraction.  Stata gives each date an integer with 01Jan1960 =0.  So,

04Feb1960 – 05Jan1960 = 30

We can then divide this number by say 365 to get the number of years between two dates (controlling for the possibility of a negative value).

Leave a Reply