FIRST and LAST Subcommands (MATCH FILES command)
FIRST and LAST create logical variables that flag
the first or last case of a group of cases with the same value for
the BY variables.
-
FIRSTandLASTmust follow allTABLEandFILEsubcommands and any associatedRENAMEandINsubcommands. -
FIRSTandLASThave only one specification—the name of the flag variable. -
FIRSTcreates a variable with the value 1 for the first case of each group and the value 0 for all other cases. -
LASTcreates a variable with the value 1 for the last case of each group and the value 0 for all other cases. - Variables created by
FIRSTandLASTare automatically attached to the end of the resulting file and cannot be dropped. - If one file has several cases with the same values
for the key variables,
FIRSTorLASTcan be used to create a variable that flags the first or last case of the group.
Example
MATCH FILES TABLE='/data/house.sav'
/FILE='/data/persons.sav'
/BY=HOUSEID /FIRST=HEAD.
- The variable HEAD contains the value 1 for the first person in each household and the value 0 for all other persons. Assuming that the persons.sav file is sorted with the head of household as the first case for each household, the variable HEAD identifies the case for the head of household.
Example
* Using match files with only one file.
* This example flags the first of several cases with
the same value for a key variable.
MATCH FILES FILE='/data/persons.sav'
/BY HOUSEID /FIRST=HEAD.
SELECT IF (HEAD EQ 1).
CROSSTABS JOBCAT BY SEX.
-
MATCH FILESis used instead ofGETto read the data file persons.sav. TheBYsubcommand identifies the key variable (HOUSEID), andFIRSTcreates the variable HEAD with the value 1 for the first case in each household and the value 0 for all other cases. -
SELECT IFselects only the cases with the value 1 for HEAD, and theCROSSTABSprocedure is run on these cases.