statquant
statquant

Reputation: 14370

SAS convert string into datetime

I have a column in a table of string representing datetime like "01-Oct-2012 12:23:43.324" how can I cast this in a SAS datetime ?

Upvotes: 2

Views: 13620

Answers (2)

Longfish
Longfish

Reputation: 7602

The DATETIME informat will read that string

new_var=input(datestring,datetime24.);
format new_var datetime24.;

Upvotes: 4

Chris J
Chris J

Reputation: 7769

I don't think there is a single informat which will read that format of datetime... so split it into the date & time components then use the dhms function to create a datetime value.

data have ;
  datestring = "01-Oct-2012 12:23:43.324" ;
run ;

data want ;
  set have ;

  dt = input(scan(datestring,1,' '),??date11.) ;
  tm = input(scan(datestring,2,' '),??time14.) ;
  dttm = dhms(dt,0,0,tm) ;

  format dt date9. tm time14.3 dttm datetime24.3 ;
run ;

Upvotes: 0

Related Questions