Home FAQ Syntax Macros Scripts Python Tips Links

Scripting (used to be) Heavy Duty Stuff!

NB: With the advent of Python, Sax Basic is now obsolete. I have not written SAX Basic code since SPSS 15.

  1. Introduction to SPSS scripting (a.k.a. SAX Basic)
  2. Index of Sample Scripts
  3. Sample Scripts
  4. VB Programs (an alternative to SPSS Production facility)



Scripts are relatively new to SPSS. If you have never used Visual Basic (VB) or Visual Basic for Applications (VBA), you will find the learning curve to be pretty steep. SPSS uses Sax Basic developed by Sax Software. By adding Sax's Type Library to your favourite VB development environment (for instance, VB6, Excel or Access) you may develop your SPSS scripts using those development environments.

Scripts allow the user to do things which would not be possible using syntax, for instance formatting an IGRAPH or having error handlers. Whenever possible, I prefer to use syntax instead of scripts.  As my knowledge of syntax increased, I discovered ways to do things which I previously thought only scripting could do, one such example is exporting or creating a variable which contains the value labels of another variable. 

Scripts can be called from syntax and vice versa. However a major problem is that a script called from anywhere in a syntax could be executed after the syntax file has finished! The same problem exists when a script calls a syntax, that syntax could only be executed once the script has finished.

Starting with version 14, Python scripts run synchronously with syntax files. The asynchronous problem is thus resolved. See Self adjusting code for examples.

Note that Scripting is not available in SPSS for Mac.


** Important Download Instructions **


Note: At the suggestion of a visitor, I have changed the extension of the *.sbs files to *.txt, this makes it easier to look at the files online. When you click on an *.sbs file below, it opens up in your browser as a text file. If you like what you see, go back to the prior page, right click on the file name as select "Save target as").

