Convert Unix datetime to SAS

How to convert a Unix datetime to a SAS datetime

Unix datetime and SAS datetime are two different formats used to represent date and time values in computer systems.

A SAS datetime value is the number of seconds that have elapsed since midnight of January 1, 1960 (01JAN1960:00:00:00).

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

Epoch timestamp: 1682828485
Timestamp in milliseconds: 1682828485000
Date and time (GMT): Sunday, 30 April 2023 04:21:25
Date and time (Your time zone): Sunday, 30 April 2023 09:51:25 GMT+05:30

You can convert a Unix datetime to a SAS datetime by adding the number of seconds in 10 years. The Number of seconds between 01JAN1960 and 01JAN1970 is 315619200.

Adding the SAS epoch value to the result gives you the number of seconds that have elapsed since January 1, 1960, 00:00:00 UTC, which is the SAS datetime value.

data _null_;
	unixDT=1682828627;
	sasDT=unixDT + 315619200;
	format _all_ datetime20.;
	put (_all_)(+4=);
run;
unixDT=29APR2013:04:23:47 sasDT=30APR2023:04:23:47

You can also use the dhms() function instead of the 9-digit constant. The dhms() function returns a SAS datetime value from date, hour, minute, and second values.

data _null_;
	unixDT=1682828627;
	sasDT=dhms('01jan1970'd, 0, 0, unixDT);
	format _all_ datetime20.;
	put (_all_)(+4=);
run;

A Unix time is often represented as UTC, so you may want to use tzoneoff() function to return the local time in SAS.

data _null_;
	unixDT=1682828627;
	sasDTLocal = dhms('01jan1970'd,0,0, unixDT + TZONEOFF());
	format _all_ datetime20.;
	put (_all_)(+4=);
run;
unixDT=29APR2013:04:23:47 sasDTLocal=30APR2023:09:53:47

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.