Examples (SELECT IF command)

Working With Simple Logical Expressions

SELECT IF (SEX EQ 'MALE').
  • All subsequent procedures will use only cases in which the value of SEX is MALE.
  • Because uppercase and lowercase are treated differently in comparisons of string variables, cases for which the value of SEX is male are not selected.
    SELECT IF (INCOME GT 75000 OR INCOME LE 10000).
  • The logical expression tests whether a case has a value that is either greater than 75,000 or less than or equal to 10,000. If either relation is true, the case is used in subsequent analyses.
    SELECT IF (V1 GE V2).
  • This example selects cases where variable V1 is greater than or equal to V2. If either V1 or V2 is missing, the logical expression is indeterminate, and the case is not selected.
    SELECT IF (SEX = 'F' & INCOME <= 10000).
  • The logical expression tests whether string variable SEX is equal to F and whether numeric variable INCOME is less than or equal to 10,000. Cases that meet both conditions are included in subsequent analyses. If either SEX or INCOME is missing for a case, the case is not selected.
    SELECT IF (SYSMIS(V1)).
  • The logical expression tests whether V1 is system-missing. If it is system-missing, the case is selected for subsequent analyses.
    SELECT IF (VALUE(V1) GT 0).
  • Cases are selected if V1 is greater than 0, even if the value of V1 has been declared user-missing.
    SELECT IF (V1 GT 0).
  • Cases are not selected if V1 is user-missing, even if the user-missing value is greater than 0.
    SELECT IF ((V1-15) LE (V2*(-0.001))).
  • The logical expression compares whether V1 minus 15 is less than or equal to V2 multiplied by −0.001. If it is true, the case is selected.
    SELECT IF ((YRMODA(88,13,0) - YRMODA(YVAR,MVAR,DVAR)) LE 30).
  • The logical expression subtracts the number of days representing the date (YVAR, MVAR, and DVAR) from the number of days representing the last day in 1988. If the difference is less than or equal to 30, the case is selected.

Understanding and Changing the Order of Evaluation

SELECT IF (RECEIV GT DUE AND (REVNUS GE EXPNS OR BALNCE GT 0)).
  • By default, AND is executed before OR. This expression uses parentheses to change the order of evaluation.
  • The program first tests whether variable REVNUS is greater than or equal to variable EXPNS, or variable BALNCE is greater than 0. Second, the program tests whether RECEIV is greater than DUE. If one of the expressions in parentheses is true and RECEIV is greater than DUE, the case is selected.
  • Without the parentheses, the program would first test whether RECEIV is greater than DUE and REVNUS is greater than or equal to EXPNS. Second, the program would test whether BALNCE is greater than 0. If the first two expressions are true or if the third expression is true, the case is selected.

Selecting Cases Based on Their Sequence in a File

COMPUTE  #CASESEQ=#CASESEQ+1.
SELECT IF (MOD(#CASESEQ,2)=0).
  • This example computes a scratch variable, #CASESEQ, containing the sequence numbers for each case. Every other case is selected, beginning with the second case.
  • #CASESEQ must be a scratch variable so that it is not reinitialized for every case. An alternative is to use the LEAVE command.

Using SELECT IF within a DO IF Structure

DO IF  SEX EQ 'M'.
+   SELECT IF PRESTIGE GT 50.
ELSE IF  SEX EQ 'F'.
+   SELECT IF PRESTIGE GT 45.
END IF.
  • The SELECT IF commands within the DO IF structure select males with prestige scores above 50 and females with prestige scores above 45.