Copying the code and pasting it directly into an SPSS script window sometimes results in the loss of the line breaks; all lines are saved consecutively in the same line, this is not very useful   :-(



Index of Sample Scripts

The scripts files are broadly classified by purpose as follows:

  1. Autoscripts 
  2. Data Document
  3. Dialogs
  4. Distributions
  5. Draft Output
  6. Effect Size
  8. Import Export (see also the corresponding syntax section)
  9. Labels (see also the corresponding syntax section)
  10. Long Variable Names(version 12+)
  11. Output Format
  12. Output Document
  13. Printing
  14. Pivot Tables
  15. Syntax Doc
  16. Transform Data
  17. Utilities
  18. Working With Many Files (see also the corresponding syntax section)


Show me data and I will show you data errors!

Sample SPSS Scripts

Note: Files with a new.gif (111 bytes) at the end were added or modified less than 30 days ago.

  1. AutoExperiment.SBS To help in the debugging of autoscripts
  2. Bold Total in SUMMARIZE table or insert Page Break.SBS This is an example of a syntax which passes an argument to an autoscript. This can be very useful. Use this syntax file to verify that things work as expected.
  1. Apply String Length.SBS This automatically reformats all string variables of any sav file to the format of the string variable having same name in a Template.sav file
  2. Create empty data file with n (n is supplied by the user at run time)
  3. Display number of variables and cases in data file.SBS
  4. Find number of cases.SBS (works for weighted and un-weighted files)
  5. Load data file and print data values in MsgBox.SBS
  6. Set data editor column width to match variable  
  7. Split current data file in n random (n is supplied by the user at run time)


  1. Get a file name from the user.SBS
  2. Get file name from user then define path by macro.SBS 
  3. Get Fiscal Year from user then run syntax for that year.SBS 
  4. Get user name then define a usrname macro.SBS
  5. Variable list demo1.SBS (from SPSS site)
  6. Variable list demo2.SBS  (from SPSS site)
  7. Variable list demo3.SBS  (from SPSS site)


  1. Distribution
  2. Distribution.SBS
  3. Generate Random variables_EN.SBS with thanks to Kirill Orlov. Creates random variables with various options (eg correlated, rounded, with missing values etc)


  1. Empty Designated Draft Output.SBS 
  1. Section T-Test or Means or ANOVA  contains a few syntaxes related to Effect Size.


  1. Change background color of all IGRAPHs in Viewer.SBS
  2. Change bar colors for each IGRAPH bar chart with only an x-axis.SBS
  3. Export All IGRAPH to Jpeg of a given size.SBS
  4. Export current IGRAPH to Jpeg of a given size.SBS
  5. Format IGRAPH.SBS
  6. Format IGRAPH.SPS (this syntax creates data, two graphics then calls the above script)



  1. Import SPSS using VBA for ACCESS.mdb This is an MS Access 2000 file. See instructions inside the GetSPSSvars module.
  2. Export labels from MS Access to SPSS.txt  This is Visual Basic code.
  3. Save from SPSS to MS Access 97 table.SPS (AnswerNet)

Data Editor:

  1. Export pivot table data cells to data editor.SBS


  1. Save all attachments of emails to a given folder.SBS (This is an Outlook VB Script) all emails in a given Outlook folder are processed) 
  2. Send a document as an attachment to an email.SBS This SaxBasic script uses Outlook 2000 to send an email to a recipient with an attachment, the attachment could be an HTML page produced by SPSS, and spo file or any other documents. 


  1. Convert to Excel 5_95 format.SBS SPSS exports to Excel 2.1; this automatically converts the file to Excel 5/95 format.
  2. Export tables to a single excel
  3. Export tables to a single excel sheet This exports then groups the lines forming a table. Very convenient.
  4.  Similar to above script however this one also exports CHARTS and IGRAPHS.
  5. As an improvement to V3, this one exports all layers of Pivot Tables.


  1. Export all pivot tables of all spo files to HTM file.SBS
  2. Export target items of all spo files to HTM file.SBS (this is an extension of above script. This one handles IGRAPH in addition to Pivot Table and Charts)


  1. Export Pivot Tables to PowerPoint.SBS
  2. Export data from Excel to This VB.NET program (author Kirill Orlov) is for those who fill PowerPoint charts with data from Excel (e.g. data of Pivot Tables exported to Excel from SPSS output). Help documentcontains is included;


  1. SPSS_to_STATA_01.SBS with thanks to Alasdair Crockett. This is the conversion guide explaining how to use the script. This scripts will be useful to those using SPSS versions older than v14 (since v14 automatically reads / writes stata files). 

