Hi,
My main macro will run every minute and inside that code, I want to run a small code just one time when time in between 15:00 - 15:01.
Can you please help me to do it?
I thought the one below will do the job but it doesn't.
Thanks,
HHC
%macro try;
%if 15:00:00 < %sysfunc(time(),.0) < 15:02:00 %then %do;
data a; set _NULL_;
run;
%end;
%else %do;
data b; set _NULL_;
%end;
%mend;
%try;
I think this may work:
%macro try;
%local now;
%let now=%sysfunc(time(),time8.);
%if "&now">"15:00:00" and "&now"<"15:02:00" %then %do;
Two points:
Set OPTIONS MPRINT; run your macro and share the result from the log.
Note: "I thought the one below will do the job but it doesn't." is not very informative.
I suspect, if that is your code that you posted is correct that one of two things is happening: you are getting a ERROR because the statement: time8 run; doesn't make any sense
216 data a; set _NULL_;time8 ----- 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 217 run;
OR that the second bit is running because you are missing a RUN to end the data step. Until a data step boundary occurs in the second case the data step is "running" but not finished.
Macro language treats everything as character so when you compare
15:00:00 < %sysfunc(time(),.0)
You are comparing a string of 15:00:00 to a different string and you need to address what the %sysfunc i actually returning (Hint: remove the format as length 0 formats don't work well for anything). If the time actually were 15:00:00 the result of %sysfunc(time()) would be 54000. When you place that into a .0 format you get an * as SAS can't fit that into that format at all. Maybe you though that TIME8 was on a different line???
Note that any data step or procedure inside a macro really really needs an explicit RUN; as the timing for when a following boundary may be very problematic.
@hhchenfx wrote:
Hi,
My main macro will run every minute and inside that code, I want to run a small code just one time when time in between 15:00 - 15:01.
Can you please help me to do it?
I thought the one below will do the job but it doesn't.
Thanks,
HHC
%macro try; %if 15:00:00 < %sysfunc(time(),.0) < 15:02:00 %then %do; data a; set _NULL_;time8 run; %end; %else %do; data b; set _NULL_; %end; %mend; %try;
Hi,
That time8 is a typo.
I try to convert time to second but still the comparison fail, always return "data a" even current time is 68102.7880001068 , which is outside the range of 67800-68000
31280 %macro try;
31281 %if 67800 < %sysfunc(time())<68000
31282 %then %do;
31283 data a; set _NULL_;
31284 run;
31285 %end;
31286 %else %do;
31287 data b; set _NULL_;
31288 %end;
31289 %put %sysfunc(time());
31290 %mend;
31291
31292 %try;
NOTE: The data set WORK.A has 0 observations and 0 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
68102.7880001068
So somehow I have to break that a<b<c into a<b and b<c to get the IF THEN works.
%macro try;
%if 67800 < %sysfunc(time()) AND %sysfunc(time())<78000
%then %do;
data a; set _NULL_;
run;
%end;
%else %do;
data b; set _NULL_;
%end;
%put %sysfunc(time());
%mend;
%try;
I think this may work:
%macro try;
%local now;
%let now=%sysfunc(time(),time8.);
%if "&now">"15:00:00" and "&now"<"15:02:00" %then %do;
Two points:
You code and explanation is very much helpful.
Thanks,
HHC
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.