Running SAS Noninteractively Using Command Files ("batch" mode)
A convenient alternative to using the SAS Display Manager System (DMS) is to place the commands you wish to execute in a text file and execute SAS using the name of this command file as an "argument" to the sas command. For example, suppose your command file is named dataprep.sas, then you can run the SAS job by typing this command at the strauss prompt:
sas dataprep.sas
More generally, run a batch job by typing:
sas <command_file>
at the strauss prompt. Substitute the name of your command file for <command_file>. As illustrated above, you may omit the extension if it is .sas.
You may use a UNIX editor to create a file containing the commands you want SAS to execute. For example, to use the pico editor to create a command file called dataprep.sas, type:
pico dataprep.sas
at the UNIX prompt. Type the SAS commands you want to execute and save the file. Then run sas with dataprep.sas as the command-line argument, as just illustrated, e.g.:
sas dataprep
For this example, an annotated copy of your commands is written to the SAS log file called dataprep.log, and results of SAS procedures are written to the SAS listing file called dataprep.lst. You may view these two files on the screen with the UNIX pager program called more. To view the output, type:
more dataprep.log more dataprep.lst
at the UNIX prompt.
In general, an annotated copy of your SAS commands are written to the SAS log. The the name of the SAS log is formed by using the root part of your command file name and log as the extension. Similarly, the name of the SAS listing file is formed by adding an extension of lst to the root part of the name of your command file.
A convenient way to run batch SAS jobs is to open your command file (.sas) in pico (or another editor) in one terminal window. Save changes without exiting pico. Run the SAS job in another terminal window and view the output there, or in a third window. You then can look at error messages in the saslog file beside your syntax and make needed changes while viewing the error messages. Also, to view different parts of your output side-by-side, open two or more terminal windows and view the file in each one using a pager such as more or less.
Note the analogies between the files used in SAS batch runs and the interactive SAS using the DMS:
Batch | DMS |
---|---|
command file, extension .sas | Program Editor Window |
saslog, extension .log | SAS Log Window |
sas listing, extension .lst | SAS Output Window |
To print these files at the Smith Hall network printers, type
qpr -q smips dataprep.log qpr -q smips dataprep.lst
You should print both the log and listing files and keep them together, so you can check to see what commands generated the output.
To print the files somewhere besides Smith Hall, substitute the name of the local printer queue for smips. Printer queue names are posted at computing sites. Or you can find them on the Computing Sites Web page.
Example 1: Reading data inclued in the command file
This example illustrates a batch-SAS run (rather than an interactive session). Note that batch SAS runs from any terminal, such as secure shell. You do not need a graphical X-window interface such as a Sun Ray, Windows computer running Xming or Cygwin or MAC with OS X. Also, batch runs are not affected by a slow connection. Therefore, you can run SAS from off-campus without need to install specialiazed software such as Xming.
If you have a small amount of data, it is convenient to put it in the same file that contains your SAS commands. This example shows how to do that. Suppose the name of your command file is income1.sas and if you typed the following lines into it:
options linesize=80 noovp; * Read data, set missing values for persinc *; data income; input gender race persinc; if persinc = 99 then persinc=.; datalines; 1 1 20 1 1 99 1 1 13 2 1 1 1 1 10 2 2 5 2 1 12 1 1 7 2 1 99 2 2 2 ; run; * Produce descriptive statistics *; proc means data=income; run;
using an editor such as pico, vi or emacs.
Note that the commands beginning with an asterisk (*) [and ending with a semicolon (;)] are comments. The lines beginning with data income; and ending with run; read the data and produce a temporary SAS data set called income. The proc means statement produces the descriptive statistics. The means procedure reads the data from the temporary SAS data set, income.
The first statement in this command file is an options statement. In this example, it instructs SAS to restrict the width of the output files to 80 characters or less. The 80-character width prevents lines from wrapping on a standard-width screen. The noovp option stands for "no overprint." Without it, diagnostic messages in the sas log output are not properly aligned and are therefore difficult to read.
Note the use of the datalines statement. It signifies to SAS that the data start on the next line. The isolated semicolon following the last line of data signifies the end of the data. (datalines is an alias for the cards statement.)
To run the program in batch mode on UNIX, type
sas income1
at the UNIX prompt. The results are written to two files. The SAS log is written to the UNIX file called income1.log. The SAS listing is written to the UNIX file called income1.lst. To view the output, type:
more income1.log more income1.lst
at the UNIX prompt. You can speed up the viewing by getting both files at once and piping them into more:
cat income1.l?? | more
The cat command lists the files to the terminal screen, by default. The pipe symbol (|) redirects the output into the pager, more. This prevents the output from scrolling by so fast you can't read it.
To print these files at the Smith Hall network printers, type
qpr -q smips income1.log qpr -q smips income1.lst
You should print both the log and listing files and keep them together, so you can check to see what commands generated the output.
To print the files somewhere besides Smith Hall, substitute the name of the local printer for smips. Printer names are posted at computing sites, or you can find them online.
Example 2: Reading data from an external file (UNIX batch run)
This example illustrates a batch-SAS run (rather than an interactive session).
The name of the command file for this example is
income2.sas
It shows how to use the infile statement to read data from an external data file and produce descriptive statistics. The data are stored in the external file named income.data.
The new command file looks like:
options linesize=80 noovp; * Read data, set missing values for persinc *; data income; infile 'income.data'; input gender race persinc; if persinc = 99 then persinc=.; run; * Produce descriptive statistics *; proc means data=income; run;
Note that statements beginning with an asterisk (*) and ending with a semicolon (;) are comments.
The first statement in this command file is an options statement. It instructs SAS to restrict the width of the output files to 80 characters or less. The 80-character width prevents lines from wrapping on a standard-width screen. The noovp option stands for no overprint. Without it, diagnostic messages in the sas log output are not properly aligned and are therefore difficult to read.
The lines beginning with data income; and ending with run; read the data and produce a temporary SAS data set called income. The proc means statement produces the descriptive statistics. The means procedure reads the data from the temporary SAS data set, income.
The data file is income.data. Its contents look like
1 1 20 1 1 99 1 1 13 2 1 1 1 1 10 2 2 5 2 1 12 1 1 7 2 1 99 2 2 2
To run the program type
sas income2
at the UNIX prompt. The results are written to two files. The SAS log file is
income2.logand the SAS listing file is
income2.lstTo view these files on your screen, type
more income2.log more income2.lst
at the UNIX prompt. You can speed up the viewing by getting both files at once and piping them into more.
cat income1.l?? | more
The cat command shows the files on the terminal screen by default. The pipe symbol (|) redirects the output into the pager, more. This prevents the output from scrolling by so fast you can't read it. The question marks after .l are "wild-card" characters. Each question mark stands for any single character. (Use an asterisk ( *) to stand for any one or more characters.)
To print these files at the Smith Hall network printers, type
qpr -q smips income2.log qpr -q smips income2.lst
You should print both the log and listing files and keep them together, so you can check to see what commands generated the output.
To print the files somewhere besides Smith Hall, substitute the name of the local printer for smips. Printer names are posted at computing sites. Or you can find them on the Computing Sites Web page.
Efficient batch SAS work
You can speed up working with batch SAS by openening at least two windows on strauss and using some UNIX shortcuts. In one window, open your command file with pico or another editor. Don't exit the editor when you are finished typing commands or making modifications. Instead, save the file while keeping the editor open. In pico, you can save your file by pressing
^O(the CONTROL key and the letter O) simultaneously. You are prompted to verify the file name; press ENTER.
Run the job in the other terminal window. Suppose your command-file name is dataprep.sas. You can gang together commands to run the job and view all the output into one line, like this
sas dataprep; cat dataprep.l?? | less
The semicolon on a UNIX command line
separates one command from the next.
Using it allows you to execute more
than one command from a single line.
The advantage of doing this here is
that the next times you want to run the
same job and view the output, you can
use the UNIX repeat shortcut, double
exclamation point (!!) repeats the most
recent command line input.
This example pipes the contents of dataprep.log and dataprep.lst into a UNIX utility pager named less. This pager is a little more convenient than the one named more referenced previously. It does not automatically exit when you reach the end of the input. The automatic exit can be a real nuisance when you want to go back through the file. It also supports easy searches and precise positioning of the contents in the terminal windows.
To maneuver through a file using less, use the following keys:
q | quit |
SPACEBAR, d | Down one full screen |
b | Back one full screen |
Down Arrow, ENTER, j | Down one line |
Up Arrow, k | Up one line |
G | Go to bottom of the file |
g | Go to top of the file |
/<string> | Search forward for "<string>" |
?<string> | Search backward for "<string>" |
n | Repeat most recent search going in the same direction |
N | Repeat most recent search going in the opposite direction |
h | Help (q> to exit help) |
-i | Toggle case sensitive. First use: ignore case differences in searches. Second use: honor case differences again. |
Keys on the same line in column 1 separated by a comma are alternative keys for the same operation. You can go down one screen by pressing the SPACEBAR or the letter d, for instance. Some connections to strauss may not support use of the up and down arrows. If they don't work, use the k and j keys, respectively.
The search operation is particularly fast, and repeating a search is even faster. A useful technique is to set the flag to ignore cases in searches (-i) and search for ^error, like this:
/^errorSAS error diagnostics start in the first character (column) on a line. Using the carat character in the search indicates that you wish to ignore errors that appear elsewhere, for example, in regression output. To find the next error message, press the letter
n (lower case).If you want to look at two (or more) locations in a file at the same time, open another terminal window on strauss and view the file in both (all) of them using a pager like less.