BookmarkSubscribeRSS Feed
david27
Quartz | Level 8

Need help in calculating difference in time between two macro variables.

Here is what i have:

%let curr_datetime = 22NOV2024:09:23:33;
%let modate = 22NOV2024:05:55:55;

%macro time_diff(curr_datetime, modate);
proc sql noprint;
    select intck('second', &modate, &curr_datetime) into :time_diff
    from sashelp.vcalendar;
  quit;
  %put The time difference is &time_diff seconds;
%mend time_diff;
%time_diff(&curr_datetime.,&modate.);

Please advise.

Thank You

4 REPLIES 4
SASJedi
Ammonite | Level 13

Your datetime literals in the SQL must be written as datetime literals. I'd also add the TRIMMED modifier to your INTO clause to trim off leading blanks:

%let curr_datetime = 22NOV2024:09:23:33;
%let modate = 22NOV2024:05:55:55;

%macro time_diff(curr_datetime, modate);
proc sql noprint;
    select intck('dtsecond', "&modate"dt, "&curr_datetime"dt) into :time_diff trimmed
    from sashelp.cars(obs=1);
  quit;
  %put The time difference is &time_diff seconds;
%mend time_diff;
%time_diff(&curr_datetime.,&modate.);
Check out my Jedi SAS Tricks for SAS Users
Quentin
Super User

You don't need to use a SQL step for the calculation, you can use the %SYSFUNC macro function to call INTCK, e.g.:

 

1    %let curr_datetime = 22NOV2024:09:23:33;
2    %let modate = 22NOV2024:05:55:55;
3
4    %put The time difference is: %sysfunc(intck(minutes,"&curr_datetime"dt,"&modate"dt)) minutes. ;
The time difference is: -208 minutes.
Tom
Super User Tom
Super User

That macro would work if you gave it actual datetime values instead of the strings you actually have.

%time_diff("&curr_datetime"dt,"&modate"dt);

But the macro is way too complicated.

%macro time_diff(curr_datetime, modate);
%local time_diff;
%let time_diff=%sysfunc(intck(second, &modate, &curr_datetime));
%put The time difference is &time_diff seconds;
%mend time_diff;

If you actually want the time in some other interval then you might add another parameter to the macro.

%macro time_diff(curr_datetime, modate, interval);
%local time_diff;
%if 0=%length(&interval) %then %let interval=second;
%let time_diff=%sysfunc(intck(&interval, &modate, &curr_datetime));
%put The time difference is &time_diff &interval.s;
%mend time_diff;

So you could get the difference in minutes or hours.

1    %macro time_diff(curr_datetime, modate, interval);
2    %local time_diff;
3    %if 0=%length(&interval) %then %let interval=second;
4    %let time_diff=%sysfunc(intck(&interval, &modate, &curr_datetime));
5    %put The time difference is &time_diff &interval.s;
6    %mend time_diff;
7
8    %let curr_datetime = 22NOV2024:09:23:33;
9    %let modate = 22NOV2024:05:55:55;
10   %time_diff("&curr_datetime"dt,"&modate"dt);
The time difference is 12458 seconds
11   %time_diff("&curr_datetime"dt,"&modate"dt,minute);
The time difference is 208 minutes
12   %time_diff("&curr_datetime"dt,"&modate"dt,hour);
The time difference is 4 hours
13   %time_diff("&curr_datetime"dt,"&modate"dt,dtday);
The time difference is 0 dtdays
Ksharp
Super User
%let curr_datetime = 22NOV2024:09:23:33;
%let modate = 22NOV2024:05:55:55;



%let time_diff=%sysevalf("&curr_datetime"dt-"&modate"dt);
%put The time difference is &time_diff seconds;
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
  • 4 replies
  • 1262 views
  • 4 likes
  • 5 in conversation