Real-life data can come in a variety of formats.  In this post, I would like to show how to convert a numeric date to Stata date.

The problem

Let’s use an example.  Say that we have date variable in the following format and we want to convert it to Stata format.

 | date     |
 | 20170520 |
 | 20170521 |
 | 20170522 |
 | 20170524 |
 | 20170524 |

The Solution

There are two steps involved to convert the numeric variable to Stata format. These are:

tostring date, gen(datevar)
gen date2 = date(datevar, "YMD")
format date2 %td


The first line of code converts the numeric variable to string variable. This is necessary as the date function can work only on string variables. The second line of code uses the date function to generate a new variable date2 from the existing variable datevarThe “YMD” specifies how the datevar has the sorting of year, month, and day. The last line of code just formats the new variable so that human can easily read it.