Spss Functions
Spss Functions
We will illustrate some functions using the following data file that includes name, x, test1, test2, and test3.
LIST.
The variable x uses -999 to indicate missing values, and test1, test2 and test3 use -99 to indicate missing values. Below
we tell SPSS about these missing values and list out the data again.
LIST.
The output is shown below. Note that the data really does not look any different after we have defined the missing values.
But, as we will see below, SPSS does know to treat these values as missing rather than treating them as though they were -
99 and -999.
2. Math functions
Now let’s try some basic math functions. The trunc function (short for truncate) takes a number and converts it to a whole
number (integer) by removing all the decimal places, for example, 6.99 and 6.49 would become 6. By contrast,
the rnd function (short for round) rounds numbers to the nearest whole number using conventional rounding rules, for
example 6.99 would become 7, but 6.49 would become 6.
SPSS has other mathematical functions. Below we illustrate functions for getting the square root (sqrt), natural log (ln), log
to the base 10 (lg10) and exponential (exp). Note that the sqrt, ln and lg10 functions do not work with negative numbers
(for example you cannot take the square root of a negative number). SPSS will generate missing values in such cases, as
we will see below.
The results are shown below. We expected SPSS to generate missing values for xsqrt, xln and xlg10 when x was negative
and we see below that those values are displayed as a single decimal point. This is the way that SPSS shows a system
missing value. Also, we see that xsqrt, xln, xlg10 and xexp were all assigned system missing values when x was -999.
The results also included warnings like the one shown below. The one below is telling us that you cannot take the square
root of a negative number and that SPSS is going to set the result to the system missing value.
Warning # 603
The argument to the square root function is less than zero. The result has
been set to the system-missing value.
3. Statistical functions
SPSS also has statistical functions that operate on one or more variables. For example, we might want to compute the
average of the three test scores. SPSS has the MEAN function that can do that for you, as shown below.
We see the results below. Note that SPSS computed the mean of the non missing values. For Samuel Adams, that meant
that his average was the same as his score on test2 since that was the only non-missing value. We could tell SPSS to give
anyone a missing value if they have fewer than 2 valid test scores using the mean.2 function. Likewise, we could tell SPSS
that we want the mean to be missing if any of the scores were missing, by using the mean.3 function. These are illustrated
below.
As you see below, avg2 is missing for Samuel Adams, and avg3 is also missing for Samuel Adams and Chris Adraktas
because they both had some missing test scores.
In addition to the mean function, SPSS also has sum, sd, variance, min and max functions.
4. String functions
Now let’s illustrate some of the SPSS string functions. Below we create up that will be the name converted into upper
case, lo that will be the name converted to lower case, and sub that will be the third through eighth character in the persons
name. Note that we first had to use the string command to tell SPSS that up lo and sub are string variables that will have a
length of up to 14 characters. Had we omitted the string command, these would have been treated as numeric variables,
and when SPSS tried to assign a character value to the numeric variables, it would have generated an error. We also
create len that is the length of the name variable, and len2 that is the length of the persons name.
STRING up lo (A14)
/sub (A6).
COMPUTE up = UPCASE(name).
COMPUTE lo = LOWER(name).
COMPUTE sub = SUBSTR(name,3,8).
COMPUTE len = LENGTH(name).
COMPUTE len2 = LENGTH(RTRIM(name)).
The results are shown below. The results for up lo sub all as we would expect. The result for len may be a bit confusing.
The variable len does not refer to the length of the person’s name, but it refers to the length of the variable name. When
we read the data we entered name (A14) for name, giving the variable a length of 14, and that is why len is always 14. By
contrast, len2 uses the rtrim function to strip off any excess blanks, and then it takes the length of that. In the
end, len2 returns the length of the persons name, for example John Smith has a length of 10.
Let’s use SPSS string functions to get the first name and last name out of the name variable. We start by using
the index function to determine the position of the first blank space in the name. We then use the substr function to extract
the part of the name before the blank to be the first name, and the part after the blank to be the last name.
The results below show that this was successful. For example, for John Smith, the substr function extracted the first name
by taking the substring from the 1st to 4th character of name, and the last name by taking the 6th character and onward.
uniform(n) – generates a random number that is 0 or greater, and less than n from a uniform distribution.
rv.binomial(n,p) – generates a value from the binomial distribution with n trials, and with a probability of
success equal to p.
Below we generate a random number that is greater than or equal to 0, but less than 1.
NAME RANNUM
Below we generate a random number that is greater than or equal to 0, but less than 10.
NAME RAN10
The example below generates a whole number (integer) from 1 to 100. The trucn function is used to convert the result into
a whole number from 0 to 99, and then 1 is added to make it from 1 to 100.
NAME RAN100
Below we use the rv.binomial function to simulate a coin flip. It is like a coin flip since the number of trials is 1 and the
probability of success is .5 (like flipping a coin once and the probability of it coming up heads is .5). Let’s treat a 1 as coming
up heads, and a 0 as coming up tails. As we see below, Ben and John each got a head, and the others got tails.
Below, we change the number of flips to 10, and count the number of heads each person gets. John got the most heads (7)
and Ben got the fewest (4).
If we repeat the example from above using the exact same seed, we will get the same results. This is very useful for being
able to replicate results of a simulation study or Monte Carlo style study. Indeed, using the same seed did generate the
same results (see below).
SET seed=943785.
INPUT PROGRAM.
+ LOOP id = 1 to 25.
+ COMPUTE cointoss = RV.BINOMIAL( 1 , .5 ).
+ END CASE.
+ END LOOP.
+ END FILE.
END INPUT PROGRAM.
LIST CASES.
The program above creates 25 observations, each having a variable called id which is the trial number, and cointoss that
will be either 1 or 0. Even if this program does not make much sense to you, you could use it as a template to make your
own simulation. You can change the number of trials by changing 25 to the number of trials you want. You can change the
probability of success by changing the value of .5 to the value you would like. Or, you could choose an entirely different
random number generating function instead of rv.binomial you might choose uniform. The results of the program above
are shown below.
ID COINTOSS
1.00 .00
2.00 1.00
3.00 1.00
4.00 .00
5.00 .00
6.00 1.00
7.00 .00
8.00 .00
9.00 .00
10.00 .00
11.00 .00
12.00 1.00
13.00 .00
14.00 .00
15.00 .00
16.00 1.00
17.00 .00
18.00 1.00
19.00 1.00
20.00 1.00
21.00 1.00
22.00 1.00
23.00 .00
24.00 1.00
25.00 .00
Function List
This section contains a listing of all the functions in the IBM SPSS Data Collection Function Library. For full details and examples of usage,
see the Data Collection Function Library topics in the IBM® SPSS® Data Collection Scripting section of the IBM® SPSS® Data Collection
Developer Library.
Categorical Functions
Function Description
AnswerCount(Val)
Returns the number of categories selected in
a category list.
ContainsAll(Val, Answers [, Identifies whether a category list contains all
Exactly]) of the categories in a given list.
ContainsAny(Val, Answers [, Identifies whether a category list contains
Exactly]) one or more categories in a given list.
ContainsSome(Val, Answers [, Identifies whether a category list contains
Min] [, Max] [, Exactly]) some of the categories in a given list.
DefinedCategories(Val [, Returns a set of categories of a categorical
Answers]) variable.
DefinedElements(Val [, Types])
Returns a set of elements of a categorical
variable.
DefinedFactors(Val [, Returns a set of factor numbers as defined
Answers]) on the categories in a variable.
DefinedListElements(Val, Returns a set of elements of a categorical
Answers [, Types]) variable.
Function Description
Returns the difference of two category lists--
Difference(Val, Answers) that is, it returns the categories that are in
the first category list but not in the second.
Factor(Val)
Returns the factor defined for an element of
a categorical variable.
GetAnswer(Val, Index)
Returns a specified category in a category
list.
HasAnswer(Val, Answer)
Identifies whether a specified category is in a
category list.
Returns the intersection of two or more
Intersection(Val [, category lists--that is, it returns the
Vals, ...]) categories that appear in all of the category
lists.
Returns the smallest available subscript for
LBound(List [, Dimension])
the indicated dimension of an array. By
default, the lower bound is returned for the
first dimension of the array.
Returns the union of a number of categorical
Merge(Vals) values; that is, returns the categories that
are in any of the input categorical values.
ReDim(List, Size [, Preserve])
Re-sizes an array to a given size. By default,
the array contents are preserved.
Returns the largest available subscript for
UBound(List [, Dimension])
the indicated dimension of an array. By
default, the upper bound is returned for the
first dimension of the array.
Returns the union of two or more category
Union(Val [, Vals, ...]) lists--that is, it returns all of the categories
that are in one or more of the category lists.
Unique(Val)
Returns a category list with any duplicate
categories removed.
Returns the exclusive union of two category
XUnion(Val, Answers)
lists--that is, it returns all of the categories
that are in either one of the category lists,
but not in both.
Text Functions
Function Description
AscW(Val)
Returns an integer value representing the
Unicode character code for a character.
ChrW(Val)
Returns the character that corresponds to a
given Unicode character code.
Format(Val [, Style [, Returns a string that is the result of formatting a
Width [, Locale]]]) value according to a specified style.
Hex(Val)
Returns a string representing the hexadecimal
value of a number.
LCase(Val)
Returns a string that has been converted to
lower case.
LTrim(Val)
Returns a copy of a string with leading spaces
removed.
Replaces characters that are not allowed in the
MakeMDMName(Val) name of an MDM object, to produce a valid
name.
MakeString(Vals)
Returns a text string by concatenating one or
more values.
Oct(Val)
Returns a string representing the octal value of a
number.
RTrim(Val)
Returns a copy of a string with trailing spaces
removed.
Split(Val [, Delimiter [,
Count ]]) Returns an array that contains substrings.
Function Description
Trim(Val)
Returns a copy of a string with both leading
spaces and trailing spaces removed.
UCase(Val)
Returns a string that has been converted to
upper case.
Conversion Functions
Function Description
CBoolean(Val) Converts a value of any data type to a Boolean value.
CCategorical(Val) Converts a value of any data type to a Categorical value.
CDate(Val) Converts a value of any data type to a Date value.
CDouble(Val) Converts a value of any data type to a Double value.
CLong(Val) Converts a value of any data type to a Long value.
CText(Val) Converts a value of any data type to a Text value.
List Functions
Function Description
FindItem(List, Key)
Returns a specified item from a list, or NULL if
the item isn't found.
GetReversalSeed()
Returns the current reversal state (which is
used by the Rev function).
GetRotationSeed()
Returns the current rotation state (which is used
by the Rot function).
Returns a randomized copy of a list. An optional
Ran(List [, Count [,
Seed]]) parameter defines how many items from the
input list are included in the randomized list.
RanSequence(Start, End [, Returns an array containing values selected
Step [, Count[, Seed]]]) randomly from a given series of integers.
Returns an array containing copies of items
from an input list, either in the normal order or
Rev(List [, Count [, Policy
[, Seed]]]) in reverse order. The optional Count parameter
defines how many items from the input list are
included in the returned list.
RevSequence(Start, End [, Returns an array containing values selected
Step [, Count[, Policy[, from a given series of integers, either in the
Seed]]]]) original order or in reverse order.
Returns an array containing copies of items
from the input list, in a rotated order. The
Rot(List [, Count [, Policy
[, Seed]]]) optional Count parameter defines how many
items from the input list are included in the
returned list.
RotSequence(Start, End [, Returns an array containing values selected
Step [, Count[, Policy[, from a given series of integers in a "rotated"
Seed]]]]) order.
Mathematical Functions
Function Description
Abs(Val) Returns the absolute value of a number.
Atn(Val) Returns the arctangent of a number.
Cos(Val) Returns the cosine of an angle.
Exp(Val)
Returns e (the base of natural logarithms) raised to a
power.
Int(Val) Returns the integer portion of a number.
Log(Val [, Base]) Returns the logarithm of a number.
MaxOf(Val1 [,
Vals, ...]) Returns the maximum of two or more values.
MinOf(Val1 [,
Vals, ...]) Returns the minimum of two or more values.
Pow(Val1, Val2) Returns the value of a number raised to a power.
Round(Val [, Digits Returns a number rounded to a specified number of
[, Policy]]) decimal places or significant digits.
Sgn(Val)
Returns an integer that indicates the sign of a
number.
Sin(Val) Returns the sine of an angle.
Sqrt(Val) Returns the square root of a number.
Tan(Val) Returns the tangent of an angle.
Miscellaneous Functions
Function Description
Calculates categories (called bands) for a
Band(Val, Min, Size [, numeric variable and returns the appropriate
Count]) category for a specified value in the numeric
variable.
BitAnd(Val1 [, Vals, ...]) Performs a bitwise AND on two or more
Function Description
numeric values and returns the result.
BitNot(Val)
Performs a bitwise NOT on a numeric value
and returns the result.
BitOr(Val1 [, Vals, ...])
Performs a bitwise OR on two or more numeric
values and returns the result.
BitXor(Val1 [, Vals, ...])
Performs a bitwise XOR on two or more
numeric values and returns the result.
CreateObject(Class)
Creates and returns a reference to an
Automation object.
DBNull() Returns a NULL data value for use with ADO.
EngineVersion([Val [, Returns part or all of the engine's version
Number]]) number.
Calculates a specified number of equal
EqualBand(Val, Count, Min, categories (called bands) for a numeric
Max) variable and returns the appropriate category
for a specified value in the numeric variable.
Eval(Expr)
Evaluates an expression and returns the
result.
Execute(Script) Executes one or more specified statements.
GetInterface(Object,
InterfaceID) Returns an alternate interface for an object.
Returns the value passed as
IIf(Val, TruePart, FalsePart)
the TruePart parameter if the expression
evaluates to True, otherwise returns the value
passed as the FalsePart parameter.
Displays a dialog box containing a specified
InputBox(Prompt[, Default[,
Title]]) message, a text box for input, an OK button,
and a Cancel button.
IsDBNull(Val)
Returns true if a value is a NULL data value
that can be used by ADO.