Running Windows or MS-DOS Commands from within SAS

Overview of Running Windows or MS-DOS Commands from within SAS

You can execute Windows or MS-DOS commands from within SAS by using the X statement or the X command. You can also use the CALL SYSTEM statement or the SYSTASK statement from within a DATA step. Windows or MS-DOS commands can be issued 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. When you run a command as a synchronous task, the command must complete before another task can run.
To issue a command asynchronously, use either the SYSTASK statement with the NOWAIT option or specify the NOXSYNC system option. To issue a command synchronously, use either the SYSTASK statement with the WAIT option or specify the XSYNC system option. For more information about running asynchronous commands using the SYSTASK statement, see SYSTASK Statement: Windows .
These language elements can be used to run code using Windows or MS-DOS commands:
  • X statement
  • X command
  • CALLSYSTEM routine
  • %SYSEXEC statement.

Running Windows Commands Using the X Statement or the X Command

You can use the X statement or the X command to run Windows commands. The X statement can be run outside of a DATA step. You can enter the X command in the command bar or any SAS command line.
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.
When you submit the X statement, you exit your SAS session temporarily and gain access to the Windows command processor. The X statement has the following syntax:
X < 'command ' >;
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.
If you want to run 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 run several operating system commands, submit the X statement without an argument. A command prompt appears where you can issue an unlimited number of operating system commands. Remember, any environment variables that you define are not available to SAS. If you submit an X statement or command without an argument command, type EXIT to return to your SAS session.
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 'n' or 'y'.

Using a DATA Step to Issue Conditional Operating System Commands Conditionally

If you want to issue 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;
   datalines;
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 Y.
For more information about the CALL SYSTEM routine, see CALL SYSTEM Routine: Windows and the section on the CALL SYSTEM routine in SAS System Options: Reference.

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 an MS-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.
If you issue an X statement or X command without a command argument, you must type EXIT to return to your SAS session, even if NOXWAIT is in effect.
When a window created by an X statement is active, reactivating SAS without exiting from the command processor causes SAS 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 Command Prompt on the Windows Taskbar. 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 that 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 SAS until the command has completed. You cannot return to your SAS session until the command prompt session spawned by the CALL SYSTEM statement, the X command, or the X 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 SAS and the command continues executing without interfering with your SAS session. With NOXSYNC in effect, you can execute a CALL SYSTEM statement, an X command, or an 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:
x notepad;
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 that the command prompt session waits for you to type EXIT before you can return to your SAS session.
XSYNC
means that SAS waits for you to finish with the other application before you can return to your SAS session.
The various option combinations are summarized in Combining the XWAIT and XSYNC System Options .
Combining the XWAIT and XSYNC System Options
Options in Effect
Result
XWAIT
XSYNC
The command prompt window waits for you to type EXIT before closing, and SAS waits for the application to finish.
XWAIT
NOXSYNC
The command prompt window waits for you to type EXIT before closing, and SAS does not wait for the application to finish.
NOXWAIT
XSYNC
The command prompt window closes automatically when the application finishes, and SAS waits for the application to finish.
NOXWAIT
NOXSYNC
The command prompt window closes automatically when the application finishes, and SAS does not wait for the application to finish.