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" on page 305 for information about 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 Routine" on page 239 and "Macro Statements in UNIX Environments" on page 265 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 ;


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.

Example 1: Executing a UNIX Command 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 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" on page 239.

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 [1] in which it executes the command that you specified. 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" on page 15.

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 (;).


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.

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" on page 14.

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 ;

SAS 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.

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 the file permissions from within SAS, issue the umask mask command on the X statement. The umask mask command sets the permissions for any new file that you create. The value of mask can be either numeric or symbolic. For more information about this command, see the man page for umask)

When SAS executes the umask mask command, it changes the file permissions of the current SAS session, but it does not change the permissions in the parent shell. Any subsequent file that you create during this SAS session will inherit the permissions that you specified.

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.

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

SAS 9.1 Companion for UNIX Environments
SAS 9.1 Companion For Unix Enivronments
ISBN: 1590472101
EAN: 2147483647
Year: 2004
Pages: 185
Authors: SAS Institute

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net