Below is my initial data set
Below is my program that will separate date and time:
data FINAL;
set RAW;
format start_dT date9.;
format start_Time time8.;
format end_dT date9.;
format end_Time time8.;
start_dT = INPUT(SUBSTR(dt_start,1,10),mmddyy10.);
start_time = INPUT(SCAN(dt_start ,2, "")||SCAN(dt_start ,3, ""),time.);
End_dT = INPUT(SUBSTR(dt_end,1,10),mmddyy10.);
End_time = INPUT(SCAN(dt_end ,2, "")||SCAN(dt_end ,3, "") ,time.);
run;
However, there seems to be a problem with the program. Please refer to below image
Error: The value of start_time should be 22:10:33 but it returns a value of 10:10:33.
May I know how to fix this?
The time. informat doesn't (unfortunately) handle AM/PM, nor does anydttme.
Your option can be to handle this by code (scan for PM, then add 12*60*60 seconds to the result), or concatenate the back the date (in DDMmmYYYY format) and then use MDYAMPM. informat.
FYI time INFORMAT but for the OPs problem MDYAMPM is the preferred method.
Yes, I wouldn't do this the way you are doing it. Its much safer to use the functions and formats provided by SAS to get the parts:
data have;
attrib mdate format=mdyampm25.;
infile datalines;
input mdate mdyampm25.;
datalines;
09-15-08 03:53:00 PM
;
run;
data want;
set have;
attrib date format=date9. time format=time5.;
date=datepart(mdate);
time=timepart(mdate);
run;
proc format;
picture myfmt low-high= '%m/%d/%Y %I:%0M:%0S %P' (DATATYPE=DATETIME);
RUN;
data new;
input dt_start & mdyampm25. dt_end mdyampm25.;
format dt_start dt_end myfmt.;
datalines;
8/30/2013 10:10:33PM 8/31/2013 3:38:49PM
8/30/2013 10:10:35PM 9/01/2013 7:18:46PM
;
data new1;
set new;
date_start= put(datepart(dt_start),mmddyy10.);
time_start= put(timepart(dt_start),time8.);
date_end= put(datepart(dt_end),mmddyy10.);
time_end= put(timepart(dt_end),time8.);
run;
I revised my program by changing time. to time12. :
data FINAL;
set RAW;
format start_dT date9.;
format start_Time time12.;
format end_dT date9.;
format end_Time time12.;
start_dT = INPUT(SUBSTR(dt_start,1,10),mmddyy10.);
start_time = INPUT(SCAN(dt_start ,2, "")||SCAN(dt_start ,3, ""),time12.);
End_dT = INPUT(SUBSTR(dt_end,1,10),mmddyy10.);
End_time = INPUT(SCAN(dt_end ,2, "")||SCAN(dt_end ,3, "") ,time12.);
run;
10:00:00 PM now became 22:00:00. My question is will this program won't have any "glitch". Because in my previous program the time from 10:00:00 PM to 12:59:59PM produce a result of 10:00:00 to 12:59:59 instead of 22:00:00 to 00:59:59. But right now I get 22:00:00 to 00:59:59. I just want to be sure that my new program has now "glitch".
Instead of the time12 format you may want the TIMEAMPM11 format to display AM / PM as your previous program did.
data test;
*char datetime;
a='8/30/2013 10:10:33 PM';
*convert to sas datetime using input;
b=input(a, mdyampm22.);
*split to date and time;
date = datepart(b);
time= timepart(b);
*whatever format you wish to apply;
format b datetime. date ddmmyy10. time time8.;
run;
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.