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-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
  • 380 views
  • 2 likes
  • 4 in conversation