BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10

hello

data have;
input TSP_DEB	TSP_MOF datetime25.6 ;
informat TSP_DEB	TSP_MOF datetime25.6;
format TSP_DEB	TSP_MOF datetime25.6 ;
datalines ;
24SEP2025:15:13:35.000000 24SEP2025:15:21:00.000000
;

i get 2 datetime. i would like to create a colum with "0h 7m 25s"

many thanks for your help

Nass

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    tsp_deb_time=timepart(tsp_deb);
    tsp_mof_time=timepart(tsp_mof);
    difference=tsp_mof_time-tsp_deb_time;
    format difference time12.;
run;

 

If you have to have the time formatted with the letters h m and s then I think you will have to create your own custom format, see instructions here https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/p1717ykol58oijn13lw9rs30lhn0.htm

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
data want;
    set have;
    tsp_deb_time=timepart(tsp_deb);
    tsp_mof_time=timepart(tsp_mof);
    difference=tsp_mof_time-tsp_deb_time;
    format difference time12.;
run;

 

If you have to have the time formatted with the letters h m and s then I think you will have to create your own custom format, see instructions here https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/p1717ykol58oijn13lw9rs30lhn0.htm

--
Paige Miller
Quentin
Super User

@PaigeMiller I don't think timepart() is needed here, since date-times are also stored as number of seconds.  If it's possible for the time range to span midnight, then using timepart() could introduce problems.

 

1    data want;
2        set have;
3        difference=tsp_mof-tsp_deb;
4        put (tsp_mof tsp_deb difference)(=) ;
5        format difference time12.;
6    run;

TSP_MOF=24SEP2025:15:21:00.000000 TSP_DEB=24SEP2025:15:13:35.000000 difference=0:07:25
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 1 observations and 3 variables.
PaigeMiller
Diamond | Level 26

Yes, I agree.

 

The issue of whether or not the date/time values could span midnight was not mentioned by @Nasser_DRMCP , and based upon the example provided I assumed that the solution did not need to account for this. This issue probably should have been mentioned, and more than one record in the example data set would have been a good thing to provide.

--
Paige Miller
Tom
Super User Tom
Super User

So you have two DATETIME values (not DATE values).  Which helps because the difference between two dates would be in DAYS and it would be impossible to detect differences of less than 24 hours. 

 

But the difference between two datetimes is in SECONDS. So you can use the TIME format to display them.  Make sure the width is long enough to properly show the maximum duration.  The default width of 8 only has room for two digits for the houts, so can only handle durations that are less than 100 hours.

data have;
  input (TSP_DEB TSP_MOF) (:datetime.) ;
  duration =  TSP_MOF - TSP_DEB ;
  format TSP_DEB TSP_MOF datetime25.6 duration time20.6;
datalines ;
24SEP2025:15:13:35.000000 24SEP2025:15:21:00.000000
24SEP2025:15:13:35.000000 25SEP2025:15:21:00.000000
;
proc print;
run;

Tom_0-1758841240324.png

 

Ksharp
Super User

Or maybe you want to show this ?

 

proc format;
picture fmt
low-high='%Hh %Mm %Ss'(datatype=time)
;
run;

data have;
input TSP_DEB	TSP_MOF datetime25.6 ;
informat TSP_DEB	TSP_MOF datetime25.6;
format TSP_DEB	TSP_MOF datetime25.6 ;
diff=TSP_MOF-TSP_DEB;
format diff fmt.;
datalines ;
24SEP2025:15:13:35.000000 24SEP2025:15:21:00.000000
;

proc print;run;

Ksharp_0-1758871031964.png

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 463 views
  • 3 likes
  • 5 in conversation