How to convert Datetime into SAS format

I am working with a huge number of observations in different tables in different versions.

I will use a date %let date_to_view = "20JAN2014:16:10"dt; But how to convert this date into SAS format?

I knew how to Convert SAS data type (use proc sql):

dhms("01JAN1970'd,3,0,i.valid_dttm/1000) format datetime20.

I see date 20JAN2014:16:34:10 is 1390224849927 but how to convert it into code?

Upvotes: 0

Views: 1523

Answers (2)

Tom
Tom

Reputation: 51566

In your formula dhms("01JAN1970'd,3,0,i.valid_dttm/1000) you are converting a number that represents the number of milliseconds since 01JAN1970 to a SAS datetime value that represents the number of seconds since 01JAN1960. You also appear to be adding 3 hours.

So it sounds like your question is how to convert a SAS DATETIME value into a Unix timestamp value. So just reverse the arithmetic.

Your formula to convert from a Unix timestamp to a SAS datetime was:

sasdt2 = '01JAN1970:00:00'dt + '03:00't + unix_timestamp2/1000 ;

So to convert from a SAS datetime value to a Unix timestamp use:

unix_timestamp1 = 1000*(sasdt1 - '01JAN1970:00:00'dt - '03:00't) ;

Upvotes: 3

Chris J
Chris J

Reputation: 7769

"20JAN2014:16:10"dt is already in the correct SAS date (datetime) format, but as a date literal. SAS stores this as a number, representing the number of seconds since 01JAN1960:00:00:00.

If you just want the date component of the datetime, use the datepart() function, and format the result accordingly, e.g. date9..

data want ;
  dt = "20JAN2014:16:10"dt ;
  date = datepart(dt) ;
  format dt datetime19. date date9. ;
  /* To have 'date' show as the unformatted value, simply remove the format */
  format date best32. ;
run ;

Upvotes: 1

Related Questions