Using First & Last variables for SPSS Syntax- Great way to reduce your code

SPSS syntax

In SPSS syntax you can streamline your code by putting the word “TO” between the first and last variable you want.  For instance, in the image below, Category, Lang, cname, Date, and Sent is highlighted. If I wanted to run frequencies perform something on all of these variables I could write the following code:

Frequency Category Lang cname Date Sent.

or I could simply do the following:

 Frequency Category TO Sent.

Both return the same results yet the second one is much simpler, cleaner and can save a lot of space (especially when there are dozens of variables).

I have a short AutoHotKey script which grabs the variables highlighted, copies them to the clipboard and takes the first item, then puts the word ” TO ” then grabs the list item.

This helps me write clean-looking syntax much faster than doing it manually.

Grabbing first and last variable with SPSS syntax


The following is the AutoHotKey code:

;************************Grab first and Last variables for SPSS syntax*******************************.
^!v:: ;Control Alt V is the hotkey that pastes what is on the clipboard
Loop, parse, Clipboard, `n, `r ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
If A_Index =1
FirstToLast:= " " . First . " TO " . Last . " "
SendPlay, %FirstToLast%


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.