BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ZZB
Obsidian | Level 7 ZZB
Obsidian | Level 7

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
samnan
Quartz | Level 8

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

5 REPLIES 5
Reeza
Super User

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.;
ZZB
Obsidian | Level 7 ZZB
Obsidian | Level 7
The data is already in SAS.
datetimevar=input(compress(char_val), anydtdtm.); DOES NOT WORK.
samnan's code works. Thanks!
Reeza
Super User

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;
data_null__
Jade | Level 19
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
samnan
Quartz | Level 8

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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