Tricks for Using Word to Make Statistical Syntax Easier

by Karen Grace-Martin


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?

{ 1 comment… read it below or add one }

Marcin March 18, 2017 at 7:29 am

With Syntax I work every day. Very useful advice.


Leave a Comment

Please note that Karen receives hundreds of comments at The Analysis Factor website each week. Since Karen is also busy teaching workshops, consulting with clients, and running a membership program, she seldom has time to respond to these comments anymore. If you have a question to which you need a timely response, please check out our low-cost monthly membership program, or sign-up for a quick question consultation.

Previous post:

Next post: