Tag Archives: SPSS

Compressing Long Lists of Variables for Tidier SPSS Syntax

You just pasted a long list of variables into your SPSS syntax that you want to delete from or rename in your dataset.

Maybe you normally just leave it as is or maybe you manually compress the list with a committed effort of backspaces and spaces. Both options I find quite frustrating: you either deal with the laggy scrolling of SPSS as you move up and down your syntax for the former (which is even more frustrating if you have thousands of variables and you’re still working on the syntax) or you let it consume your time as you meticulously work through the list for the latter.

Well, I’m here to tell you about one way I’ve found to quickly compress long lists of variables.

Step 1: Copy and paste your variables into Microsoft Word.

Step 2: Go to Edit -> Find -> Replace…

Step 3: In the Find menu, select “Paragraph Mark” and within the Replace menu, manually press the spacebar once to effectively replace paragraph marks with a single blank space (you could also use the dropdown menu to select “Nonbreaking Hyphen” or “Nonbreaking Space”). Press Replace All.

Formatting marks (the blue markings) are shown for clarification
Formatting marks (the blue markings) are shown for clarification

Step 4: With the list now compressed into a single paragraph, you can either copy and paste this into your syntax and add in the paragraph breaks where you desire (as this will just give you one long row of variables) or you can add in the paragraph breaks within Word first and then copy it into your syntax (example below)

Paragraph breaks manually added in Word, again format markings shown for clarification
After adding paragraph breaks in Word, this is what it will look like after copying and pasting it into your syntax.

That’s it! Depending on how many variables you have, the manual paragraph breaks afterwards can still be a bit time consuming, but much less time consuming than removing the paragraph breaks one variable at a time within the syntax.

If you are aware of an even faster approach, please let me know. If not, I hope this helps! Happy syntaxing.

Variable and Value Labels in SPSS

Let’s face it, a well prepped SPSS dataset has informative and accurate labels for each variable and their respective values. However, it’s all too easy to plow ahead and think you’ll remember what each obscure acronym you create in the moment and the values assigned to them will mean some years down the road. Maybe, maybe not, but what I know is that if you spend a little extra time prepping your dataset, you can save your colleagues or yourself a great deal of time that would be spent trying to understand what your past-self was thinking.

Luckily, the business of renaming variable and value labels is fairly straightforward, yet there are still some tips and tricks that you can use in special cases that I will mention below.

But first, I’ll quickly go over the basics.

Syntax for Labeling or Relabeling Variable Labels

Labeling one variable

VARIABLE LABELS varname ‘Type your variable label here’.

e.g.,
VARIABLE LABELS FPK ‘MEAN SCALE SCORE: Follower’s political knowledge’.

Labeling more than one variable

VARIABLE LABELS varname ‘Type your variable label here’
/varname2 ‘Type your variable label 2 here’
/varname3 ‘Type your variable label 3 here’.

e.g.,
VARIABLE LABELS FPK ‘MEAN SCALE SCORE: Follower’s political knowledge’
/FPS ‘MEAN SCALE SCORE: Follower’s political skill’
/FPW ‘MEAN SCALE SCORE: Follower’s political will’.

Syntax for Labeling or Relabeling Value Labels

Labeling the values for one variable

VALUE LABELS varname #’Type your value number here’.

e.g.,
VALUE LABELS FPK 1’Strongly disagree’ 2’Somewhat disagree’ 3’Neither agree nor disagree’ 4’Somewhat agree’ 5’Strongly agree’

Labeling the values for more than on consecutive variable

VALUE LABELS varname1 to varname9 #’Type your value number here’.

e.g.,
VALUE LABELS FPK1 to FPK9 1’Strongly disagree’ 2’Somewhat disagree’ 3’Neither agree nor disagree’ 4’Somewhat agree’ 5’Strongly agree’

Labeling the values for more than one non-consecutive variable

VALUE LABELS varname1 #’Type your value number here’
/varname6 #’Type your value number here’.

e.g.,
VALUE LABELS FPK1 1’Strongly disagree’ 2’Somewhat disagree’ 3’Neither agree nor disagree’ 4’Somewhat agree’ 5’Strongly agree’
/ABSENCE 0’No’ 1’Yes’.

Tips and Tricks for Renaming Variable Labels

The most important thing to remember when labeling or relabeling variable labels is that you have something for each variable. The idea is that you should understand what each variable is without having to open any other file or going back to your original survey or source material.

Often times, you will have special variables that you created solely to conduct analyses on, such as mean scale scores, clinical cut-off scores, and so forth. I find it helpful to make these important variables pop out by beginning their label with an all-caps description (e.g., MEAN SCALE SCORE: Follower’s political knowledge; CLINICAL CUTOFF SCORE: HADS depression).

Tips and Tricks for Renaming Value Labels

The same general informative tip applies to value labels. It’s easy to leave these blank, but you can make your life easier by labeling these where appropriate.

Occasionally your source material will have or produce wonky values and value labels for you that you want to change (recoding variables is another related but separate topic that I will write about soon). After recoding the variable values, there is a very easy method of removing the old value labels and replacing them with ones that match your updated values.

Here is the syntax:
VALUE LABELS varname.
VALUE LABELS varname #’Type your value label here’.

e.g.,
VALUE LABELS FPK.
VALUE LABELS FPK 1’Strongly disagree’ 2’Somewhat disagree’ 3’Neither agree nor disagree’ 4’Somewhat agree’ 5’Strongly agree’

Here, the first VALUE LABELS command will remove the existing value labels and the second VALUE LABELS command will produce new value labels for your variable.

Remove Cases in SPSS

Notice some outliers or problematic cases in your dataset and want a shorthand way to quickly remove them while also keeping a record of which cases you removed? No problem, there are numerous ways to approach this.

If it is just one or a few numerical cases, then a great shorthand is:

SELECT IF VARNAME <> CASE.
exe.

OR

SELECT IF (VARNAME ne CASE)
exe.

With this syntax, replace VARNAME with the identifying variables (i.e., the variable that will identify the case you want to remove) and CASE with the specific entry within that variable. For instance, if your VARNAME is ID and the CASE you want to drop is 653, then your syntax would look like this:

SELECT IF ID <> 653.
exe.

OR

SELECT IF (ID ne 653).
exe.

If you have a few cases rather than just one, the latter syntax may be more efficient to use. For example, imagine you also have cases 155, 374, and 416 you want to remove. Here is what the syntax would look like:

SELECT IF (ID ne 653 and ID ne 155 and ID ne 374 and ID ne 416).
exe.

You can also use the the exact same syntax with string variables by adding ‘ ‘ around the entry that would identify the case you want to remove. For example:

SELECT IF NAME <> ‘Dave’.
SELECT IF (NAME ne ‘Dave’).
SELECT IF (NAME ne ‘Dave’ and NAME ne ‘Bob’ and NAME ne ‘Bill’).

If you have a large dataset and want to remove a good chunk of cases – say you have a number of cases that are missing on a key variable – then you can use the following syntax:

SELECT IF (not missing(VARNAME)).
exe.

You may come across circumstances where you need to get more creative with your case removal syntax, but in general these are the basic approaches you’ll most often use. As I come across new strategies for removing cases in SPSS, I will be sure to add them to this post for reference.

[More to come]