DATA Step, Macro, Functions and more

How to convert Julian Datetime to DateTime?

Accepted Solution Solved
Reply
Contributor jei
Contributor
Posts: 61
Accepted Solution

How to convert Julian Datetime to DateTime?

Hi,

 

I have an issue regarding on converting the Julian datetime to datetime. The data are given below which is in julian datetime.

 

1465538477225
1465547195922
1465534266266
1465546595003
1465543587493
1465545693637
1465531252406

 

I want to ask how am I going to convert thesejulian datetime in ANYDTDTE20. format.

 

Thank you!

 


Accepted Solutions
Solution
‎06-12-2016 11:12 PM
Trusted Advisor
Posts: 1,115

Re: How to convert Julian Datetime to DateTime?

[ Edited ]

Hi @jeideegertes,

 

Your "Julian" datetime values seem to be numbers of milliseconds since January 1, 1970, 00:00:00, which makes the conversion easy:

data want;
input jdt;
dt=jdt/1000+'01JAN1970:0:0'dt;
d=datepart(dt);
t=timepart(dt);
format jdt 14.
       dt  datetime20.
       d   mmddyy10.
       t   e8601tm.;
cards;
1465538477225
1465547195922
1465534266266
1465546595003
1465543587493
1465545693637
1465531252406
;

proc print data=want;
run;

 

Edit:


jeideegertes wrote:

I want to ask how am I going to convert thesejulian datetime in ANYDTDTE20. format. 


 ANYDTDTE20. is an informat, not a format. It cannot be used to read values in your specific datetime format.

View solution in original post


All Replies
Respected Advisor
Posts: 4,661

Re: How to convert Julian Datetime to DateTime?

What is the definition of those "Julian datetimes"? If they were expressed in seconds, the way SAS datetimes are, they would correspond to more than 46000 years. So they must be expressed in a smaller time unit.

PG
Contributor jei
Contributor
Posts: 61

Re: How to convert Julian Datetime to DateTime?

Julian Datetime also includes time, the hour, minutes, and seconds. The excel conversion of the following values are:

 

1465538477225  =  06/10/2016 06:01:17
1465547195922  =  06/10/2016 08:26:36
1465534266266  =  06/10/2016 04:51:06
1465546595003  =  06/10/2016 08:16:35
1465543587493  =  06/10/2016 07:26:27
1465545693637  =  06/10/2016 08:01:34
1465531252406  = 06/10/2016 04:00:52

 

How can I get the datetime values in SAS just like the result in excel?

 

Thank you!

Contributor
Posts: 62

Re: How to convert Julian Datetime to DateTime?

I don't think SAS has a Julian datetime format or informat. In SAS, you will need to separate the values from Excel into dates and times; you can then translate the date part according to this:

 

http://support.sas.com/kb/38/811.html

Norman.
SAS 9.4 (TS1M0) X64_7PRO WIN 6.1.7601 Service Pack 1 Workstation

Solution
‎06-12-2016 11:12 PM
Trusted Advisor
Posts: 1,115

Re: How to convert Julian Datetime to DateTime?

[ Edited ]

Hi @jeideegertes,

 

Your "Julian" datetime values seem to be numbers of milliseconds since January 1, 1970, 00:00:00, which makes the conversion easy:

data want;
input jdt;
dt=jdt/1000+'01JAN1970:0:0'dt;
d=datepart(dt);
t=timepart(dt);
format jdt 14.
       dt  datetime20.
       d   mmddyy10.
       t   e8601tm.;
cards;
1465538477225
1465547195922
1465534266266
1465546595003
1465543587493
1465545693637
1465531252406
;

proc print data=want;
run;

 

Edit:


jeideegertes wrote:

I want to ask how am I going to convert thesejulian datetime in ANYDTDTE20. format. 


 ANYDTDTE20. is an informat, not a format. It cannot be used to read values in your specific datetime format.

Contributor jei
Contributor
Posts: 61

Re: How to convert Julian Datetime to DateTime?

[ Edited ]

Hello @FreelanceReinhard,

That what I was looking for. I am also able to convert the other Julian dates.

Thank you very much!

If it is okay, I still have a question. I wanted to get the current date in which the format is similar to the Julian date format. How am I going to extract or get the current date?

Thank you!

Trusted Advisor
Posts: 1,115

Re: How to convert Julian Datetime to DateTime?

You're welcome. So, you want to convert the current datetime to a Julian datetime value (in the sense of milliseconds since January 1, 1970, 00:00:00)?

 

data _null_;
jdt=round((datetime()-'01JAN1970:0:0'dt)*1000);
put jdt= 14.;
run;

The DATETIME function returns the current datetime as a SAS datetime value (seconds since January 1, 1960, 00:00:00).  I used the ROUND function to ensure integer results. On my Windows workstation, however, rounding to integers seems to be redundant, because the time precision (DATETIME() values) is limited to milliseconds anyway. I would still use the ROUND function (if I needed integer results) in order to avoid numeric representation issues.

 

The optional PUT statement writes the result to the log.

Contributor jei
Contributor
Posts: 61

Re: How to convert Julian Datetime to DateTime?

Hi @FreelanceReinhard,

No, what I mean is I wanted to get the current date. But when I'm getting it the resulted format is in Julian Datetime. That is, what I want is to get the current date (which is in Julian DateTime) in DateTime Function.
Smiley Happy)
Trusted Advisor
Posts: 1,115

Re: How to convert Julian Datetime to DateTime?

Well, the current date as a SAS date value (number of days after January 1, 1960) is returned by the DATE (or TODAY) function. By default, a date does not contain a time component. To convert a date to a Julian datetime value, you have to specify a time value in addition to the date. In the data step below I use 00:00:00 as the time value, but feel free to replace it by something else (up to 23:59:59).

data _null_;
t='00:00:00't;
jdt=round((date()*86400+t-'01JAN1970:0:0'dt)*1000);
put jdt= 14.;
run;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 588 views
  • 3 likes
  • 4 in conversation