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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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