03-12-2018 02:00 PM
I have a column with hours and minutes. Some examples below:
The format and informat of the column is $5. I need to subtract 1 hour from this column. So the result would be:
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.
03-12-2018 03:47 PM
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
Need further help from the community? Please ask a new question.