Company News Products Projects Texts Museum Download Contact Map RSS Polski
YAC Software Texts SPSS SPSS Syntax Tips Trybiks' Dive
  Back

List

Data

Excel

Media research

Market research

Respondent quotas

SPSS

VBA

YAC Data Language

SPSS Syntax Tips
Here are several tips for SPSS users - I hope that these will answer some of the questions/searches that I've been seeing in my Apache logs. :-)

Tip 1
Ok, the first thing was: recoding values per cases, not per variables.

A standard operation in SPSS is to calculate a value of a variable based on values of other variables. But such operations are carried out for all cases in the data file.

Now, I'd like to change, for instance, eights to nines for a range of variables in a single case. Here's how this can be done:
  do repeat v = v1 to v4 .
    if( respid = 1 & v = 9 ) v = 8 .
  end repeat .
do repeat is a really nice instruction that can loop over variables as well as values (this will be shown in the second example). Above, the loop goes through variables from v1 to v4. Next, in the if instruction we check whether this is the case with the respondent's ID set to 1. If so, and if the value of v is 9, recode it to 8.

You can also replace missing data by rows instead of columns by using the missing and sysmis instructions. For instance:
  do repeat v = v1 to v4 .
    if( respid = 1 & missing( v ) ) v = 9 .
  end repeat .
Missing returns true if the variable's value is defined as a missing value (for that variable). Sysmis returns true if the variable's value is a system missing value.

Tip 2
Now, let's say that we have multi-response data written to multi-value variables. For instance, if a respondent chose responses 1, 2, and 4, these values would be written to variables v1, v2, and v3. But what we need is dichotomous variables with values 0 and 1 in variables w1 to w4 (w3 would get a zero in this example, the other 3 variables - a one).

do repeat loops have a nice feature - you can loop in parallel through several sequences of variables and/or values:
  do repeat w = w1 to w4
          / a = 1 thru 4 .
    count z = v1 to v3 ( a ) .
    if( z > 0 ) w = 1 .
  end repeat .
This loop iterates through the pairs (w, a): (w1, 1), (w2, 2), ..., (w4, 4). The count instruction counts (in the variable z) the number of values in v1, v2, and v3 specified by a. If this number is greater than 0, the respective dichotomous variable gets a 1.

You could add an instruction that if z equals to 0 then w is assigned 0. But as an additional example, take a look at the following code:
  do repeat w = w1 to w4 .
    if( sysmis( w ) ) w = 0 .
  end repeat .
We once again loop through our dichotomous variables and if the value of the variables is a system missing (no value was assigned in the previous loop), then assign a 0.

Tip 3
All the instruction above are data processing instruction, but no calculations are then carried out. The SPSS processor goes into a "transformations pending" state and you need to run pending transforms from a menu.

The instruction below does this automatically:
  exe .
That's shorthand for "execute" (SPSS understands abbreviated words as long as their meaning is uniquely defined).

Tip 4
Final tip in this text: how can the order of variables be changed in the data file?

Well, take a look at the save instruction. Let's assume that our data files has the following variables (in that order): v1 to v4, w1 to w3. And we want to change the order thus: w1 to w3, v1 to v4:
  save outfile='d:\path\1.sav'
       /keep w1 to w3 v1 to v4 .
It's as simple as that. The /keep modifier let's you not only specify which variables to keep, but also in what order. Note that there is a /drop modifier that lets you drop only selected variables.

Tip 5
Oh, and by the way, after processing your data, this is how you can save it automatically, too:
  save outfile='d:\path\2.sav' .
HTH

Top

Comments
#1
Rich wrote on 2012-02-05 19:25:19
Thanks, very much for this information. This is very useful. I have another question, that might be related to this.
Is there a syntax for computing a new variable, depending on how people responded to a set of items in a multiple response question? Let me explain: There is a multiple response question, with 20 items. We consider all items in the question as all to pointing to healthy life style.

I want to create a new variable "healthy life style" (YES/NO)...so if a person responded "yes" to ANY of the items in the multiple response question, she/he would be categorized as having a healthy life style.

Usually I use the Transform>Compute Variable>If>Include if cases satisfy condition>...and enter the condition for each individual item (e.g. if v1=2 & v2=2 & v3=3...etc)

This is OK, but very time consuming when you have a large set of items to include...is there a way to do this with a syntax?

Thanks heaps for sharing your knowledge
#2
Trybik wrote on 2012-02-07 10:17:30
Glad to be of help.

For the question you ask, take a look here:
SPSS Syntax - COUNT function

Hope that helps.

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

SPSS


Related pages

SPSS Syntax - COUNT Function