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.
Or simpler:
data want;
set have;
sastime = timepart( intnx('dthour',input(time,time5.),-1,'same'));
format sastime time5.;
run;
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
Or simpler:
data want;
set have;
sastime = timepart( intnx('dthour',input(time,time5.),-1,'same'));
format sastime time5.;
run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.