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

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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

 

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
PROC Star

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?

Quentin
Super User

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.

 

 

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

hhchenfx
Rhodochrosite | Level 12
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
Quentin
Super User

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

 

Sajid01
Meteorite | Level 14

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

hhchenfx
Rhodochrosite | Level 12

Yeah!

Thanks a lot.

It works perfectly.

HHC

hhchenfx
Rhodochrosite | Level 12
%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()

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 9 replies
  • 2942 views
  • 6 likes
  • 6 in conversation