Вы находитесь на странице: 1из 11

5-1

Session 5

ConditionaI Computes - the IF Command



Basic Command 5-2
Relational and Logical Operators and Functions 5-5
Relational operators 5-5
Logical operators 5-6
Functions 5-7
Conditional Expressions and Missing Values 5-8
Practical session 5 5-9

5-2
SESSION 5: ConditionaI Computes - the IF Command

Basic Command

This command is essentially a compute command with a conditional
statement attached to it.

target variabIe=assignment expression

IF(conditionaI expression is true)

You can use conditionaI expressions to apply transformations to a
selected subset of cases. A conditional expression returns a value of

true
faIse
or missing

for each case. The assignment is executed only if the conditional
expression is true. n other words if the conditional expression is either
false or missing, then the assignment is not made.

Let's suppose we have a record of examination results marked as
percentages in our data file. The pass mark is 40% and we wish to record
how many students passed in the following data for 5 students


Figure 5.1

n English we could say ...

F the final examination MARK is greater than or equal to 40%, then the
student PASSES

This can be re-expressed in terms of an IF statement as follows ..

PASS=1 F(MARK>=40)


n order to carry this out in SPSS, we select ...

5-3
Transform
Compute variable

The Compute VariabIes dialog box appears and we fill in the name of the
Target VariabIe, pass and the Numeric Expression that pass should
take the value 1 (see Figure 5.2).


Figure 5.2

We then click on the If... box and we see the following dialog box ...


Figure 5.3

5-4

We now need to create the conditional expression that if mark is greater
than or equal to 40, the case satisfies the condition and will be included
for the compute command.

So we need to click on IncIude if case satisfies condition box and then
build up the condition expression mark>=40 as in Figure 5.3.

Clicking on Continue in this dialog box and OK in the compute variable
dialog box, gets SPSS to create the new variable, pass (see Figure 5.4).


Figure 5.4

f we then carried out a frequencies on the new variable, pass, then we
would see the following table, Figure 5.5:


Figure 5.5

Notice that those cases that did not satisfy the condition have been given
a system missing value for the new variable, pass.

5-5

Often it is the practice to set the new variable to 0, before creating a
conditional expression, i.e. use the compute dialog box first to set pass to
zero for all cases (no F statement). Doing this gives Figure 5.6:


Figure 5.6

Then after the conditional compute the resulting Data Editor window will
look like ...


Figure 5.7


ReIationaI and LogicaI Operators and Functions

n the example above used the relational operator >= There are several
other relational operators.

ReIationaI operators

EQ or = equal to
LT or < less than
GT or > greater than
NE or ~= not equal to
LE or <= less than or equal to
GE or >= greater than or equal to

n addition you can join conditional expressions using logical operators.

NB SPSS will ask if you
are sure you want to
change the variable 'pass'
when you do the second
stage of this compute
command say 'OK'
5-6
LogicaI operators

AND or &
OR or
NOT or ~


Example using both relational and logical operators

Let us take an example from bsas91b.sav. Suppose we have the
condition
PENSION=1
F (RAGE>=65 AND RSEX = 1) OR (RAGE>=60 AND RSEX =2)

i.e. if the respondent is aged 65 or over and is male, or if the respondent
is aged 60 or over and is female, set the variable PENSON to 1. f the
respondent is not of pensionable age, PENSON is left unchanged (if it
was not set to a value beforehand it will be system missing).

n SPSS for Windows open the Compute VariabIe dialog box, Figure 5.8
and enter the result we want, i.e


Figure 5.8

Click on If to set up the condition. We get the Compute VariabIe: If
Cases box, Figure 5.9.


Figure 5.9
Operators are performed in a particular
order if unsure, use brackets, as the
expressions in brackets are evaluated
before combining
5-7

n this example used the key pad to build up the expression by clicking
on the various elements.

However, could just as easily have created the F statement by typing
rather than clicking it into the box. However it is done you should (after
Continue, OK and a Compute, Frequencies) end up with
Statistics
pension
780
2138
Valid
Missing
N

pension
780 26.7 100.0 100.0
2138 73.3
2918 100.0
1.00 Valid
System Missing
Total
Frequency Percent Valid Percent
Cumulative
Percent


Figure 5.10

Functions

As with the Compute command there are many functions available such
as SUM, MEAN, RANGE and ANY.

