The Asynchronous Problem
Options to coordinate Sax (Win Wrap) Basic (a.k.a. SPSS "VBA") scripts and syntax execution
Note that this problem is completely resolved with SPSS version 14 by the advent of Python scripting.
Description of files used
A syntax and a script are used to illustrate the SPSS asynchronous problem. (This is based on a 1999/08/16 post to the spss newsgroup by Kai Borgolte).
**** test.sps **** . data list free / v1 to v3. begin data 1 2 3 end data. frequencies variables = v1. * Next line creates the syntax file test.inc which includes the command: frequencies variables = v2 script 'c:\temp\test.sbs'. * We then include (run) that file to get the frequencies of v2. include 'c:\temp\test.inc'. * we erase the file since we no longer need it. erase file = 'c:\temp\test.inc'. frequencies variables = v3.
' **** c:\temp\test.sbs **** . Sub Main Open "c:\temp\test.inc" For Output As #1 Print #1, "frequencies variables = v2 ." Close #1 'A message to the user confirming that the file has been created. MsgBox "test.sbs is running, test.inc has been written" End Sub
Demonstration of the problem
- Save the above script and syntax in c:\temp\. The first time you run the syntax,
- Frequencies of v1 is printed.
- The message from the script confirms that the test.inc has been written.
- Frequencies of v2 is not printed.
- Frequencies of v3 is printed.
- When you look at the log, you will see that test.inc was not found and that, hence, it was not erased (since it did not exist when that line of syntax was executed).
- However if you look in the c:\temp folder, you will see the file. It was created after the ERASE command was run.
- Now if you run the syntax again, without manually erasing the test.inc file, the program will work as expected, the frequencies of all 3 variables will be produced.
The above example involves a syntax calling a script. Note that the asynchronous problem occurs also when a script calls a syntax.
Possible work around
Version 12 and earlier
Fabrizio's script SyntaxScript.sbs was written to solve that problem. Note that the original script enters in an endless loop when a syntax file has hard spaces at the end. The following version SyntaxScript2.sbs solves that problem.
Ensure that the test.sps syntax is the designated syntax window, load and execute SyntaxScript2 and you will see that the syntax test.sps performs as expected. All 3 frequencies are produced. It is convenient to add a custom made button to the toolbar and attach the SyntaxScript2 to it. It becomes very simple to invoke the script whenever necessary.
Note that there are limitations to the script. In particular when the script call passes arguments to the script, a different method must be used by the script to read the argument.
In some cases, the executable by Alexis-Michel Mugabushaka can also be used to circumvent the asynchronous problem. See the VB Programs section of the script page.
The introduction of the HOST COMMAND provided new ways to work around the asynchronous problem. See the the second edition of SPSS Programming and Data Management which is available as a free download.
Version 14 and above
With version 14, Python Scripts executed within a syntax file runs synchronously with the syntax, so the problem is now solved. See Self adjusting code for examples of synchronous execution using Python.