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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 1541 views
  • 3 likes
  • 3 in conversation