Examples using functions

Let's go back to the marks example. This time, however, a student needs
to have a mean mark of 40% over a set of five exam papers to pass.
Once again here is some fictitious data :


Figure 5.11

Figure 5.12 shows the Compute VariabIe dialog box which creates the
variable pass to equal to 1 if the mean of the 5 marks is equal to or
greater than 40.

5-8
Figure 5.12

Here is the resulting Data window :


Figure 5.13

ie, the student in row 1 did not pass.


ConditionaI Expressions and Missing VaIues

As mentioned above, a conditional expression is evaluated as either true,
false or missing and only if it is true is the assignment statement
executed.

A conditional expression is evaluated as missing if any one of the
variabIes used in the conditionaI expression is system or user-
missing.

5-9
However, where relations are joined by AND or OR, the expression can
sometimes be evaluated as true even when variables have missing
values.


Expressions using AND Outcome Expressions using OR Outcome
True true True true true true
True false False true false true
false false False false false false
true missing Missing true missing true*
missing missing Missing missing missing missing
false missing False* false missing missing

* ndicates where expressions can be evaluated without complete
information


PracticaI session 5

1. Working-cIass Tories and middIe-cIass sociaIists

The data for this exercise is in H:My Documents\spss data\


bsas91b.sav

A majority of people who think of themselves as working class vote
Labour and a majority of those who think of themselves as middle class
vote Conservative. The F command can be used to identify people who
vote out of line with the majority of their class.

You will need to use the following two variables :

PARTYD1, the political party the respondent identifies with, and
SRSOCCL, the self-rated social class of the respondent


Before you can extract those who vote out of line with their class, you will
need to do some preparatory work with these variables. First, obtain a
FREQUENCES listing for them, to see the range of values and the
distribution of respondents on each variable, or look in the bsas91b
codebook.


Use a RECODE command to recode SRSOCCL so that the Upper Middle
and Middle class respondents are given the value 1 and the Upper
Working, Working and Poor respondents are given the code 2.
(Recode to the same variable)


5-10
The following commands would create a new variable, FALSECON (short
for 'false consciousness') which takes the value 1 for working class
Labour supporters, 2 for working class Conservatives, 3 for middle
class Labour supporters, and 4 for middle class Conservatives, except
that there is a deliberate mistake in one of the commands


Find and correct the error and then create the variable FALSECON using
the 4 COMPUTE and If. commands (click OK between each
command to ensure it is carried out before changing the Compute box
for the next one)

FALSECON=1 F (PARTYD1 = 2 AND SRSOCCL = 2).
FALSECON=2 F (PARTYD1 = 1 AND SRSOCCL = 2).
FALSECON=3 F (PARTYD1 = 2 AND SRSOCCL = 2).
FALSECON=4 F (PARTYD1 = 1 AND SRSOCCL = 1)


FALSECON is a new variable, which you create through these
commands. nitially, before any of the F commands are run, the value of
FALSECON is set automatically by SPSS to a 'system missing' value.
Those respondents who support neither Labour nor the Conservatives will
satisfy none of the four conditions and therefore the value of FALSECON
will remain unchanged as system missing for these people. The system
missing value is pre-defined to be 'missing' and appears in frequency
distributions as having the code '.', a full stop.

After the F commands, label the values of FALSECON (don't label the
system missing value).

Then obtain a FREQUENCES distribution of the variable.

Use this to answer these questions :

What proportion of the sample are 'working class Tories'?
What proportion are 'middle class socialists'?
What proportion of the sample are counted as 'missing' on this variable?
Why is the proportion so large?


2. CIass or poIitics?

Do middle-class Labour voters have opinions on lifestyle and
environmental issues which more closely resemble other members of the
middle-class or other Labour supporters?

Explore this question by cross-tabulating FALSECON with some or all of
the following variables (see if you can guess what the relationship will be
between FALSECON and these variables before you carry out the
tabulations. Then check your guesses):
Find the
mistake first!
5-11
How should the following family jobs generally be shared:
SHCHORE1 household shopping
SHCHORE2 making the evening meal
SHCHORE3 doing the dishes in the evening
SHCHORE5 doing the washing and ironing
SHCHORE7 organizing the household money and payment of bills


SAVE YOUR OUTPUT AS EXER5.SPO.

Вам также может понравиться