BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ummath16
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

2 REPLIES 2
ballardw
Super User

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

PGStats
Opal | Level 21

Or simpler:

 

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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