Chapter Contents

Previous

Next
SAS Companion for UNIX Environments

Executing Operating System Commands from Your SAS Session

You can execute UNIX commands from your SAS session either asynchronously or synchronously. When you run a command as an asynchronous task, the command executes independently of all other tasks that are currently running. To run a command asynchronously, you must use the SYSTASK statement. See SYSTASK for information on executing commands asynchronously.

When you execute one or more UNIX commands synchronously, then you must wait for those commands to finish executing before you can continue working in your SAS session. You can use the CALL SYSTEM routine, %SYSEXEC macro program statement, X statement, and X command to execute UNIX commands synchronously. The CALL SYSTEM routine can be executed with a DATA step. The %SYSEXEC macro statement can be used inside macro definitions, and the X statement can be used outside of DATA steps and macro definitions. You can enter the X command on any SAS command line. See CALL SYSTEM and Macro Statements for more information.


Executing a Single UNIX Command

To execute only one UNIX command, you can enter the X command, X statement, CALL SYSTEM routine, or %SYSEXEC macro statement as follows:

X command
X command;
CALL SYSTEM ('command');
%SYSEXEC command;

Note:   When you use the %SYSEXEC macro statement, if the UNIX command you specify includes a semicolon, you must enclose the UNIX command in a macro quoting function. Refer to SAS Macro Language: Reference for more information on quoting functions.  [cautionend]

When you specify only one command, the SAS System checks to see whether the command is cd, pwd, or setenv and, if so, executes the SAS equivalent of these commands. The SAS cd and pwd commands are equivalent to their Bourne shell counterparts. The SAS setenv command is equivalent to its C shell namesake. These three commands are built into the SAS System because they affect the environment of the current SAS session. When executed by the SAS System, they affect only the SAS environment and the environment of any shell programs started by the SAS session. They do not affect the environment of the shell program that began your SAS session.

If the command is not cd, pwd, or setenv, SAS starts a shell(footnote 1) in which it executes the command that you specified.

For example, you can use the X statement to execute the ls UNIX command (in a child shell) as follows:

x ls -l;

Inside a DATA step, you could use the CALL SYSTEM routine to execute cd command, which will change the current directory of your SAS session:

data _null_;
call system ('cd /users/smith/report');
run;
The search for any relative (partial) filenames during the SAS session will now begin in the /users/smith/report directory. When you end the session, your current directory will be the directory in which you started your SAS session.


Executing Several UNIX Commands

You can also use the X command, X statement, CALL SYSTEM routine, and %SYSEXEC macro statement to execute several UNIX commands:

X 'command-1;...command-n'
X 'command-1;...command-n';
CALL SYSTEM ('command-1;...command-n' );
%SYSEXEC quoting-function(command-1;...command-n);
Separate each UNIX command with a semicolon (;).

Note:   When you use the %SYSEXEC macro statement to execute several UNIX commands, because the list of commands uses semicolons as separators, you must enclose the string of UNIX commands in a macro quoting function. Refer to SAS Macro Language: Reference for more information on quoting functions.  [cautionend]

When you specify more than one UNIX command (that is, a list of commands separated by semicolons), the SAS System passes the entire list to the shell and does not check for the cd, pwd, or setenv commands, as it does when a command is specified by itself (without semicolons).

For example, the following code defines and executes a macro called pwdls that executes the pwd and ls -l UNIX commands:

%macro pwdls;
%sysexec %str(pwd;ls -l);
%mend pwdls;
%pwdls;
This example uses %str as the macro quoting function.


Starting a Shell

If you are not running in the SAS windowing environment, you can start a shell by not specifying any UNIX commands in the X statement:

X;

The SAS System responds with

Enter 'exit' to return to your SAS session.
SAS then starts a shell.

Enter any UNIX commands. When you are ready to return to the SAS session, enter the exit command.

Even if you changed directories while in the shell, you will be in the same directory as when you started the shell.


Executing X Statements in Batch Mode

If you run your SAS program in batch mode and if your operating system supports job control, the program will be suspended when an X statement within the program needs input from the terminal.

If you run your SAS program from the batch queue by submitting it with the at or batch commands, SAS processes any X statements as follows:


FOOTNOTE 1:  The shell used depends on the SHELL environment variable. [arrow]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.