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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Reeza
Super User

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. 


 

Tom
Super User Tom
Super User

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
SuryaKiran
Meteorite | Level 14
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 2720 views
  • 0 likes
  • 4 in conversation