Sunday, May 27, 2012

Scheduling SAS jobs using windows scheduler

Windows scheduler to run SAS programs
For PC SAS installation

Objective: The objective of this post is to give the readers running periodic jobs an initial introduction on scheduling jobs using windows scheduler and make their lives easy by making the jobs run without any human intervention.

What is a windows scheduler?
A windows scheduler is a facility in windows which can be used to schedule various jobs (not only SAS but almost any application installed on the system) to run at specific times or to run periodically at the dates mentioned.
It has a wizard for scheduling jobs which is very intuitive and you’ll not need any help even when you are using it the first time.

Step by step process to schedule a job?

So here I will be discussing a step by step process which you can follow to schedule a SAS job. Of course there a multiple other ways which will be discussed in separate posts. This process will take you through the process with visual so that you can go ahed and schedule the job on your own. Then we can discuss certain specifics :

STEP 1) Create a batch file:
A batch file is a file which can be used execute certain jobs. Though the names looks intimidating, it is very easy to create one.

To create a batch file used to run one or many SAS jobs, open notepad, type the path of the SAS software then – SYSIN and then the path of the SAS program which you want to execute using windows scheduler.

Your file content will look something like this :
“C:\Program Files\SAS\SASFoundation\9.2(32-bit)” –SYSIN “C:\my_progs\codes\”

For multiple programs you will have multiple rows like this with the path of the code changed.

The path of SAS software can be different for different installations.

To find it : Go to SAS icon -> right Click -> Properties -> Shortcut tab -> open file location

This will take you to respective folder and you can copy and paste the address.

Path of program you’ll know as you have written it J
SYSIN option specifies to start SAS and submit the file in batch mode. 

When you file is complete save it as ABC.BAT or you can rename a .txt file to .BAT also.

SO now you have your batch file.

STEP 2) Schedule the job using windows scheduler:

Go to Start -> Control panel -> Scheduled tasks (In windows NT and Xp)

For windows 7 the process is same only the look and feel is different.

Go to Start -> Control panel -> Administrative tools -> Task scheduler.(Windows 7)

I’ll tell the process further for windows XP for windows 7 also it is the same with a different look and feel but it is intuitive.
Double click on ADD SCHEDULED TASK
A window will open with a list of software, no need to do anything with the list, just click browse and specify you batch file in it.

Press next, it will ask you for timings when you want to run the task. You can specify something like Weekdays the a list will open for selecting which weekdays you want to run or else you can just select if it is an everyday task and has to be run on weekends as well.

Press next, it will ask for your windows logon password. It will be used when your system is locked to run the job. Just give the task and press FINISH.

And WOW !!  your task is scheduled. It will run at specified times without your intervention.

STEP 3) Points to keep in mind:

1) Your program should be self sufficient. That means when you generally run that program you just press F3 and do nothing else like changes in code etc.

The reason being, that if the job requires human intervention then the original purpose of scheduling is defeated.
IF your program is not self sufficient the make it..SAS is powerful !!

Taking a simple example, you have to change the date in the program to today date before running it, the use today function and make the date selection dynamic.

If you have to do some formatting in the excel sheet your programs generates before sending mail, create a permanent VB macro and call it in the program which automatically does the formatting.

If you have to send the results generated in the program by mail, use SAS mailing facility (you can also send attachments)
The only point is if you are not in office on a particular day the program should still do the complete job.

2) Use proc printto to save logs and mailing facility to send those to you through mail so that you can check them directly without even going to the job location.

3) TO run the jobs consecutively you just have to specify multiple rows in the file as stated above.
But if the programs are independent and can be run concurrently then each row in your batch file should start with

Start “Job1” “C:\Program Files\S… <same as above>
Start “Job2” “C:\Program Files\S… <same as above>

Job1 and Job2 are just any names and are required in the syntax. SO you can use anything, no dependencies.
The above syntax in your batch file will run your jobs concurrently.

Further reading:
The below paper I found on net describes the same process with graphics and great detail and will be useful:

Conclusion: So after reading this paper I hope the readers can use windows scheduler to save time and energy and free their mind from the tension of forgetting to run a job and can spend that extra time with their family.

Will be back with some more SAS magic. Goodbye Till then.

Saurabh Singh  Chauhan
Note: Comments and suggestions are always welcome.

Disclaimer :
SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute the USA and other countries. ® Indicates USA registration.
Other brand and product names are registered trademarks or trademarks of their respective companies. 
The contents of this post are the works of the author(s)and do not necessarily represent the opinions,recommendations, or practices of any organization whatsoever.