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.
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
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.
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
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.