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

I need to deduct an hour from my Time variable. Currently, the variable is in TIME20.3 format. 

 

Here is an example, 

 

Time                                           

10:07:46.104                 

 

and i need 

 

Time_new

9:07:46.104

 

Also, i would like to create another time variable as

 

Time_new2

9:07:46.000

 

 

Thanks in advance for your help. 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Hello,

 

As mentioned by @Tom and @Sajid01 the format does not play a role w.r.t. your question.

 

You can also use the INTNX function to get what you want.

data _NULL_;
 have = '10:07:46.104't ;
 Time_new  = INTNX('HOUR',have,-1,'SAME');
 put Time_new  TIME20.3;
 Time_new2 = INTNX('SECOND',have,-60*60,'BEGINNING');
 put Time_new2 TIME20.3;
run;
/* end of program */

Cheers,

Koen

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Time values are stored as the number of seconds since midnight.  To to subtract one hour you need to subtract 60*60 seconds.

It is easier to just use a time literal.  '01:00't is one hour.

data test;
  have = '10:07:46.104't ;
  want = have - '01:00't ;
  format have want time20.3 ;
  put have= want=;
run;

Do you want to round the time to the newest second?  ROUND(want,1)

Or truncate the fractional seconds? INT(want)

 

Sajid01
Meteorite | Level 14

Internally SAS maintains time as number of seconds from midnight. So one hour is 3600 Seconds.
Subtracting 3600 gives the result.
Sample code is given below. Modify as needed

data test_time;
informat time_ time12.3;
format time_ time12.3;
input time_;
datalines;
10:07:46.104   
12:00:16.100  
02:11:26.040  
;
run;
data test_time;
format new_time time12.3;
set test_time;
new_time=time_ - 3600;
run;

The output will be some thing like this.
Time_ is the original time and new_time is what we get substracting one hour (3600 secs)

Obs new_time time_
1 9:07:46.104 10:07:46.104
2 11:00:16.100 12:00:16.100
3 1:11:26.040 2:11:26.040
sbxkoenk
SAS Super FREQ

Hello,

 

As mentioned by @Tom and @Sajid01 the format does not play a role w.r.t. your question.

 

You can also use the INTNX function to get what you want.

data _NULL_;
 have = '10:07:46.104't ;
 Time_new  = INTNX('HOUR',have,-1,'SAME');
 put Time_new  TIME20.3;
 Time_new2 = INTNX('SECOND',have,-60*60,'BEGINNING');
 put Time_new2 TIME20.3;
run;
/* end of program */

Cheers,

Koen

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
  • 3 replies
  • 941 views
  • 6 likes
  • 4 in conversation