|SAS Companion for the Microsoft Windows Environment|
You can execute DOS or Windows 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. For more information on running asynchronous commands using the SYSTASK statement, see SYSTASK.
When you execute one or more DOS or Windows 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, X statement, and X command to execute DOS or Windows commands synchronously. The CALL SYSTEM routine can be executed with a DATA step and the X statement can be used outside of DATA steps. You can enter the X command on any SAS command line.
You can submit the X statement to exit your SAS session temporarily and gain access to the Windows command processor. The X statement has the following syntax:
The optional command argument is used either to issue an operating system command or to invoke a Windows application such as Notepad. This discussion concentrates on using the X statement to issue operating system commands; however, you should be aware that the X statement can also be used to invoke Windows applications.
Note: The X statement is similar to the X command in
the SAS windowing environment. The major difference between the two is that
the X statement is submitted like any SAS statement; however, the X command
is issued as a windowing environment command. This section uses the X statement
in its examples, but the information applies to the X command as well.
If you want to execute only one operating system command, include the command as an argument to the X statement. When you submit the X statement, the command is executed, and you cannot issue any additional commands.
If you want to execute several operating system commands, submit the X statement without an argument. A command prompt appears where you can execute an unlimited number of operating system commands. Remember, however, that the commands you issue from the command prompt do not affect the SAS process. For example, if you change from the SAS working folder to MYDATA at the command prompt, the SAS System still uses the SAS folder as its working folder. Also, any environment variables you define are not available to the SAS System. If you submit an X statement or command without a command argument, type EXIT to return to your SAS session.
Other methods of temporarily exiting your SAS session include:
|Executing Operating System Commands Conditionally|
If you want to execute operating system commands conditionally, use the CALL SYSTEM routine, as in the following example:
options noxwait; data _null_; input flag $ name $8.; if upcase(flag)='Y' then do; command='md c:\'||name; call system(command); end; cards; Y mydir Y junk2 N mydir2 Y xyz ;
This example uses the value of the variable FLAG to
conditionally create folders. After the DATA step executes, three folders
have been created: C:\MYDIR, C:\JUNK2, and C:\XYZ. The C:\MYDIR2 folder is
not created because the value of FLAG for that observation is not
The X command is a global SAS statement; therefore, it is important to realize that you cannot conditionally execute the X command. For example, if you submit the following code, the X statement is executed:
data _null_; answer='n'; if upcase(answer)='y' then do; x 'md c:\extra'; end; run;
In this case, the C:\EXTRA folder is created regardless
of whether the value of ANSWER is equal to
For more information about the CALL SYSTEM routine, see CALL SYSTEM and the section on the CALL SYSTEM routine in SAS Language Reference: Dictionary.
|XWAIT System Option|
The XWAIT system option controls whether you have to type EXIT to return to your SAS session after an X statement or X command has finished executing a DOS command. (The XWAIT system option is not used if an X statement is issued without a command argument or if the X statement invokes a Windows application such as Notepad.) This option and its negative form operate in the following ways:
|XWAIT||specifies that you must type EXIT to return to your SAS session. This is the default value.|
|NOXWAIT||specifies that the command processor automatically returns to the SAS session after the specified command is executed. You do not have to type EXIT.|
When a window created by an X statement is active, reactivating the SAS System without exiting from the command processor causes the SAS System to issue a message box containing the following message:
The X command is active. Enter EXIT at the prompt in the X command window to reactivate this SAS session.
If you receive this message box, click on [MS-DOS Prompt] on the Windows Task Bar. Enter the EXIT command from the prompt to close the window and return to your SAS session.
|XSYNC System Option|
The XSYNC system option specifies whether the operating system command you submit executes synchronously or asynchronously with your SAS session. This option and its negative form operate in the following ways:
|XSYNC||specifies that the operating system command execute synchronously with your SAS session. That is, control is not returned to the SAS System until the command has completed. You cannot return to your SAS session until the command prompt session spawned by the X command or statement is closed. This is the default.|
|NOXSYNC||specifies that the operating system command execute asynchronously with your SAS session. That is, control is returned immediately to the SAS System and the command continues executing without interfering with your SAS session. With NOXSYNC in effect, you can execute an X command or X statement and return to your SAS session without closing the window spawned by the X command or X statement.|
Specifying NOXSYNC can be useful if you are starting applications such as Notepad or Excel from your SAS session. For example, suppose you submit the following X statement:
If XSYNC is in effect, you cannot return to your SAS session until you close the Notepad. But if NOXSYNC is in effect, you can switch back and forth between your SAS session and the Notepad. The NOXSYNC option breaks any ties between your SAS session and the other application. You can even end your SAS session; the other application stays open until you close it.
|Comparison of the XWAIT and XSYNC System Options|
The XWAIT and XSYNC system options have very different effects. An easy way to remember the difference is the following:
|XWAIT||means the command prompt session waits for you to type EXIT before you can go back to your SAS session.|
|XSYNC||means the SAS System waits for you to finish with the other application before you can go back to your SAS session.|
The various option combinations are summarized in Combining the XWAIT and XSYNC System Options.
|Options in Effect||Result|
|The command prompt window waits for you to type EXIT before closing, and the SAS System waits for the application to finish.|
|The command prompt window waits for you to type EXIT before closing, and the SAS System does not wait for the application to finish.|
|The command prompt window closes automatically when the application finishes, and the SAS System waits for the application to finish.|
|The command prompt window closes automatically when the application finishes, and the SAS System does not wait for the application to finish.|
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.