DATA Step, Macro, Functions and more

Time Conversion

Reply
Contributor
Posts: 22

Time Conversion

Below is my initial data set

1 EFY.PNG

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


2 EFY.PNG


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?




Super User
Posts: 5,260

Re: Time Conversion

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.

Data never sleeps
Respected Advisor
Posts: 3,777

Re: Time Conversion

FYI time INFORMAT but for the OPs problem MDYAMPM is the preferred method.

data _null_;
  
do t = '09:09:09 am','09:09:09 pm';
      r = input(t,
time12.);
      format r time.;
     
put (_all_)(=);
      end;
  
run;

t=
09:09:09 am r=9:09:09
t=
09:09:09 pm r=21:09:09


Super User
Super User
Posts: 7,430

Re: Time Conversion

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;

New Contributor Vix
New Contributor
Posts: 4

Re: Time Conversion

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;

Contributor
Posts: 22

Re: Time Conversion

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".


Super User
Posts: 10,550

Re: Time Conversion

Instead of the time12 format you may want the TIMEAMPM11 format to display AM / PM as your previous program did.

New Contributor
Posts: 4

Re: Time Conversion

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;

Ask a Question
Discussion stats
  • 7 replies
  • 553 views
  • 6 likes
  • 7 in conversation