Character to Date

Accepted Solution Solved
Reply
Contributor ZZB
Contributor
Posts: 34
Accepted Solution

Character to Date

How to convert the following Charater to Date format?

 

For example, raw variable DATE "01 MAR 2013 00:00:02.358". The length is 24. There are space between '01' and 'MAR' and '2013' and '00'.

 

How to seperate it into two column '03/01/2013' + 'hh:mm:ss:sss' or 'hh:mm:ss'. Both are date format. Thanks!

 


Accepted Solutions
Solution
‎01-24-2016 01:16 PM
Contributor
Posts: 53

Re: Character to Date

Hi ZZB;

 

try this 

 

data have;
DATE = "01 MAR 2013 00:00:02.358";

run;

 

data want;
set have;
NewDate = input(compress (substr(DATE ,1,11)),date9.);
NewTime = input( scan(DATE,-1,' '),time12.);
format NewDate date9. NewTime time12.;

run;

View solution in original post


All Replies
Super User
Posts: 17,743

Re: Character to Date

Are you reading these from a text file or is the data already in SAS?

 

You can try compress and input to read in the data.

 

untested:

 

datetimevar=input(compress(char_val), anydtdtm.);
var_datepart=datepart(datetimevar);
var_timepart=timepart(datetimevar);

format var_datepart date9. var_timepart time8.;
Contributor ZZB
Contributor
Posts: 34

Re: Character to Date

The data is already in SAS.
datetimevar=input(compress(char_val), anydtdtm.); DOES NOT WORK.
samnan's code works. Thanks!
Super User
Posts: 17,743

Re: Character to Date

The informat provided by @data_null__ is the correct informat. Correcting the informat fixes the issues.

 

data have;

original_var = "01 MAR 2013 00:00:02.358";
temp=compress(original_var);

dt_var=input(original_var, datetime24.3);
date_part=datepart(dt_var);
time_part=timepart(dt_var);

format dt_var datetime24.3 date_part date9. time_part time11.3;
run;

proc print data=have;
run;
Respected Advisor
Posts: 3,777

Re: Character to Date

34         data _null_;
35            input dt & datetime.;
36            put (dt)(=datetime22.3);
37            list;
38            cards;

dt=01MAR2013:00:00:02.358
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                     
39         01 MAR 2013 00:00:02.358
Solution
‎01-24-2016 01:16 PM
Contributor
Posts: 53

Re: Character to Date

Hi ZZB;

 

try this 

 

data have;
DATE = "01 MAR 2013 00:00:02.358";

run;

 

data want;
set have;
NewDate = input(compress (substr(DATE ,1,11)),date9.);
NewTime = input( scan(DATE,-1,' '),time12.);
format NewDate date9. NewTime time12.;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 413 views
  • 3 likes
  • 4 in conversation