Executing Operating System Commands from Your SAS Session

Deciding Whether to Run an Asynchronous or Synchronous Task

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 Statement: UNIX for information about executing commands asynchronously.
When you execute one or more UNIX commands synchronously, 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. For more information, see CALL SYSTEM Routine: UNIX and Macro Statements in UNIX Environments.

Executing a Single UNIX Command

Single Commands

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 that you specify includes a semicolon, you must enclose the UNIX command in a macro quoting function. For more information about quoting functions, see SAS Macro Language: Reference.

Example 1: Executing a UNIX Command by Using the X Statement

You can use the X statement to execute the ls UNIX command (in a child shell) as follows:
x ls -l;

Example 2: Executing a UNIX Command by Using the CALL SYSTEM Routine

Inside a DATA step, you can use the CALL SYSTEM routine to execute a 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.
For more information about the CALL SYSTEM routine, see CALL SYSTEM Routine: UNIX.

How SAS Processes a Single UNIX Command

When you specify only one command, SAS checks to see whether the command is cd, pwd, setenv, or umask 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. The SAS umask command is equivalent to the numeric mode of the umask command supported by the Bourne, Korn, and C shells. These four commands are built into SAS because they affect the environment of the current SAS session. When executed by SAS software, 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 in which it executes the command that you specified. The shell that is used depends on the SHELL environment variable. If the command is umask, but you do not specify a mask, then SAS passes the command to the shell in which the current SAS session was started. For more information about the umask command, see Changing the File Permissions for Your SAS Session.

Executing Several UNIX Commands

Executing 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. For more information about quoting functions, see SAS Macro Language: Reference.

Example: Executing Several Commands Using the %SYSEXEC Macro

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.

How SAS Processes Several UNIX Commands

When you specify more than one UNIX command (that is, a list of commands separated by semicolons), SAS passes the entire list to the shell and does not check for the cd, pwd, setenv, or umask commands, as it does when a command is specified by itself (without semicolons).
For more information about how SAS processes the cd, pwd, setenv, or umask commands, see How SAS Processes a Single UNIX Command.

Changing the File Permissions for Your SAS Session

At invocation, a SAS session inherits the file permissions from the parent shell. Any file that you create will inherit these permissions. If you want to change or remove file permissions from within SAS, issue the following command in the X statement: umask. The umask command applies a new “mask” to a file, that is, it sets new file permissions for any new file that you create. In this way, the umask command can provide file security by restricting access to new files and directories for the current process.
The default value for umask varies. Some systems, like Secure Linux, use a default of 220. Other systems use 022 as the default. System administrators can set their own default value and you can check your default and change it in your own .kshrc, .cshrc, or .profile files. These values affect all child processes that are executed in the shell. Any subsequent file that you create during the current SAS session will inherit the permissions that you specified. The permissions of a file created under a given mask are calculated in octal representation.
Note: The value of a mask can be either numeric or symbolic. For more information about this command, see the UNIX man page for umask.

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:
  • If the X statement does not specify a command, SAS ignores the statement.
  • If any UNIX command in the X statement attempts to get input, it receives an end-of-file (standard input is set to /dev/null).
  • If any UNIX command in the X statement writes to standard output or standard error, the output is mailed to you unless it was previously redirected.