This tutorial covers some obvious and some less obvious functions for editing numeric variables. You can follow along with this tutorial by downloading and opening hospital.sav. They syntax we'll use can be copy-pasted or downloaded here.
Specifying User Missing Values
This tutorial will only use the last 2 variables in the data.These are ordinal variable so, strictly, calculations are not allowed on them. However, this is common practice nevertheless as explained in Assumption of Equal Intervals. Before doing anything else, we first specify 6 as a user missing value by running the line of syntax below.
missing values food_rating facilities_rating (6).
Numeric values can be added in SPSS by simply using the + operator. The syntax below gives an example by computing add_1 as the addition of food_rating and facilities_rating
compute add_1 = food_rating + facilities_rating.
One thing to keep in mind is that if any of the operands (mostly “input variables”) is a missing value, the result will a system missing value. This can be avoided by using the
SUM function instead, which we'll discuss later.
Subtracting numeric values in SPSS is straightforward and can be done with the - operator as shown in the syntax below.
compute sub_1 = facilities_rating - food_rating.
Numeric values can be multiplied in SPSS with the * operator. The syntax below demonstrates this.
compute multiply_1 = food_rating * facilities_rating.
Simply use the / operator for division in SPSS. The syntax below gives an example.
compute divide_1 = food_rating / facilities_rating.
Exponentiating in SPSS is done by the ** operator. The syntax below gives some examples. Note that you can also compute (square or other) roots by exponentiation as shown in the second example.
compute exponentiate_1 = food_rating **2.
*2. Compute cubic root of food_rating.
compute exponentiate_2 = food_rating **(1/3).
*3. Raise food_rating to the power facilities_rating.
compute exponentiate_3 = food_rating ** facilities_rating.
SPSS SQRT Function
Computing square roots in SPSS can be done by exponentiating a number to the power 0.5 as hinted at by the previous syntax example. For the less mathematically inclined, SPSS also has the
SQRT function. For the sake of completeness, we'll demonstrate it below.
compute square_root_1 = sqrt(food_rating).
SPSS RND Function
Rounding numbers in SPSS is done with the
RND takes an optional second argument, which is the nearest value to round to. Like so, you can round to the nearest quarter point (second example below) or tenfold (third example).
If the second argument is omitted, values will be rounded to the nearest integer (first example). Note that the syntax below uses some variables that were computed in the previous examples.
SPSS RND Syntax Examples
compute rnd_1 = rnd(square_root_1).
*2. Round square_root_1 to nearest quarter value.
compute rnd_2 = rnd(square_root_1,.25).
*3. Round exponentiate_3 to nearest tenfold.
compute rnd_3 = rnd(exponentiate_3,10).
SPSS TRUNC Function
Truncating numbers basically means rounding them down to the nearest integer or other value. SPSS includes a
TRUNC function for doing so. It takes an optional second argument, which is the nearest value to which to truncate. Like so, you may truncate a number to the nearest tenfold (third example below) are quarter value (second example).
If the second argument is omitted, numbers will be truncated to the nearest integer. Note that the syntax below uses some variables that were computed in the previous examples.
SPSS TRUNC Syntax Examples
compute trunc_1 = trunc(square_root_1).
*2. Truncate square_root_1 to nearest quarter value.
compute trunc_2 = trunc(square_root_1,.25).
*3. Truncate exponentiate_3 to nearest tenfold.
compute trunc_3 = trunc(exponentiate_3,10).
SPSS MOD Function
The modulus function returns the remainder of a number after subtracting a second number from it as many times as possible. Note that it's basically the opposite of truncating; instead of throwing away the remainder, we now throw away everything except for the remainder.
MOD(a,b), means “subtract b from a as many times as possible and return the remainder of a”. If that still sounds a little abstract, try running the two examples in the syntax below. Note that it uses some variables from the previous examples. The results are also shown in the screenshot that follows.
SPSS MOD Syntax Examples
compute mod_1 = mod(square_root_1,1).
*2. Remove all tenfolds of exponentiate_3.
compute mod_2 = mod(exponentiate_3,10).
THIS TUTORIAL HAS 11 COMMENTS:
By Ruben Geert van den Berg on September 8th, 2021
COMPUTE newvariable = oldvariable**(2/3).
This simply raises oldvariable to the power (2/3). Like so, you could also use
COMPUTE newvariable = oldvariable**(0.5).
as a replacement for
COMPUTE newvariable = sqrt(oldvariable).
Hope that helps!