Return to Homepage

Goto the Tip of the Month Archive

Other interesting pages ...
LinkedIn Profile
SAS Cheat Sheet
Useful SAS Code
Full SAS Example
Basic Statistics
Contact Information

SAS Tip of the Month
April 2013
(for SAS and WPS)

Often I have a number of SAS programs to run so I create a batch file in Windows and then run that batch file. But how do you create that batch file? As nearly always, there is more than one way to do it.

Lets first look at a SAS program which reads in a directory listing of SAS programs (those with the extension .SAS) and then creates a batch file with all the programs.

The first step of the process is to switch on the option NOXWAIT and set the directory we are interested in running the programs, as shown below:

   %let dir=%str(directory_being_processed);
   options NOXWAIT;

An example of such a directory_being_processed value could be

   C:\TUAI\LISTINGS

Next step is to create a list of .SAS files and put that in the directory_being_processed directory with the file name of TMP.TXT -- these are in name order, using the option '/o:n' and exclude the directory name in front of the file using the option '/b'.

   data _null_;
      length _txt $200;
      _txt='dir /b /o:n "'||strip("&dir")||'\*.sas" > "'||strip("&dir")||'\tmp.txt"';
      put _txt=;
      call system(_txt);
   run;

The following step reads in the names of the SAS programs.

   data _lst0;
      length fn $200;
      infile "&dir.\tmp.txt";
      input fn;
      fn=upcase(FN);
   run;

In the above step it is possible to have file name selection clauses after the last statement to exclude any programs that you do not want added to the batch file.

The last step creates the batch file, with the name of the file being specified in the FILE statement:

   data _null_;
      file "&dir.\RUNALL.bat";
      length logfn txt $200;
      set _lst0 end=eof;
      if _n_=1 then do;
         put "set SASLOC=C:\Program Files\SAS\SAS 9.1";
         txt='set PRJLOC='||strip("&dir");
         put txt;
         txt=scan("&dir",1,"\");
         put txt;
         put 'CD %PRJLOC%';
         put 'DEL /Q *.LOG';
         put 'DEL /Q *.LST';
      end;
      txt='"%SASLOC%\sas.exe" -rsasuser -noicon -nosplash '||
          '-nostatuswin -nologo -SYSIN "%PRJLOC%\'||strip(fn)||'"';
      put txt;
      if eof then put 'EXIT';
   run;

It is important to note that the line

   put "set SASLOC=C:\Program Files\SAS\SAS 9.1";

sets the location of the SAS.EXE file which is different for site, operating system and SAS version -- the SET statement is a DOS command that allows a DOS variable to contain a value. A similar task is done in the next line

   txt='set PRJLOC='||strip("&dir");

where the location of the directory being processed is being set.

The following lines will set the drive where your programs are and location for the programs to run:

   txt=scan("&dir",1,"\");
   put txt;
   put 'CD %PRJLOC%';

Lastly, any LOG and LST files in the directory are delected using the DOS DEL command:

   put 'DEL /Q *.LOG';
   put 'DEL /Q *.LST';

Note that as a rule I delete any LOG files before I run a SAS program just in case the (rare) situation arises where the SAS LOG is not replaced during the run of the program.

The line

   txt='"%SASLOC%\sas.exe" -rsasuser -noicon -nosplash '||
         '-nostatuswin -nologo -SYSIN "%PRJLOC%\'||strip(fn)||'"';
   put txt;

creates the DOS SAS batch file statement that calls each program.

Finally, the

   EXIT

command closes the DOS window when the batch file is run.

If you are running WPS, the line

   put "set SASLOC=C:\Program Files\SAS\SAS 9.1";

would be relaced by the line

   put "set WPSLOC=C:\Program Files\World Programming WPS 3";

(or similar), and the line

   txt='"%SASLOC%\sas.exe" -rsasuser -noicon -nosplash '||
         '-nostatuswin -nologo -SYSIN "%PRJLOC%\'||strip(fn)||'"';

would be replaced by the lines

   logfn=tranwrd(upcase(fn),".SAS",".log");
   txt='"%WPSLOC%\bin\wps" "%PRJLOC%\'||strip(fn)||
       '" > "%PRJLOC%\'||strip(logfn)||'"';

Hope this is useful. Have a safe and happy April.

________________________________
Updated April 06, 2013