Convert Character Expression to Time

Accepted Solution Solved
Reply
User
Posts: 1
Accepted Solution

Convert Character Expression to Time

I have a column with hours and minutes. Some examples below:

00:00

00:15

11:45

12:30

23:30

 

The format and informat of the column is $5. I need to subtract 1 hour from this column. So the result would be:

23:00

23:15

10:45

11:30

22:30

 

What is the easiest way of doing this? Should I be converting to time and then subtract or is there another way? Thanks in advance.


Accepted Solutions
Solution
‎03-14-2018 08:42 AM
Esteemed Advisor
Posts: 5,521

Re: Convert Character Expression to Time

Or simpler:

 

data want;
    set have;
    sastime = timepart( intnx('dthour',input(time,time5.),-1,'same'));
    format sastime time5.;
run;
PG

View solution in original post


All Replies
Super User
Posts: 13,501

Re: Convert Character Expression to Time

If your variable is character then you need to make a new variable with the numeric time (or write a whole bunch of code that the SAS time functions will handle for you with a time value). However since you are involved with crossing midnight boundaries you need to considire a date portion,You do not indicate whether the result should be numeric(a SAS time value) or character.

 

data want;
    set have;
    sastime = timepart( intnx('hour',dhms('01JAN2018'd,0,0,input(time,time5.)),-1,'same'));
    format sastime time5.;
run;

The INPUT portion creates the time of day of your time variable, the DHMS creates a datetime value based on the shown date, INTNX function increments the resulting datetime variable by minus 1 hour and keeps the minutes (and seconds) the same, then TIMEPART extracts the time portion only of the resulting date time.

 

The format displayes the SASTIME value results as 00:45.

Without the date portion the -1 hour would yield negative times like 00:00 - 01:00 = -01:00 instead of 23:00

Solution
‎03-14-2018 08:42 AM
Esteemed Advisor
Posts: 5,521

Re: Convert Character Expression to Time

Or simpler:

 

data want;
    set have;
    sastime = timepart( intnx('dthour',input(time,time5.),-1,'same'));
    format sastime time5.;
run;
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 101 views
  • 3 likes
  • 3 in conversation