1) Using a wide variety of mathematical functions, you can compute new variables based on highly complex equations. In this example, however, we will simply compute a new variable that is the difference between the values of two existing variables.
2) The data file demo.sav contains a variable
for the respondent's current age and a variable for the number of years at
current job. It does not, however, contain a variable for the respondent's age
at the time he or she started that job. We can create a new variable that is the
computed difference between current age and number of years at current job,
which should be the approximate age at which the respondent started that job.
► From the menus in the Data Editor
window choose:
► For Target Variable, enter jobstart.
► Select Age in
years [age] in the source variable list and click the arrow button to
copy it to the Numeric Expression text box.
► Click the minus (–) button on the
calculator pad in the dialog box (or press the minus key on the keyboard).
► Select Years with
current employer [employ] and click the arrow button to copy it to the
expression.
► Click OK
to compute the new variable.
The new variable is displayed in the Data Editor. Since the
variable is added to the end of the file, it is displayed in the far right
column in Data View and in the last row in Variable View.
3) You can also use predefined functions in expressions. More than 70
built-in functions are available, including:
• Arithmetic functions
• Statistical functions
• Distribution functions
• Logical functions
• Date and time aggregation and extraction functions
• Missing-value functions
• Cross-case functions
• String functions
Using Functions in Expressions
======================
4) Functions are organized into logically distinct groups, such
as a group for arithmetic operations and another for computing statistical
metrics.
5) For convenience, a number of commonly used system variables,
such as $TIME (current date and time), are also included
in appropriate function groups.
6) A brief description of the currently selected function (in
this case, SUM) or system variable is displayed in a
reserved area in the Compute Variable dialog box.
7) To paste a function into an expression:
► Position the cursor in the
expression at the point where you want the function to appear.
► Select the appropriate group from
the Function group list. The group labeled All
provides a listing of all available functions and system variables.
► Double-click the function in the
Functions and Special Variables list (or select the function and click the arrow
adjacent to the Function group list).
8) The function is inserted into the expression. If you highlight
part of the expression and then insert the function, the highlighted portion of
the expression is used as the first argument in the function.
9) The function is not complete until you enter the arguments,
represented by question marks in the pasted function. The number of question
marks indicates the minimum number of arguments required to complete the
function.
► Highlight the question mark(s) in
the pasted function.
► Enter the arguments. If the
arguments are variable names, you can paste them from the variable
list.
Using Conditional Expressions
=====================
10) You can use conditional expressions (also called logical expressions) to apply
transformations to selected subsets of cases. A conditional expression returns a
value of true, false, or missing for each case. If the result of a conditional
expression is true, the transformation is applied to that case. If the result is
false or missing, the transformation is not applied to the case.
11) To specify a conditional expression:
► Click If
in the Compute Variable dialog box. This opens the If Cases dialog
box.
► Select Include
if case satisfies condition.
► Enter the conditional
expression.
12) Most conditional expressions contain at least one relational
operator, as in:
age>=21
or
income*3<100
13) In the first example, only cases with a value of 21 or
greater for
Age [age] are selected.
14) In the second example, Household income in
thousands [income] multiplied by 3 must be less than 100 for a case to be
selected.
15) You can also link two or more conditional expressions using
logical operators, as in:
age>=21 | ed>=4
or
income*3<100 & ed=5
16) In the first example, cases that meet either the Age [age] condition or the Level of education
[ed] condition are selected.
17) In the second example, both the Household
income in thousands [income] and Level of education
[ed] conditions must be met for a case to be selected.
No comments:
Post a Comment