FILE Subcommand (REREAD command)
FILE
specifies
an external raw data file from which the next DATA LIST
command reads data.
- The default file is the file specified on the immediately
preceding
DATA LIST
command. - If the file specified on
FILE
is not the default file, the same file must be specified on the nextDATA LIST
. Otherwise, theFILE
subcommand is ignored and theDATA LIST
command reads the next record from the file specified on it or, if no file is specified, from the file specified on the previousDATA LIST
command.
Example
INPUT PROGRAM.
DATA LIST FILE=UPDATE END=#EOF NOTABLE
/#ID 1-3. /*Get rep ID in new sales file.
DATA LIST FILE = SALESREP NOTABLE
/ID 1-3 SALES 4-11(F,2)
NEWSALE 12-19(F,2). /*Get rep record from primary file.
LOOP IF #EOF OR (#ID GT ID). /*If UPDATE ends or no new sales made.
+ COMPUTE NEWSALE = 0. /*Set NEWSALE to 0
+ END CASE. /*Build a case.
+ DATA LIST FILE = SALESREP NOTABLE
/ID 1-3 SALES 4-11(F,2)
NEWSALE 12-19(F,2). /*Continue reading primary file.
END LOOP
DO IF NOT #EOF. /*If new sales made.
+ REREAD FILE=UPDATE COLUMN = 4. /*Read new sales from UPDATE.
+ DATA LIST FILE=UPDATE
/NEWSALE 1-8(F,2).
+ COMPUTE SALES=SALES+NEWSALE. /*Update primary file.
END IF.
END CASE. /*Build a case.
END INPUT PROGRAM.
LIST.
- This example uses
REREAD
to merge two raw data files (SALESREP and UPDATE). - Both files are sorted by sales representative ID number. The UPDATE file contains only records for sales representatives who have made new sales, with variables ID and NEWSALE. The primary file SALESREP contains records for all sales representatives, with variables SALES (which contains year-to-date sales) and NEWSALE (which contains the update values each time the file is updated).
- If a sales representative has made no new sales, there is no matching ID in the UPDATE file. When UPDATE is exhausted or when the ID’s in the two files do not match, the loop structure causes the program to build a case with NEWSALE equal to 0 and then continue reading the primary file.
- When the ID’s match (and the UPDATE file is not yet exhausted), the
REREAD
command is executed. The followingDATA LIST
rereads the record in UPDATE that matches the ID variable. NEWSALE is read from the UPDATE file starting from column 4 and SALES is updated. Note that the followingDATA LIST
specifies the same file. - When the updated base is built, the program returns to the first
DATA LIST
command in the input program and reads the next ID from the UPDATE file. If the UPDATE file is exhausted (#EOF=1), the loop keeps reading records from the primary file until it reaches the end of the file. - The same task can be accomplished using
MATCH FILES
. WithMATCH FILES
, the raw data must be read and saved as IBM® SPSS® Statistics data files first.