%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.
%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.
Hi @iSAS You would need INTNX function (hour interval)
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.;
%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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.