Your SAS programs, embedded in web apps and elsewhere

Convert EPOCH date

Reply
Occasional Contributor
Posts: 6

Convert EPOCH date

Can anybody help me to convert EPOCH date to human redable date format ?

for example epoch date 1409071482460 should look like 8/26/2014.

Thanks

Anand

Super User
Posts: 7,766

Re: Convert EPOCH date

This is a short program I wrote to show the time and date of a UNIX timestamp (helps me to determine if someone's password has expired etc):

data times (keep=x date time);

x = 1398435549; * the UNIX timestamp;

days = int(x / 86400);

time = x - days * 86400;

date = '01jan1970'd + days;

format

  date mmddyy10.

  time time5.

;

label

  x = "Timestamp"

  date = "Date"

  time = "Time (GMT)"

;

run;

proc print data=times noobs label;

var x date time;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 3,212

Re: Convert EPOCH date

A more easy way, the epoch and sas datetime type only are different in the choicen point of zero. SAS is using 1 jan1960, the epoch is using 1 jan1970.  Unix time - Wikipedia, the free encyclopedia  - SAS(R) 9.4 Language Reference: Concepts, Third Edition (date time).   The conversion is adjusting the time-measure for the point of zero.  Giving this datetime variable a SAS format you like you can get every lay-out presented.

data times2;
x = 1398435549; * the UNIX timestamp;
datetm=x+"01jan1970 0:0:0"dt ;
Format datetm datetime. ;
run;

proc print;
run;

---->-- ja karman --<-----
Super User
Posts: 7,766

Re: Convert EPOCH date

It just occured to me that he could use a different epoch than the ones we are used to. 1409071482460 is 3 digits longer.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,799

Re: Convert EPOCH date

Posted in reply to KurtBremser

Could it be that it needs a decimal.

32         GOPTIONS ACCESSIBLE;

33         data _null_;

34            x = 1409071482.460 + '01JAN1970:00:00:00'dt;

35            *should look like 8/26/2014.;

36            put x=datetime25.3;

37            run;

x=26AUG2014:16:44:42.460

Trusted Advisor
Posts: 3,212

Re: Convert EPOCH date

Yep you are right, http://www.epochconverter.com/ older system 32-bit are based on seconds, newer systems using the "long epoch" are using fields in milliseconds.
IF this "long epoch" is read binary or as decimal to get seconds divide by 1000.   Checked his long number and it is a "long epoch".  26aug2014:16:44:22

Current Millis - convert millis to date and time, live UTC time

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 5 replies
  • 2927 views
  • 0 likes
  • 4 in conversation