SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

Quick Overview SPSS Operators and Functions

The table below presents a quick overview of functions and operators in SPSS, sorted by type (numeric, string, ...). Details and examples for some lesser known functions are covered below the table.

TYPE FunctionDESCRIPTIONEXAMPLE
Comparison= (or EQ)Equal toif(var01 = 0) var02 = 1.
Comparison<> (or NE)Not equal toif(var01 <> 0) var02 = 1.
Comparison< (or LT)Less thanif(var01 < 0) var02 = 1.
Comparison<= (or LE)At mostif(var01 <= 0) var02 = 1.
Comparison> (or GT)Greater thanif(var01 > 0) var02 = 1.
Comparison>= (or GE)At leastif(var01 >= 0) var02 = 1.
ComparisonRANGEWithin rangeif(range(score,0,20) grp01 = 1.
ComparisonANY First value among second, third, ... values?if(any(nation,1,3,5)) flag01 = 1.
Logical& (or AND)All arguments true?if(sex = 0 & score >= 100) grp01 = 1.
Logical| (or OR)At least 1 argument true?if(sex = 0 | score <= 90) grp01 = 1.
LogicalNOTArgument not trueselect if(not(missing(score))).
Numeric+Additioncompute sum01 = var01 + var02.
Numeric-Subtractioncompute dif01 = var01 - var02.
Numeric*Multiplicationcompute revenue = sales * price.
Numeric/Divisioncompute avg01 = sum01 / trials.
Numeric**Exponentiationcompute square01 = var01**2.
NumericSQRTSquare rootcompute root01 = sqrt(var01).
NumericRND Roundcompute score = rnd(reactime).
NumericMOD Modulo Functioncompute cntr = mod(id,4).
NumericTRUNC Truncatecompute score = trunc(reactime).
NumericABSAbsolute valuecompute abs01 = abs(score).
NumericEXPExponential Functioncompute escore = exp(score).
NumericLNNatural logarithmcompute lnscore = ln(score).
StatisticalMINMinimum over variablescompute min01 = min(var01 to var10).
StatisticalMAXMaximum over variablescompute max01 = max(var01 to var10).
StatisticalSUMSum over variablescompute total = sum(var01 to var10).
StatisticalMEAN Mean over variablescompute m01 = mean(var01 to var10).
StatisticalMEDIANMedian over variablescompute me01 = median(var01 to var10).
StatisticalVARIANCEVariance over variablescompute vnc01 = variance(var01 to var10).
StatisticalSD Standard deviation over variablescompute sd01 = sd(var01 to var10).
MissingMISSINGSystem or user missingselect if(missing(score)).
MissingSYSMISSystem missingselect if(not(sysmis(score))).
MissingNMISSCount missing values over variablescompute mis01 = nmiss(v01 to v10).
MissingNVALIDCount valid values over variablescompute val01 = nvalid(v01 to v10).
StringLOWERConvert to lowercasecompute sku = lower(sku).
StringUPCASEConvert to uppercasecompute sku = upcase(sku).
StringCHAR.LENGTHNumber of characters in stringcompute len01 = char.length(firstname).
StringCHAR.INDEXPosition of first occurrence of substringcompute pos01 = char.index('banana','a').
StringCHAR.RINDEXPosition of last occurrence of substringcompute pos02 = char.rindex('banana','a').
StringCHAR.SUBSTRExtract substringcompute firstchar = char.substr(name,1,1).
StringCONCATConcatenate stringscompute name = concat(fname,' ',lname).
StringREPLACEReplace substringcompute str01 = replace('dog','g','t').
StringRTRIMRight trim stringcompute str02 = rtrim(str02).
StringLTRIMLeft trim stringcompute str03 = ltrim(str03).
DateDATE.DMYConvert day, month, year into datecompute mydate = date.dmy(31,1,2024).
DateDATEDIFFCompute difference between dates in chosen time unitscompute age = datediff(datevar02,datevar01,'years').
DateDATESUMAdd time units to datecompute followup = datesum(datevar01,100,'days').
DateXDATEExtract date component from datecompute byear = xdate.year(bdate).
TimeTIME.HMSConvert hours, minutes, seconds into timecompute time01 = time.hms(17,45,12).
DistributionCDF Cumulative probability distribution or density Functioncompute pvalue = cdf.normal(-1.96,0,1).
DistributionIDF Inverse probability distribution or density Functioncompute zvalue95 = idf.normal(.025,0,1).
DistributionPDFProbability distribution or density Functioncompute prob = pdf.binom(0,10,.5).
DistributionRVDraw (pseudo) random numbers from specified probability distribution or density Functioncompute rand01 = rv.uniform(0,1).
OtherLAGRetrieve value from previous casecompute prev = lag(varname).
OtherNUMBER Convert string to numbercompute nvar = number(svar,f3).
OtherSTRINGConvert number to stringcompute svar = string(nvar,f3).
OtherVALUELABEL Set value labels as string valuescompute svar = valuelabel(nvar).

SPSS ANY Function Example

In SPSS, ANY evaluates if the first value is among the second, third, ... values. So for example, let's say we want to know if the completion day was a Monday, Wednesday or a Friday? We could use if(cday = 2 or cday = 4 or cday = 6) flag01 = 1. However, a nice shorthand here is if(any(cday,2,4,6)) flag01 = 1. The screenshot below shows the result when run on spss-functions.sav.

SPSS Any Function Example Result of IF(ANY(CDAY,2,4,6)) FLAG01 = 1.

As a second example, let's flag all cases who scored at least one 1 among the last 5 variables. This is often done with COUNT and then RECODE but a shorter option is if(any(1,q1 to q5)) flag02 = 1. which checks if the value 1 is among variables q1 to q5.

SPSS RND Function Example

In SPSS, you can round a value x to some constant c which is 1 by default. Like so,

So for rounding salaries to dollars, you could use compute salary = rnd(salary). Alternatively, use compute salary = rnd(salary,.01). for rounding to dollar cents. For rounding salaries to thousands of dollars, use compute salary = rnd(salary,1000). as shown below when run on spss-functions.sav.

SPSS Round Numbers Example Result of COMPUTE SALARY = RND(SALARY,1000).

SPSS MOD Function Example

In SPSS, MOD is short for the modulo function where MOD(X,Y) returns the remainder of X after subtracting Y from it as many times as possible. This comes in handy for creating a trial counter if each respondent has the same number of trials as in compute trial = mod(($casenum - 1),4) + 1. When run on spss-functions.sav, the result is shown below.

SPSS Mod Function Example Trial counter created by COMPUTE TRIAL = MOD(($CASENUM - 1),4) + 1.

SPSS TRUNC Function Example

In SPSS, you can truncate (“round down”) a value x to some constant c which is 1 by default. Like so,

TRUNC comes in handy for creating a respondent identifier if each respondent has the same number of trials as in compute respid = trunc(($casenum - 1) / 4) + 1. The result is shown below when run on spss-functions.sav.

SPSS Trunc Example Result from COMPUTE RESPID = TRUNC(($CASENUM - 1) / 4) + 1.

SPSS MEAN Function Example

In SPSS, MEAN is pretty straightforward but there's 2 things you should know: first off, if there's any missing values, then

$$mean = \frac{sum(valid\;values)}{number\;of\;valid\;values}$$

This is important because the number of valid values may differ over respondents.

Second, you can restrict MEAN to a minimal number of valid values, k, by using MEAN.k(...). So for spss-functions.sav, compute m01 = mean.5(q1 to q5). only computes mean scores over cases not having any missing values over these 5 variables as shown below.

SPSS Compute Means With Missing Values Result from COMPUTE M01 = MEAN.5(Q1 TO Q5).

SPSS SD Function Example

In SPSS, SD computes the standard deviation over variables. It has the same properties discussed for MEAN. SD comes in handy for detecting “straightliners” (respondents giving the same answer to all or most questions). Like so, compute sd01 = sd(q1 to q5). quickly does the job for spss-functions.sav.

SPSS Sd Function Example Detecting straightliners with COMPUTE SD01 = SD(Q1 TO Q5).

SPSS CDF Function Example

CDF is short for cumulative (probability) density (or distribution) function: it returns $$P(X \le x)$$,
given some probability density function. For example, assuming that z follows a standard normal distribution, what's the 2-tailed p-value for z = -2.0? We can find this out by running compute pvalue = 2 * cdf.normal(-2,0,1). as shown below.

SPSS Compute Pvalue

SPSS IDF Function Example

IDF is short for inverse (probability) density (or distribution) function: it returns a critical value for some chosen probability, given a density function. Note that this is exactly what we do when computing confidence intervals. For example: which z-value has a cumulative probability of .025? Well, we can compute this by compute zcrit = idf.normal(.025,0,1). as shown below.

SPSS Compute Critical Value

SPSS NUMBER Function Example

In SPSS, NUMBER converts a string variable into a (new) numeric one. With regard to spss-functions.sav, compute nage = number(age,f2). creates a numeric age variable based on the string variable containing age.

SPSS Number Function Example Result from COMPUTE NAGE = NUMBER(AGE,F2). Note the illegal character for case 11.

Importantly, choosing the f2 format results in SPSS ignoring all but the first 2 characters. If we choose f3 instead as in compute nage = number(age,f3). then SPSS throws the following warning:

>Warning # 1102
>An invalid numeric field has been found. The result has been set to the
>system-missing value.
>Command line: 117 Current case: 11 Current splitfile group: 1
>Field contents: '27a'

This is because the age for case 11 contains an illegal character, resulting in a system missing value. Sadly, when converting this variable with ALTER TYPE, this value simply disappears from your data
without any warning or error.
In our opinion, this really is a major stupidity in SPSS and very tricky indeed.

SPSS VALUELABEL Function Example

The VALUELABEL function sets the value labels for some variable as the values for some string variable. The syntax below illustrates how it's done for spss-functions.sav.

*DECLARE NEW STRING VARIABLE WITH LENGTH 10.
string sday(a10).

*SET VALUE LABELS FOR CDAY AS VALUES.
compute sday = valuelabel(cday).
execute.

Result

SPSS Valuelabel Function Result Result from COMPUTE SDAY = VALUELABEL(CDAY).

Final Notes

Now honestly, our overview of SPSS operators and functions is not 100% comprehensive. I did leave out some examples that are so rare that covering them mostly just clutters up the table without helping anybody.

If you've any questions or remarks, please leave a comment below. And other than that:

thanks for reading!

Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.

THIS TUTORIAL HAS 21 COMMENTS: