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.
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
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)
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 |
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.