Word or Text file:

  1. Export Data Dictionary to text (thanks to Jean-francois Allaire)
  2. Export Pivot Tables to (original by SPSS, modified by John Hendrickx to format tables)
  3. Export sav file to free format.SBS 
  4. Export visible output to word via  (here, word has links to the graphs, graphs are not embedded in the word document)
  5. Export visible output to word via HTML (here the graphs are embedded in the word document)


  1. Export content of data editor to XML (with thanks to Tom Diericks. See xml101 for info on the XML format.)  Note: this script and the next one require at least version 3.0 of the Microsoft XML parser. The parser can be downloaded for free at
  2. Export content of data editor to XML format version is for SPSS version 11 and up). (See the above note)
  3. Export2Triple-S.SBS(this script creates a Triple-S v1.2 XML file and related data file from the current SPSS data file). Thanks to the author Chris Johnson for sending it.
  4. Export data from SPSS to Pulsar Beta 0.16.SBS (with thanks to Alexander Bougakov). This is based on the above script by Chris Johnson. 
  5. Export spss Output viewer to XML.SBS


  1. Add extension to all variable names.SBS
  2. Add the word "index" at the beginning of all variable labels.SBS
  3. Add value labels.SBS
  4. Add value labels using a loop.SBS
  5. Add variable name at beginning of variable label.SBS
  6. Assign variable label equal to variable name.SBS (can also be done with syntax)
  7. Capitalise variable and value
  8. Define macro calls for each value label (This is a useful programming tool)
  9. Define macros giving variable labels.SBS (can also be done with syntax)
  10. Delete value labels of all
  11. Export variable and value labels to txt files.SBS (thanks to Wouter Egberink for sending me this code)
  12. Export variable name and variable labels to text file.SBS
  13. Populate variable with value labels of another variable.SBS (can also be done with syntax)
  14. Remove pivot table row and column
  15. Reverse autorecode.SBS To create a new string variable containing the value labels of an existing variable (this can also be done using syntax alone)
  16. Reverse auto recode10.SBS Use this version for SPSS v10 and over.


  1. Rename Long to and Rename Port Short to These scripts allow switching between long and short variable names. Use with this syntax file.sps. These come from a posting of Jon Peck.  


  1. Apply autofit.SBS
  2. Apply Table look to all
  3. Change graph table
  4. Frequencies
  5. Size labels to avoid


  1. Add page breaks at selected places.SBS (an easy to use script to automatically insert page breaks before any specified pivot tables). This syntax file shows how to use the script.
  2. Break output file into smaller pieces.SBS(AnswerNet)
  3. Change label title of last run procedure.SBS (With thanks to Michael Wexler)
  4. Create new Output with a given filename and file path.SBS
  5. Data footer.SBS (sets footer of Output to name and path of .sav file)
  6. Delete statistics and case processing
  7. Empty designated output
  8. Extract information from a text output.SBS (The script is called by this syntax )
  9. Insert a text object into the SPSS
  10. Produce a Table of Content (TOC) listing all pivot tables.SBS
  11. Remove page breaks.SBS
  12. Replace left pane PageTitle by content of right pane.SBS (Useful to help locate given areas of the output navigator)
  13. Save current output windows.SBS The script is called from this syntax
  14. Save current output window-by script.SBS
  15. Search label in Output
  16. Select last table in current output.SBS
  17. Write index in output navigator.SBS


  1. Add footnotes to every Pivot Tables.SBS
  2. Applying any script to all pivot tables .sbs
  3. Automation of pivotting.SBS (Examples to learn how to manipulate layers, columns and rows)
  4. Autoscript to format correlation removes upper diagonal, highlights significant correlations, moves N's and Sig's to the Layer's dimension to hide them) With thanks to Ferd Britton)
  5. Change format means report.SBS (the script is called by this syntax)
  6. Change pivot table (This is useful to change column or row labels from English to other languages)
  7. Change Sig to
  8. Combine mean and SD.SBS (the script is called by this syntax)
  9. Create pivot
  10. Create pivot (the script is called by this syntax)
  11. Crop and retain.SBS (with thanks to Mark Baxter)
  12. Cycling through all layers of a
  13. Define customized crosstab titles.SBS Note: to avoid asynchronous problem, the script could be modified to check all documents in the Output window. The script would then be called only once.
  14. Delete footnotes of all Pivot Tables.SBS
  15. Do not display value labels in pivot tables.SBS
  16. Exchange rows and columns of Pivot Table.SBS
  17. Hide valid and valid percentage in frequency table.SBS
  18. Hide a column in pivot table.SBS
  19. Highlights significant cells of all ANOVA tables in Designated This is an example of Applying a script to all (ANOVA) pivot tables in the Designated Viewer.
  20. Highlight significant T-Test
  21. Highlight significance levels_EN.SBS with thanks to Kirill Orlov. Dialog box offers various options to the user. 
  22. Italize percentages in PivotTable.SBS
  23. Left right or center justify title of all pivot
  24. Make totals This applies only to the currently selected Pivot Table. Can also change the color of applicable cells. 
  25. Make Totals Bold All Pivot Similar to the above script but applies to all visible Pivot Tables in the Designated Viewer
  26. Pivoting to each layer of a pivot table.SBS
  27. Remove percent signs from PivotTable.SBS
  28. Replacing Empty Cells of Pivot Tables by user-defined
  29. Reverse columns
  30. Rotate inner labels of all visible PivotTables.SBS
  31. Round frequencies in crosstab tables.SBS
  32. Round percentages in crosstab.SBS
  33. Set number of decimals in pivot table.SBS
  34. Set number decimals of percentages in PivotTable.SBS
  35. Substitute string in pivot tables.SBS
  36. Table widths- with syntax.SBS(the script is called by this syntax) (Thanks to Jason Burke for this improved version).
  37. Traffic
  38. Traffic light fixed 15 and (as above but without dialogs)
  39. Traffic light with 3 cut off
  40. Undo percent notation.SBS
  41. Undo scientific notation.SBS (this Sub is already included in the Global.SBS file)
  42. Ungroup a column in a pivot table.SBS


  1. Print all spo files in a given folder.SBS
  2. Print current syntax with file path, date, time and page numbers.SBS (It is convenient to attach that script to a new toolbar button in the script window)
  3. Print data filename and path as part of output footer.SBS
  4. Print (this is called by next syntax)
  5. Print options.sps (from AnswerNet)
  6. Print orientation.SBS (Add heading, date, time, page numbers to printout)
  7. Read script parameters from log file.SBS  
  8. Toggle display of page numbers in Output window.SBS


  1. Close All Syntax Doc.SBS
  2. Convert Syntax To
  3. Find and replace block of text in syntax 
  4. Paste text at the end of syntax
  5. Run Designated Syntax in Batch Mode.SBS I find this very useful, for convenience associate it to a custom made button on your syntax toolbar. Click the button when you want the Designated Syntax to run up to the first error.
  6. Write syntax file defining variable and value labels.SBS


  1. Define missing for all numeric variables.SBS
  2. Standardize within


  1. Add file date to file names in a given folder.SBS
  2. Automatically gets data and output HTML files every hour.SPS
  3. Automatically run a script or syntax when SPSS starts.SBS
  4. Call script B from script A.SBS
  5. Convert syntax into script format.SPS (with thanks to Rolf Kjoeller)
  6. Create new output
  7. Script to run a Dos command or Bat file; code by Fabrizio
  8. Levenshtein Distance between 2 strings.SBS (this syntax file explains how to use the script)
  9. Illustrate windows state.SBS (how to minimize, maximize a window)
  10. Multi purpose script: (run Sub based on syntax call).SBS
  11. Name data file based on date read from data file.SBS
  12. Syntax This is Fabrizio script to circumvent the asynchronous problem. You must ensure that there are no trailing spaces at the end of the syntax.
  13. Toggle Output (with thanks to Karel Asselberghs)
  14. Terminate syntax with period.SBS
  15. Write table of content of output navigator to text file.SBS


  1. Combine all .sav files in the given folder.SBS (only file path and mask is supplied, script does the rest... The resulting sav file contains a variable identifying the source file of each case)
  2. Combine all sav or text files in given folder.SBS This is a generalization of the preceding script, this one combines either sav or txt files. A variable in the final file contains the name of the file where the case comes from. Of course the DATA LIST commands must be adapted to each particular situation.
  3. Convert all files in folder into other formats.SBS Can convert from 
    *sav to *.por
    *.por to *.sav
    *.sav to *.xls
  4. Create as many sav files as there are txt files.SBS
  5. Export all pivot tables (and/or Charts) of all spo files to HTM file.SBS (only spo files meeting a given path and file mask are processed. One HTM file is created per SPO file).
  6. Export target items of all spo files to HTM file.SBS (this is an extension of above script. This one handles IGRAPH in addition to Pivot Table and Charts)
  7. List all files in given folder.SBS
  8. Run Same Syntax on 17 Zipped Files.SBS



VB Programs


This program has been written by Alexis-Michel Mugabushaka of the University of Kassel, Germany. The program is similar to SPSS Production's facility. You feed the program with a list of syntax files or a file containing a list of syntax files. The program runs each syntax one by one and create a separate output file for each of the syntax file and having the same name as the syntax file. See also Break Output File into Smaller Pieces.SBS


ViewSav is a Real-Time Codebook for SPSS data files. It has been written by Karel Asselberghs from the Sociology Department,   University of Amsterdam. He has also 2 other SPSS utilities:CRC32SAV and LISTSAV. The first one computes checksums for separate parts of a SPSS data file: file header, dictionnary and data. This enables you to check if these areas of 2 sav files are identical. The second one list variables and variable labels of a SPSS data file. (source code included).

Top of page