Vous êtes sur la page 1sur 2

A Closer Look at FIRST.var and LAST.

var
Marge Scerbo
Center for Health Program Development and Management, UMBC

ABSTRACT
Understanding how to use the temporary SAS variables,
FIRST.var and LAST.var, is straightforward when there is
only one BY group defined. But as more BY variables are
added, the correct analysis of these temporary variables
may become cloudy and cause their misuse. This paper
will step through the creation of a table to demonstrate how
these variables are set and how to use them in a data step.
INTRODUCTION
Table 1 on the next page contains a portion of inpatient
admission and discharge data. This dataset has been
cleaned and validated, and then sorted in final order by
RECID (the patient number), DOSAD (the admission
date) and DOSDIS (the discharge date). There can be
multiple records for one patient as well as multiple
observations for one date of admission.
STEP 1
To identify the first record for each patient is
straightforward. Since the dataset has been sorted in
patient number order, the following data step statement can
identify the first record for each:
DATA step1;
SET paper2;
BY recid;
*IF first.recid is equal to IF first.recid = 1;
IF first.recid then firstpat = 1;
RUN;
When SAS encounters the first patient number, the
temporary SAS variable, FIRST.RECID, is automatically
set to 1. For all other records, this variable is set to 0.
Those patient records are clearly identified. The same
would be true for identifying the last patient number
(LAST.RECID). The results are shown in Table 2.
STEP 2
The next step would be to identify each new date of
admission. The following code:

DATA step2;
SET paper2;
BY recid dosad dosdis;
IF first.dosad then firstad = 1;
RUN;
Table 3 displays the outcome of this code. Each new date
of admission is highlighted. If this is the intent of the code,
then the solution is correct. For example, the analysis may
have included a study of the number of admissions one
patient might have over a period of time. The above code
could have been expanded, and totals and subtotals on the
first and/or last date of admission could have been coded.
But, if the purpose was to identify the first admission date
for each patient number, then the outcome is false.
STEP 3
To clearly identify the first date of admission for each
patient number, the following code could be used:
DATA step3;
SET paper2;
BY recid dosad dosdis;
IF first.recid and first.dosad then frecad = 1;
RUN;
This code defines each record which is the first date of
admission for each patient. Compare the outcome of this
code (Table 4) to that of Step 1 (Table 1), which identifies
the first record for each patient number. The results are the
same. Since the data are sorted by patient number as well
as date of admission, by identifying the first record for each
patient number, the first date of admission for each patient
is also identified.
Conclusion
If it is not clear exactly what the results of the definition the
first or last record of a particular BY group might be,
write a test program to create flag variables which are set
by the FIRST.var and LAST.var. Look at the data and
clarify that the correct record is being identified.

TABLE 1:
Set paper2;
By recid dosad dosdis;
ID Number Admission Discharge
001210
10/07/94 10/20/94
001210
05/03/95 05/07/95
001210
05/03/95 05/08/95
001210
05/04/95 05/08/95
001210
05/04/95 05/09/95
004530
05/16/95 05/20/95
004530
05/20/95 05/23/95
007190
01/02/95 01/05/95
007190
01/03/95 01/05/95
007190
01/03/95 01/05/95
012170
07/25/94 07/29/94
012170
07/25/94 07/30/94
012170
07/26/94 07/30/95
024160
08/09/94 09/15/94
024160
08/10/94 09/11/94
024160
08/11/94 09/11/94
024160
08/11/94 09/15/94
025370
12/21/94 12/21/94
025370
01/24/95 01/25/95
025370
02/07/95 02/10/95
025370
02/18/95 02/20/95
025370
03/14/95 03/15/95
025370
04/16/95 04/19/95
025370
05/02/95 05/02/95
025370
05/12/95 05/23/95
025370
05/26/95 05/28/95

TABLE 2:

TABLE 3:

TABLE 4:

if first.recid;

if first.dosad;

if first.recid & first.dosad;

if last.recid;

if last.dosad;

if last.recid & last.dosad;

ID Number
001210
001210
001210
001210
001210
004530
004530
007190
007190
007190
012170
012170
012170
024160
024160
024160
024160
025370
025370
025370
025370
025370
025370
025370
025370
025370

Admission Discharge
10/07/94 10/20/94
05/03/95 05/07/95
05/03/95 05/08/95
05/04/95 05/08/95
05/04/95 05/09/95
05/16/95 05/20/95
05/20/95 05/23/95
01/02/95 01/05/95
01/03/95 01/05/95
01/03/95 01/05/95
07/25/94 07/29/94
07/25/94 07/30/94
07/26/94 07/30/95
08/09/94 09/15/94
08/10/94 09/11/94
08/11/94 09/11/94
08/11/94 09/15/94
12/21/94 12/21/94
01/24/95 01/25/95
02/07/95 02/10/95
02/18/95 02/20/95
03/14/95 03/15/95
04/16/95 04/19/95
05/02/95 05/02/95
05/12/95 05/23/95
05/26/95 05/28/95

Id Number
001210
001210
001210
001210
001210
004530
004530
007190
007190
007190
012170
012170
012170
024160
024160
024160
024160
025370
025370
025370
025370
025370
025370
025370
025370
025370

Admission Admission
10/07/94
10/07/94
05/03/95
05/03/95
05/03/95
05/03/95
05/04/95
05/04/95
05/04/95
05/04/95
05/16/95
05/16/95
05/20/95
05/20/95
01/02/95
01/02/95
01/03/95
01/03/95
01/03/95
01/03/95
07/25/94
07/25/94
07/25/94
07/25/94
07/26/94
07/26/94
08/09/94
08/09/94
08/10/94
08/10/94
08/11/94
08/11/94
08/11/94
08/11/94
12/21/94
12/21/94
01/24/95
01/24/95
02/07/95
02/07/95
02/18/95
02/18/95
03/14/95
03/14/95
04/16/95
04/16/95
05/02/95
05/02/95
05/12/95
05/12/95
05/26/95
05/26/95

ID Number
001210
001210
001210
001210
001210
004530
004530
007190
007190
007190
012170
012170
012170
024160
024160
024160
024160
025370
025370
025370
025370
025370
025370
025370
025370
025370

Admission Discharge
10/07/94
10/20/94
05/03/95
05/07/95
05/03/95
05/08/95
05/04/95
05/08/95
05/04/95
05/09/95
05/16/95
05/20/95
05/20/95
05/23/95
01/02/95
01/05/95
01/03/95
01/05/95
01/03/95
01/05/95
07/25/94
07/29/94
07/25/94
07/30/94
07/26/94
07/30/95
08/09/94
09/15/94
08/10/94
09/11/94
08/11/94
09/11/94
08/11/94
09/15/94
12/21/94
12/21/94
01/24/95
01/25/95
02/07/95
02/10/95
02/18/95
02/20/95
03/14/95
03/15/95
04/16/95
04/19/95
05/02/95
05/02/95
05/12/95
05/23/95
05/26/95
05/28/95

Vous aimerez peut-être aussi