Help using Base SAS procedures

Convert Stata DateTime to SAS DateTime

Frequent Contributor
Posts: 78

Convert Stata DateTime to SAS DateTime

I'm frequently having to move data back and forth from SAS to Stata or Stata to SAS. I know how to convert SAS Date/Time to Stata, but I cannot find a way to convert Stata Date/Time back to SAS.

Is there a known calculation to do this?

The variables come into SAS formatted as datetime18., but the time is incorrect. I used StatTransfer v11 to do this, but I also tried exporting from Stata and it still did not work.

My thought is that I will need to break this out into separate date and time variables and recalculate time and then put them back together.

Any ideas if this will work or if there is an easier option?

Thank in advance!
Posts: 7,364

Re: Convert Stata DateTime to SAS DateTime

I'm not familiar with it, but you could try the following:
or, in short form:

It is a routine, from someone at Duke University, that supposedly converts stata files to sas files, including datetime variables.

Frequent Contributor
Posts: 78

Re: Convert Stata DateTime to SAS DateTime

Thank you for this link.

I was able to install it and run it with no problem. It did create the SAS database and it looks like dates formatted fine, but still the datetime variables did not convert correctly. I did try to format into datetime and all that I get is a long number, never a formatted datetime variable.
Valued Guide
Posts: 632

Re: Convert Stata DateTime to SAS DateTime

When you say that the times did not convert correctly do you mean that they come out as 12:43 when they should be 13:35? or are they just not 'formatted'. Have you applied a datetime format? Datetime values are a large number (seconds since midnight of 01jan1960 - the start of time).

when you use the following what happens?
put myval= datetime20.;
Frequent Contributor
Posts: 78

Re: Convert Stata DateTime to SAS DateTime

Stata time is in milliseconds from 01jan1960 00:00:00.000 where 1 is one millisecond from that time. 01jan1960 00:00:00.001

When I try to format (datetime20.) it just doesn't work, I still get the long number in milliseconds. I realize now that I"m going to have to convert the milliseconds first (to seconds) and then format the datetime value.

Thanks for helping me realize this.
Super Contributor
Super Contributor
Posts: 3,174

Re: Convert Stata DateTime to SAS DateTime

With an output FORMAT of datetime20. you are telling SAS to truncate at the second -- suggest you first try using BEST. as an output format and you may see "seconds since 1/1/1960". Then explore using DATETIME format specified as datetime20.3 instead.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

import stata dta datetime
Ask a Question
Discussion stats
  • 5 replies
  • 4 in conversation