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

%let time= %sysfunc(compress(%str(%')%sysfunc(time(),time.)%str(%'t)));

%put &time.;

 

Variable time will get the current time. May I know how to adjust the program wherein it will get the time 14 hours earlier. Example, if the current time id 20:00:00, I want to get the time 14 hours earlier which is 06:00:00. Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%let time_minus_14_f=%sysfunc(intnx(hour,%sysfunc(time()),-14,s),time8.);
%put &=time_minus_14_f; /* just to show that it works */
%let time_minus_14=%sysfunc(intnx(hour,%sysfunc(time()),-14,s));
%put &=time_minus_14; /* this is the value you want to work with */

Note that, for the vast majority of use cases, it is better to store the raw (unformatted) value, as it causes less hassle when creating it (no %str(%'t) needed) and using it in comparisons or as macro parameters (no quoting needed). The only time you need formatted values in macro variables is when you need them for display (e.g. in a title statement).

Also see Maxim 28.

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
ed_sas_member
Meteorite | Level 14

Hi @iSAS 

 

The following macro variable will return the current time minus 14hours. The result is a numeric value, which can be used for further calculation (even if not 'human-readable').

%let time = %sysfunc(intnx(hour,%sysfunc(time()),-14));
%put &time.;

I you want to format it (which is not recommended except for use in title for example), you can do that:

%let time = %sysfunc(intnx(hour,%sysfunc(time()),-14),time.);
%put &time.;
Kurt_Bremser
Super User
%let time_minus_14_f=%sysfunc(intnx(hour,%sysfunc(time()),-14,s),time8.);
%put &=time_minus_14_f; /* just to show that it works */
%let time_minus_14=%sysfunc(intnx(hour,%sysfunc(time()),-14,s));
%put &=time_minus_14; /* this is the value you want to work with */

Note that, for the vast majority of use cases, it is better to store the raw (unformatted) value, as it causes less hassle when creating it (no %str(%'t) needed) and using it in comparisons or as macro parameters (no quoting needed). The only time you need formatted values in macro variables is when you need them for display (e.g. in a title statement).

Also see Maxim 28.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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