Turn a date into Stata format quickly

There's a little program that's shown up more than once now in my housekeeping do-files, so it may be useful enough for a blog post, but it doesn't quite warrant a spot in c(sysdir_personal) as a stand-alone ado-file. Here:


// turn this date to Stata format
// if it's not that way already
capture prog drop setStataDate
program setStataDate

args v fmt // fmt can be MDY or YMD

tempname x
capture confirm string variable `v'
if _rc==0 {
   local l`v' : variable label `v'
   gen `x'=date(`v',"`fmt'")
   format `x' %td
   drop `v'
   rename `x' `v'
   label variable `v' "`l`v''"
   // order `v'
}

end

I use it with data sets derived from merging other data sets. It's useful if in the original data sets there are string dates in mixed formats -- maybe YYYY-MM-DD in the "master", and MM/DD/YYYY in the "using" -- or if these string dates have labels I want to keep. So, you see why it's not clear that this is worth an ado-file. I don't want to type all the code between the curlies more than once, but usually I don't have to.

I do want to be able to call this program by name, as in setStataDate somedate MDY from within another program, then forget about it, safe in the knowledge that it won't make any difference if somedate is already in Stata format. That's the job of the if-condition you see there, and this is all this little program does.

As to the reason for using the temporary name x, see the comment thread. Temporary names for variables generated only to be renamed are the safe option. The alternative is to use a one-letter convenience name, like x, and I did that first. But what if you want to use this program with a data set that includes a variable named x already?

5 Responses to “Turn a date into Stata format quickly”

  1. Nick Cox writes:

    Interesting idea. Two small comments:

    Better to work with a -tempvar x- than a permanent variable x.

    Not everyone will want the result of -order `v'-.

  2. Gabi Huiber writes:

    Hi Nick. You're right about order `v'; I could have posted a personal taste disclaimer there. But I'm not sure what the gain from tempvar is in this case. I do want to keep x for good, once it's renamed and labeled.

  3. Nick Cox writes:

    What you say clashes with your code. The name -x- is used only within the program. Using -tempvar x- ensures no clash with an existing variable named -x-.

  4. Gabi Huiber writes:

    You're right! I haven't thought of it because I don't ever have variables named x in my data (I expect my variable names to be more descriptive than that) but that's no excuse. Tempvar would have been better. Thank you.

  5. Nick Cox writes:

    That's right. Once you write programs to be made public it is best to avoid all names that might be in use, which means any! The justifiable exceptions might -generate- variables with specific names, but that should be documented; the program should check whether they exist and bail out if they do; and there should be ways of supplying alternative names. That may sound a tough order but once you see why it becomes natural to write that way.

    I suspect lots of users have variables called -x-!

Leave a Reply