Destring a time variable using Stata


How to destring a time variable (7:00) using Stata?

I have tried destring: however, the : prevents the destring. I then tried destring, ignore(:) but was unable to then make a double and/or format %tc. encode does not work; recast does not do the job.

I also have a separate string date that I was able to destring and convert to a double.

Am I missing that I could be combining these two string variables (one date, one time) into a date/time variable or is it correct to destring them individually and then combine them into a date/time variable?


<strong>Short answer</strong>

To give the bottom line first: two string variables that hold date and time information can be converted to a single numeric date-time variable using some operation like

generate double datetime = clock(date + time, "DMY hm") format datetime %tc

except that the exact details will depend on exactly how your dates are held.

For understanding dates and times in Stata there is no substitute for

help dates and times

Everything else tried is likely to be wrong or irrelevant or both, as your experience shows.

<strong>Longer answer, addressing misconceptions</strong>

destring, encode and recast are all (almost always) completely wrong in Stata for converting string dates and/or times to numeric dates and/or times. (I can think of one exception: if somehow a date in years had been imported as string with values "1960", "1961", etc. then destring would be quite all right.)

In reverse order,


recast is not for any kind of numeric to string or string to numeric conversion. It only recasts among numeric or among string types.

</li> <li>

encode is essentially for mapping obvious strings to numeric and (unless you specify otherwise) will produce integer values 1, 2, 3, and so forth which will be quite wrong for times or dates in general.

</li> <li>

destring as you applied it implies that the string times "7:00", "7:59", "8:00" should be numeric, except that someone stupidly added irrelevant punctuation. But if you strip the colons :, you get times 700, 759, 800, etc. which will not match the standard properties of times. For example, the difference between "8:00" and "7:59" is clearly one minute, but removing the <strong>informative</strong> punctuation would just yield numbers 800 and 759, which differ by 41, which makes no sense.

</li> </ul>

For a pure time, you can set up your own system, or use Stata's date-time functions.

For a time between "00:00" and "23:59" you can use Stata's date-times:

. di %tc clock("7:00", "hm") 01jan1960 07:00:00 . di %tc_HH:MM clock("7:00", "hm") 07:00

With variables you would need to generate a new variable and make sure that it is created as double.

A pure time less than 24 hours is (notionally) a time on 1 January 1960, but you can ignore that. But you need to hold in mind (constantly!) that the underlying numeric units are <strong>milliseconds</strong>. Only the format gives you a time in conventional terms.

If you have times more than 24 hours, that is probably not a good idea.

Your own system could just be to convert string times in the form "hh:mm" to minutes and do calculations in those terms. For times held as variables, the easiest way forward would be to use split, destring to produce numeric variables holding hours and minutes and then use 60 * hours + minutes.

However, despite your title, the real problem here seems to be dealing jointly with date and time information, not just time information, so at this point, you might like to read the short answer again.


  • List all files in a folder and subfolders in Excel
  • SQL Server 2008 generate script wizard gives me a script that results in “unclosed quotation marks”
  • Using sed to extract string values
  • Get Quarters StartDate and EndDate from Year
  • MS SQL Server 2008 :Getting start date and end date of the week to next 8 weeks
  • Suspending event listeners
  • Aggregating two data frame columns without any existing pattern logic
  • SSRS 2008 - Sorting within a group
  • Pass nested C++ vector as built-in style multi-dimensional array
  • How to upload files in php using html
  • data.table replicate rows after join?
  • Excel distinct count with conditions
  • Motorola barcode scanner SDK events C#
  • Better Indy for Dephi 2007
  • Where these are stored?
  • How do I remove all but some records based on a threshold?
  • WP8.1 AppBarButton holding event
  • SetWindowsHookEx does not react on media keys
  • How do I open a C file with a relative path?
  • ActiveRecord query for a count of new users by day
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Atlas images wrong size on iPad iOS 9
  • Yii2: Config params vs. const/define
  • Meteor: Do Something On Email Verification Confirmation
  • Illegal mix of collations for operation for date/time comparison
  • Master page gives error
  • NetLogo BehaviorSpace - Measure runs using reporters
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Where to put my custom functions in Wordpress?
  • ActionScript 2 vs ActionScript 3 performance
  • RestKit - RKRequestDelegate does not exist
  • Matrix multiplication with MKL
  • A cron job substitute?
  • WPF Applying a trigger on binding failure
  • Why joiner is not used after Sequence generator or Update statergy
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Android Heatmap on canvas or ImageView
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?