SPSS Tutorials


Restrict Command to Variables having Minimum Valid N


"I'd like to see the frequency distributions of some variables. However, I'd like variables with a small number of valid observations to be excluded automatically. Is there an easy way to accomplish this?"

SPSS Python Syntax Example

*Quick check to see which variables have valid N >= 90.

desc r1 to r5.

*Automatically restrict FREQUENCIES command to just those variables.

begin program.
variables = 'r1 to r5' # Specify variables
minValids = 90 # Specify minimum number of valids for inclusion
import spssaux,spss
vDict = spssaux.VariableDict(caseless=True)
vList = vDict.expand(variables)
cmd = spssaux.CreateXMLOutput('descriptives %s.'%" ".join(vList))
valids = spssaux.GetValuesFromXMLWorkspace(cmd,'Descriptive Statistics',colCategory = 'N',cellAttrib = 'number')
include = [vList[i] for i in range(len(valids) - 1) if int(valids[i]) >= minValids]
if include:
    spss.Submit('frequencies %s.'%" ".join(include))
end program.


Previous tutorial: Insert Values from Output Tables into Text

Next tutorial: SPSS Missing Values Functions

Let me know what you think!

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