BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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";

 

 

 

0 Likes
1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Amethyst | Level 16

@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()

 

Boston Area SAS Users Group is hosting monthly FREE webinars!
Check our upcoming events: https://www.basug.org/events
Next up: Rick Wicklin presents The Essential Guide to Bootstrapping in SAS, on November 9.

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SASKiwi
Opal | Level 21

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?

0 Likes
Quentin
Amethyst | Level 16

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.

 

 

Boston Area SAS Users Group is hosting monthly FREE webinars!
Check our upcoming events: https://www.basug.org/events
Next up: Rick Wicklin presents The Essential Guide to Bootstrapping in SAS, on November 9.
Kurt_Bremser
Super User

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).

0 Likes
hhchenfx
Barite | Level 11
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
0 Likes
Quentin
Amethyst | Level 16

@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()

 

Boston Area SAS Users Group is hosting monthly FREE webinars!
Check our upcoming events: https://www.basug.org/events
Next up: Rick Wicklin presents The Essential Guide to Bootstrapping in SAS, on November 9.
Sajid01
Ammonite | Level 13

Commercial job schedulers (Autosys, Control M) have provision assign both time and dependency.
You can explore if that works in your case.

0 Likes
hhchenfx
Barite | Level 11

Yeah!

Thanks a lot.

It works perfectly.

HHC

0 Likes
hhchenfx
Barite | Level 11
%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()
0 Likes
JUST RELEASED

SAS Viya with pay-as-you-go pricing.

Deploy software automatically at the click of a button on the Microsoft Azure Marketplace.

Learn more

55 percent discount.jpg

Recommended by SAS
These recommendations are generated using AI from SAS. For personalized recommendations, sign in with your SAS profile.
Discussion stats
  • 9 replies
  • 215 views
  • 6 likes
  • 6 in conversation