| SAS Companion for the Microsoft Windows Environment |
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. ![[cautend]](../common/images/cautend.gif)
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 directory to MYDATA at the command prompt, the SAS System still uses the SAS directory as its working directory. 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:
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 directories. After the DATA step executes, three directories have been created: C:\MYDIR,
C:\JUNK2, and C:\XYZ. The C:\MYDIR2 directory is not created because the value of FLAG for that observation is not
Y.
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 directory is created regardless of whether the value of ANSWER is equal to
'n' or
'y'.
For more information about the CALL SYSTEM routine, see CALL SYSTEM and the section on the CALL
SYSTEM routine in SAS Language Reference: Dictionary.
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:
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 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.
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:
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.
The XWAIT and XSYNC system options have very different effects. An easy way to
remember the difference is
the following:
The various option combinations are summarized in Combining the XWAIT and XSYNC System Options.
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.