Hi All,
I would like to run my SAS code using (%include command) every minute at, say, 5th second.
(The sleep option seems not working for me since I don't know how long the code runs)
Can you please help?
Thanks
HHC
%Include "E:\SAS code.sas";
@hhchenfx wrote:
I am thinking about a macro that runs the code. Keep track of the Time it ends (time-end). Then calculate the gap time betwwen time-end and the next minutes and 5 second.
This gap time will be put into sleep
This way, Sas will resume and run the next round right at mm:05 ( or around that time).
I might break my question into piece if this above approach works.
Thanks.
Hhc
That's how I was thinking you could use the WAKEUP function example I posted. But instead of calculating the gap, you just tell SAS to wake up at 5 seconds after the start of the next minute. Something like:
%macro doeveryminute() ;
%*beware the infinite loop ;
%do %while (1) ;
*do something ;
title1 "Current Time: %sysfunc( time(), time )" ;
proc print data=sashelp.class ;
run ;
title1 ;
%*Sleep until 5 seconds after the start of the next minute ;
%put %sysfunc(wakeup("%sysfunc(putn(%eval(%sysfunc(intnx(minute,"%sysfunc( time(), time )"t,1))+5),time.))"t));
%end ;
%mend ;
%doeveryminute()
Scheduling of tasks is usually done in the operating system. For example, Windows has an applicatoin called Task Scheduler that can cause your SAS code to be run at any time or at any frequency.
Running a batch program every minute is going to produce 60 SAS logs per hour and 1,440 per day. A looping program would be better but you need to check how long each run takes. Why does it need to be run so often?
As Paige said, scheduling via the OS (task scheduler on Windows, crontab on linux) is usually the best approach.
Since you mention the SLEEP() function, note that SAS on Windows has a WAKEUP() function which allows you to wake at a certain time. If you wanted to say "wake me at 5 seconds past the next minute," you could do something ugly like:
*Check the time to wakeup, 5 seconds after the start of the next minute ;
%put %sysfunc(putn(%eval(%sysfunc(intnx(minute,"%sysfunc( time(), time )"t,1))+5),time.)) ;
*Sleep until 5 seconds after the start of the next minute ;
%put %sysfunc(wakeup("%sysfunc(putn(%eval(%sysfunc(intnx(minute,"%sysfunc( time(), time )"t,1))+5),time.))"t));
So you would put that inside an infinite loop which runs your code, and then sleeps.
But I only mention that because you mentioned the SLEEP function. Using OS scheduling tools is much much better.
The simple scheduling daemon cron, used on UNIX systems, can break down to minutes. If the 5 seconds are crucial, run the SLEEP function as first action of your code. But keep in mind that initiating a SAS session already takes time (where I used to work, pre-assigning all metadata bound libraries took about half a minute).
@hhchenfx wrote:
I am thinking about a macro that runs the code. Keep track of the Time it ends (time-end). Then calculate the gap time betwwen time-end and the next minutes and 5 second.
This gap time will be put into sleep
This way, Sas will resume and run the next round right at mm:05 ( or around that time).
I might break my question into piece if this above approach works.
Thanks.
Hhc
That's how I was thinking you could use the WAKEUP function example I posted. But instead of calculating the gap, you just tell SAS to wake up at 5 seconds after the start of the next minute. Something like:
%macro doeveryminute() ;
%*beware the infinite loop ;
%do %while (1) ;
*do something ;
title1 "Current Time: %sysfunc( time(), time )" ;
proc print data=sashelp.class ;
run ;
title1 ;
%*Sleep until 5 seconds after the start of the next minute ;
%put %sysfunc(wakeup("%sysfunc(putn(%eval(%sysfunc(intnx(minute,"%sysfunc( time(), time )"t,1))+5),time.))"t));
%end ;
%mend ;
%doeveryminute()
Commercial job schedulers (Autosys, Control M) have provision assign both time and dependency.
You can explore if that works in your case.
Yeah!
Thanks a lot.
It works perfectly.
HHC
%macro doeveryminute() ;
%do %while ( %sysevalf(%sysfunc(time())<'21:56:08't,boolean) ) ;
*do something ;
data _null_;
dt=today(); format dt yymmdd10.;
curr_datetime=datetime(); format curr_datetime datetime20.;
put dt ;
put curr_datetime;
run;
%*Sleep until 5 seconds after the start of the next minute ;
%put %sysfunc(wakeup("%sysfunc(putn(%eval(%sysfunc(intnx(minute,"%sysfunc( time(), time )"t,1))+5),time.))"t));
%end ;
%mend ;
%doeveryminute()
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.