Resynchronizes the input data when SAS encounters a missing or invalid record in data that has multiple records per observation.
| Valid in: | DATA step |
|---|---|
| Categories: | Action |
| CAS | |
| Type: | Executable |
Table of Contents
The LOSTCARD statement prevents SAS from reading a record from the next group when the current group has a missing record.
When SAS reads multiple records to create a single observation, it does not discover that a record is missing until it reaches the end of the data. If there is a missing record in your data, the values for subsequent observations in the SAS data set might be incorrect. Using LOSTCARD prevents SAS from reading a record from the next group when the current group has fewer records than SAS expected.
LOSTCARD is most useful when the input data have a fixed number of records per observation and when each record for an observation contains an identification variable that has the same value. LOSTCARD usually appears in conditional processing such as in the THEN clause of an IF-THEN statement, or in a statement in a SELECT group.
When LOSTCARD executes, SAS takes several steps:
This example uses the LOSTCARD statement in a conditional construct to identify missing data records and to resynchronize the input data.
data inspect;
input id 1-3 age 8-9 #2 id2 1-3 loc
#3 id3 1-3 wt;
if id ne id2 or id ne id3 then
do;
put 'DATA RECORD ERROR: ' id= id2= id3=;
lostcard;
end;
datalines;
301 32
301 61432
301 127
302 61
302 83171
400 46
409 23145
400 197
411 53
411 99551
411 139
;
The DATA step reads three input records before writing an observation. If the identification number in record 1 (variable ID) does not match the identification number in the second record (ID2) or third record (ID3), a record is incorrectly entered or omitted. The IF-THEN DO statement specifies that if an identification number is invalid, SAS prints the message that is specified in the PUT statement message and executes the LOSTCARD statement.
In this example, the third record for the second observation (ID3=400) is missing. The second record for the third observation is incorrectly entered (ID=400 while ID2=409). Therefore, the data set contains two observations with ID values 301 and 411. There are no observations for ID=302 or ID=400. The PUT and LOSTCARD statements write these statements to the SAS log when the DATA step executes.
DATA RECORD ERROR: id=302 id2=302 id3=400 NOTE: LOST CARD. RULE:----+----1----+----2----+----3----+----4----+----5----+---- 14 302 61 15 302 83171 16 400 46 DATA RECORD ERROR: id=302 id2=400 id3=409 NOTE: LOST CARD. 17 409 23145 DATA RECORD ERROR: id=400 id2=409 id3=400 NOTE: LOST CARD. 18 400 197 DATA RECORD ERROR: id=409 id2=400 id3=411 NOTE: LOST CARD. 19 411 53 DATA RECORD ERROR: id=400 id2=411 id3=411 NOTE: LOST CARD. 20 411 99551
The numbers 14, 15, 16, 17, 18, 19, and 20 are line numbers in the SAS log.