12

I'm new to Stata, and I'm wondering how can I change a string variable which contains a date to a date format.

The data in the variable looks like this:

yyyy-mm-dd

Should I first remove the dashes so that Stata can recognize the format in order to later use gen var = date() ?

Thank you for your help.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
finstats
  • 1,349
  • 4
  • 19
  • 31
  • did you get the answer you needed?\ – No Idea For Name Aug 14 '13 at 11:13
  • The word "format" is treacherous here. "yyyy-mm-dd" as a pattern or style for holding dates is a format (sense 1). Assigning Stata's daily date display format with the `format` command is Stata's sense of the word (sense 2), but is not sufficient. Date format (sense 3) means daily dates being stored numerically and counted relative to 1 January 1960. – Nick Cox Aug 14 '13 at 12:07

2 Answers2

15

The Stata date function is smart about removing separator characters. See help datetime_translation under the section "the date function"

If your dates are in v1 and in the form yyyy-mm-dd you can specify the commands:

generate v2 = date(v1, "YMD")
format %td v2

The YMD is called a mask, and it tells Stata the order in which the parts of the date are specified. The second line will assign the variable the Stata daily date format, which means that when you look at that variable in the data, it will be shown in human readable form. The date is stored, however, as the number of days since January 1, 1960.

The best way to experiment with the date function is to use the display command. The first line will display an integer representing the number of days since January 1, 1960. The second line will display the date in a human readable format.

display date("2013-08-14", "YMD")
display %td date("2013-08-14", "YMD")
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
sechilds
  • 859
  • 7
  • 5
  • 1
    I've edited the explanation of formatting slightly. People too often confuse assigning a display format with changing what is stored. Your wording was good, but I have tightened it further. – Nick Cox Aug 14 '13 at 11:59
1

you can look here to see how to convert to data in Stata or do like this

tostring datedx, replace
generate str4 dxyr1= substr(datedx,1,4)
generate str2 dxmo1 = substr(datedx,6,7)
generate str2 dxda1 = substr(datedx,9,10)
destring dx*, replace
gen datedx1 = mdy(dxmo1, dxda1, dxyr1)
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • 1
    The OP starts with a string variable, so `tostring` is redundant, but harmless. Using the `date()` function, as in the answer by @sechilds, is much more direct. – Nick Cox Aug 14 '13 at 12:02