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.
Let’s use an example. Say that we have date variable in the following format and we want to convert it to Stata format.
+----------+ | datevar | |----------| | 20170520 | | 20170521 | | 20170522 | | 20170524 | | 20170524 | +----------+
There are two steps involved to convert numeric variable to Stata format. These are:
tostring date, replace 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 datevar . The “YMD” sepcifies 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.