1

I am submitting the following code expecting to see SAS attempt one X command a time and not wait for a submission of 'exit' into Command Prompt to close:

options noxwait xsync;

data _null_;
x 'cd C:\Python33';
x 'start test.py';
run;

data _null_;
call system ('exit');
run;

data _null_;
x 'cd C:\Python33';
x 'start test.py';
run;

data _null_;
call system ('exit');
run;

data _null_;
x 'cd C:\Python33';
x 'start test.py';
run;

data _null_;
call system ('exit');
run;

data _null_;
x 'cd C:\Python33';
x 'start test.py';
run;

data _null_;
call system ('exit');
run;

data _null_;
x 'cd C:\Python33';
x 'start test.py';
run;

data _null_;
call system ('exit');
run;

Instead what is happening is that code initiates each instance of calling some Python code in sequence, but does not wait for the previous instance to finish. Is this the correct behaviour for the 'noxwait xsync' combination?

If so, I am attempting to use a work around of 'call system ('exit'). If you run the code in xwait mode you get two Command Prompt windows. One is the system administrator window and one is the one that invokes the code.

The one that invokes the code always closes itself down no matter whether noxwait or xwait is selected. If noxwait is selected all the windows eventually close themselves. In xwait mode the submission to command line of 'call system ('exit') does not close the administrator window.

Is there a way around this? I cannot just use noxwait and allow all the command prompt submissions to open and close in turn as the x commands are within nested macros and there ends up being thousands of Python calls all trying to execute at once.

Thanks

user3043997
  • 27
  • 1
  • 7
  • Btw, in the macro code there is only one instance of the x command submissions, not five. I've just used multiple ones to quickly demonstrate my point. – user3043997 Mar 12 '14 at 19:32
  • First off, `x` is used in open code, not in `data _null_` steps. Can you verify that isn't the issue? – Joe Mar 12 '14 at 19:46
  • Ah right, sorry...thought it needed to be run in a data step...ive run it in open code and it hasn't made a difference. – user3043997 Mar 12 '14 at 19:54

1 Answers1

1

XSYNC works as one would expect. However, you need to ask Windows to wait for the program to complete; either use the CALL command or use START /WAIT. This question goes into some detail of the differences.

See the following example. Here I use Notepad as the example program; your python script should behave similarly - keeping the X window open until it is closed or finishes, and then the next one is opened.

c:\temp\runtest.bat contains

cd c:\windows
start /wait notepad.exe

SAS program:

options xsync noxwait;
x 'c:\temp\runtest.bat';
x 'c:\temp\runtest.bat';

One notepad window should open, and then when you close it, the second should open. You may be able to skip the .bat file here and just run start /wait directly, as well.

You could do the same thing by creating a single .bat file with your multiple python programs and running each of them with start /wait, and calling just the one from SAS, depending on your use case and preferences.

Community
  • 1
  • 1
Joe
  • 62,789
  • 6
  • 49
  • 67
  • what is happening with my code though is that the SAS loop is executing faster than the Python code. Everytime that happens each Python script runs slower and slower until everything grinds to a halt and I have hundreds or thousands of windows open when noxwait is used. – user3043997 Mar 12 '14 at 19:56
  • Run the above, does the above work as you expect and as I see? – Joe Mar 12 '14 at 19:57
  • It may work on Vista; if not, find some other way to do a couple second time out (ping a dummy IP with a -w 2000 or so should do it). – Joe Mar 12 '14 at 21:08
  • im not sure to be honest what you are asking me to do. – user3043997 Mar 12 '14 at 21:41
  • i understand that you are asking me to check the functioning of noxwait, but my shell scripting experience is virtually none. i dont know how to ping a dummy ip. i dont really want to use noxwait if possible. i need a method of submitting an exit command to the remaining administrator shell that is open after the python script closes... – user3043997 Mar 12 '14 at 21:43
  • This is the standard method of doing this in SAS. You should figure out why this isn't working for you, rather than looking for an alternate method that, if extant, will be suboptimal. I would be curious if Python is to blame; if you have `XSYNC NOXWAIT`, and send a request to run a python program that takes enough time to be obvious (5+ seconds), does the x window stay open for that duration of time, or close? If it closes, then the issue is that Python is running and doesn't let the OS know it's not done yet. Consider running a .bat file with your python instead of the pyhton directly. – Joe Mar 12 '14 at 21:53
  • xsync xwait: Two windows open, the first the administrator window, the second the one for python executing. The second one closes but the first one stays open and requires me to type 'exit'... – user3043997 Mar 12 '14 at 22:16
  • `xsync noxwait` is the correct way to do this (as you initially expected). Can you write a trivial python program that takes 5-10 seconds to run, and place the call to start test.py (or whatever) in the .bat file like in my example, and try it otherwise identically? – Joe Mar 12 '14 at 22:19
  • xsync noxwait: two windows open...the administrator window, which closes after a split second, then the python one which closes when the script runs. Im on a SAS course at the minute and the person running it suggested that SAS is submitting the command to run the python script and thinks that is the end of the process. so when noxwait is activated it is submitting the 'start test.py' command and then thinking that is the process completed. How would be running the python script in batch help? – user3043997 Mar 12 '14 at 22:19
  • Because you can do things like in [this SO answer](http://stackoverflow.com/questions/13257571/dos-call-command-vs-start-with-wait-option) to force it to wait for the program to complete. You may be able to do that in the x command directly, but I find it easier to write .bat files to do the same thing for testing purposes. In your case, try adding /WAIT to your START command and see if that helps first - it may be all you need. – Joe Mar 12 '14 at 22:20
  • I edited the answer to reflect what we've learned - give that a shot and let us know if it works. – Joe Mar 12 '14 at 22:28
  • options noxwait xsync; x 'cd C:\Python33'; x 'start /wait tester.bat'; x 'cd C:\Python33'; x 'start /wait tester.bat'; – user3043997 Mar 12 '14 at 22:31
  • this makes SAS behave as if it is back in xwait mode. i now have a two command prompt windows that remain open. if i type X into the first one they both disappear...i have also tried x 'cd C:\Python33'; x 'start /wait tester.bat /exit'; that runs but has no bearing on the outcome – user3043997 Mar 12 '14 at 22:33
  • Ok, I think this is now working. options noxwait xsync x 'C:\Python33\tester.bat'; with the batch file containing 'start /wait test.py', sans the single quotes. it works, but i dont really understand why as when i placed the '/wait' in the x command it didnt... – user3043997 Mar 12 '14 at 22:49
  • Some commands are different in .bat than at the command line; this may be one of them. – Joe Mar 12 '14 at 22:53
  • 1
    I copied everything over to my real SAS code and it started misbehaving again. In the end adding 'exit' to the end of the batch file seemed to do the trick. – user3043997 Mar 12 '14 at 23:14