IBM Support

How do I calculate a new variable based on values of other variables

Question & Answer


Question

I would like to compute a new variable, the result of which depends upon what is in two other variables which are both numeric. For example if var1=1 and var2=0, then newvar=0. If var1=1 and var2=1 then newvar=1. If var1=2 and var2=0 then newvar=1. If var1=2 and var2=1 then newvar=2. If var1=2 and var2=2 then newvar=3. How can I do this in the Statistics application?

Answer

Method 1 is to create a syntax file and run the syntax.

The syntax below first generates a sample data file. Then it computes newvar based on what the values of var1 and var2 are.

DATA LIST / var1 1-1 var2 3-3.
BEGIN DATA
1 0
1 1
2 0
2 1
2 2
END DATA.
EXE.


** First compute the new variable called newvar with a default value of 0 and then test the values of
** var1 and var2 to determine the value to give newvar.

COMPUTE newvar=0.
IF ((var1 = 1) & (var2 = 0)) newvar=0.
IF ((var1 = 1) & (var2 = 1)) newvar=1.
IF ((var1 = 2) & (var2 = 0)) newvar=1.
IF ((var1 = 2) & (var2 = 1)) newvar=2.
IF ((var1 = 2) & (var2 = 2)) newvar=3.
EXECUTE.

Method 2 is to use the Statistics menu and the Transform > Compute Variable option.

The Transform menu has an item called Compute Variable. Here you can create new variables.

The Target Variable field is where you will type the new variable name such as newvar in the example above.
The Type and Label button allows you to assign a variable label to the new variable as well as
to declare the new variable's format such as string (alphanumeric) or numeric.
By default new variables created in this dialog box are numeric.

The Numeric Expression window is used for a value or formula.
A numeric expression can be a specific value (e.g. 12), an equation (e.g. VAR1 * VAR2 or (VAR3 * VAR4)/ 5.5 )
or an SPSS function (ARSIN(VAR5)) ). There is a pull-down menu of algebraic functions that you may use
as well as a keypad to insert numbers and arithmetic signs (such as "=" or ">").
The SPSS Syntax Reference Guide has a section on the list of functions (used for computing numeric,
string, and numeric date variables) and what they mean.
Please refer to this guide for thorough information regarding these functions.

The IF button allows for conditional computation.
You can compute a new variable such as newvar in the example above by entering newvar in the Target Variable window.
In the Numeric Expression area you can enter a value such as 1 or a numeric expression or an expression using given functions.
Click the If button if you want to set a condition for when this value should be assigned to the new variable.
In the example above, you would click the If button and choose Include if case satisfies condition.
The condition would be ((var1 = 1) & (var2 = 1)) for example.

You can also change the value in an existing variable for a subset of cases. You can paste the variable name
in the Target Variable window, type the new value in the Numeric Expression window, then click on the If button.
You can specify the condition (such as GENDER=1 or RACE=1 AND REGION=3) and then click on the Continue Button.
This will bring you back to the Compute Variable window.
You can click the OK button to execute the compute, or you can click on Paste to generate the syntax which can be run later.
You can continue to edit the pasted syntax as needed, prior to running it.

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"19.0","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

53687

Document Information

Modified date:
16 April 2020

UID

swg21480831