Tricks for Using Word to Make Statistical Syntax Easier

We’ve talked a lot around here about the reasons to use syntax — not only menus — in your statistical analyses.

Regardless of which software you use, the syntax file is pretty much always a text file. This is true for R, SPSS, SAS, Stata — just about all of them.

This is important because it means you can use an unlikely tool to help you code: Microsoft Word.

I know what you’re thinking. Word? Really?

Yep, it’s true. Essentially it’s because Word has much better Search-and-Replace options than your stat software’s editor.

Here are a couple features of Word’s search-and-replace that I use to help me code faster:

1. Searching and replacing tabs and hard returns

A lot of times when you’re working with syntax, you’ll find that you need to insert a tab or a hard return after every semi-colon. Or in the middle of a list of variables. This is very easy to do in Word.

Let’s say that everywhere I have BY in my code, I’d like a tab before it instead of a space. (You’ll see why I need this below.) I can just do the following:

In case it’s not clear, there is a space before the BY in “Find what:” and a ^t before the BY in “Replace with.”

In Word ^t is the tab mark. (The hard return — a.k.a. paragraph mark — is ^p). But if you forget, you get the full list by clicking on the More button, then the Special button:

2. Copying columns of text

Whether it’s code or output, there are times you’d like to copy a column of text out of a text file.

For example:

Let’s say you’re doing descriptive statistics before beginning an analysis and you’re running a series of crosstabs. Here is the syntax for doing this in SPSS:

Now granted, this is a small data set with only a few variables, but imagine you had 40 categorical variables instead of 5. If I tried to copy the variables out of SPSS, I could only grab the entire lines of text, highlighted below:

That certainly works, but then I would have to clean up all the words before and after it. And that is slow.

But Word will let me copy out a column of text. Here’s how:

  1. Copy and paste the whole thing into Word.
  2. Make sure that all the words I want to copy are in a column of text. In other words, I need enough blank spaces after gender, ethnicity, and edulevel that the BYs are all on top of each other (you’ll see why on the next step).

To do this, I search for the space before each BY and replace with a tab, as seen above. It will result in this:

Holding down the Alt key before copying allows you to select values straight down in a column. This is why I needed all the BY’s to be moved to the right or some of them would have been included in my selection.

I can now paste my list as a vertical column.

If I want them horizontal in my code, I can just search for the paragraph marks (^p) and replace them with spaces.

It also works nicely when pulling output from R, which is also just text. Let’s say I wanted to just copy out the coefficients from my regression model. I could use Alt to select just the column of coefficients, like this:

So that’s that. Pretty cool, right?


Reader Interactions


  1. Karen I. says

    Helpful advice.
    Although this helps with editing syntax, I actually use Excel to help me build a lot of syntax–e.g., taking var names and using the CONCATENATE function to create variable labels (with appropriate syntax) that I can then cut and paste into a SYS file.

  2. Chao says

    You can do the same in Notepad++ (or any good text editor) and probably more. For example instead of “^p”, you use “\r” to find the paragraph mark (or a return character).

  3. Ángel Rodríguez says

    Thank you for the great tips. Holding down the Alt key before copying works for me in Word but not with R output in R Studio (not even in the R console). Any clue to sort this out?

  4. Jon K Peck says

    The SPSS Syntax Editor avoids the use of the tab character because of the confusion tabs in the input can cause, mainly with data, but it does have a column mode. ctrl+k starts it and escape exits it.

    I do like Notepad++ for complex edits and its ability to display nonprinting characters such as tab. Its regular expression search and replace is sometimes very handy. Of course, it doesn’t color code SPSS syntax or provide popup syntax help (ctrl+spacebar).

    The SPSS SE shortcuts can be displayed via Edit > Syntax Keyboard Shortcuts1

  5. Gary Klein says

    I sort of prefer Notepad++ to Word. It can easily do column blocks like your example (in fact the identical way), but since SPSS syntax files are text, as are the Notepad files (unlike Word), I don’t need to go back and forth pasting to the SPSS syntax file but can complete all my syntax work in Notepad++. There are other plusses in the way one can maneuver across files in ++, and it gets a third + for being free!

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.