How to extract seperate numeric date and time from Character datetime string

Accepted Solution Solved
Reply
New User
Posts: 1
Accepted Solution

How to extract seperate numeric date and time from Character datetime string

Hi,

 

I have character variables containing datetime stamps in the format of 0000-00-00-00.00.00.000000 (e.g. 2008-01-03-09.58.19.207772)

 

I am currently able to extract the date part of these variables using the below code:

 

data want;

set have;

char_date = substr(char_date_time,1,10);

num_date = input(char_date,anydtdte11.);

format num_date_time2 ddmmyy10.;

run;

 

I now want to complete the same exercise in this step to extract the time from these variables in to its own variable, but after trying a number of things I can't seem to get it to work.

 

Hopefully someone can help. Thanks in advance. 


Accepted Solutions
Solution
‎03-29-2018 03:37 AM
Super User
Super User
Posts: 8,081

Re: How to extract seperate numeric date and time from Character datetime string

Posted in reply to paddy_fenna

Should be easy.

data test;
  input string $32. ;
  date = input(substr(string,1,10),yymmdd10.);
  time = input(substr(string,12),time32.);
  datetime=dhms(date,0,0,time);
  format datetime datetime24.3 date date9. time time12.3 ;
  put (_all_) (=/);
cards;
2008-01-03-09.58.19.207772
;
string=2008-01-03-09.58.19.207772
date=03JAN2008
time=9:58:19.208
datetime=03JAN2008:09:58:19.208

View solution in original post


All Replies
Super User
Posts: 23,689

Re: How to extract seperate numeric date and time from Character datetime string

Posted in reply to paddy_fenna

The same approach would work. 

Can you post what you've tried so far, so we can let you know where the mistake is?

 


@paddy_fenna wrote:

Hi,

 

I have character variables containing datetime stamps in the format of 0000-00-00-00.00.00.000000 (e.g. 2008-01-03-09.58.19.207772)

 

I am currently able to extract the date part of these variables using the below code:

 

data want;

set have;

char_date = substr(char_date_time,1,10);

num_date = input(char_date,anydtdte11.);

format num_date_time2 ddmmyy10.;

run;

 

I now want to complete the same exercise in this step to extract the time from these variables in to its own variable, but after trying a number of things I can't seem to get it to work.

 

Hopefully someone can help. Thanks in advance. 


 

Solution
‎03-29-2018 03:37 AM
Super User
Super User
Posts: 8,081

Re: How to extract seperate numeric date and time from Character datetime string

Posted in reply to paddy_fenna

Should be easy.

data test;
  input string $32. ;
  date = input(substr(string,1,10),yymmdd10.);
  time = input(substr(string,12),time32.);
  datetime=dhms(date,0,0,time);
  format datetime datetime24.3 date date9. time time12.3 ;
  put (_all_) (=/);
cards;
2008-01-03-09.58.19.207772
;
string=2008-01-03-09.58.19.207772
date=03JAN2008
time=9:58:19.208
datetime=03JAN2008:09:58:19.208
Valued Guide
Posts: 590

Re: How to extract seperate numeric date and time from Character datetime string

[ Edited ]
Posted in reply to paddy_fenna
data have;
input datetime $50.;
datalines;
2008-01-03-09.58.19.207772
;
run;
data want (Drop=datetime_) ;
format datetime datetime24.3;
set have(rename=(datetime=datetime_));
datetime=dhms(input(substr(datetime_,1,10),yymmdd10.),0,0,input(substr(datetime_,12),time32.));
run;

You need to rename the variable, if you wish to change the type of a variable as you desired. Rename and use @Tom suggestion to convert to DATETIME format

 

Thanks,
Suryakiran
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 99 views
  • 0 likes
  • 4 in conversation