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.

  • FIRST and LAST must follow all TABLE and FILE subcommands and any associated RENAME and IN subcommands.
  • FIRST and LAST have only one specification—the name of the flag variable.
  • FIRST creates a variable with the value 1 for the first case of each group and the value 0 for all other cases.
  • LAST creates a variable with the value 1 for the last case of each group and the value 0 for all other cases.
  • Variables created by FIRST and LAST are 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, FIRST or LAST can 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 FILES is used instead of GET to read the data file persons.sav. The BY subcommand identifies the key variable (HOUSEID), and FIRST creates the variable HEAD with the value 1 for the first case in each household and the value 0 for all other cases.
  • SELECT IF selects only the cases with the value 1 for HEAD, and the CROSSTABS procedure is run on these cases.