BookmarkSubscribeRSS Feed
01SASUser
Fluorite | Level 6

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?




7 REPLIES 7
LinusH
Tourmaline | Level 20

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
data_null__
Jade | Level 19

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


RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Vix
Calcite | Level 5 Vix
Calcite | Level 5

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;

01SASUser
Fluorite | Level 6

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


ballardw
Super User

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

HBruun
Calcite | Level 5

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 2012 views
  • 6 likes
  • 7 in conversation