Explanation : This example shows how SAS uses FIRST.<variable> and LAST.<variable> to mark the beginning and end of BY groups. FIRST. and LAST. variables are temporary variables automatically created by SAS, representing the start and end of each BY group. They can be referenced in the DATA step but are not part of the output dataset. The automatic variable _N_ is used as a counter for DATA step iterations.
Copied!
data zip;
input State $ City $ ZipCode Street $20-29;
datalines;
FL Miami 33133 Rice St
FL Miami 33133 Thomas Ave
FL Miami 33133 Surrey Dr
FL Miami 33133 Trade Ave
FL Miami 33146 Nervia St
FL Miami 33146 Corsica St
FL Lakeland 33801 French Ave
FL Lakeland 33809 Egret Dr
AZ Tucson 85730 Domenic Ln
AZ Tucson 85730 Gleeson Pl
;
proc sort data=zip;
by State City ZipCode;
run;
data zip2;
set zip;
by State City ZipCode;
put _n_= City State ZipCode
first.city= last.city=
first.state= last.state=
first.ZipCode= last.ZipCode= ;
run;
1
DATA zip;
2
INPUT State $ City $ ZipCode Street $20-29;
3
DATALINES;
4
FL Miami 33133 Rice St
5
FL Miami 33133 Thomas Ave
6
FL Miami 33133 Surrey Dr
7
FL Miami 33133 Trade Ave
8
FL Miami 33146 Nervia St
9
FL Miami 33146 Corsica St
10
FL Lakeland 33801 French Ave
11
FL Lakeland 33809 Egret Dr
12
AZ Tucson 85730 Domenic Ln
13
AZ Tucson 85730 Gleeson Pl
14
;
15
PROC SORTDATA=zip;
16
BY State City ZipCode;
17
RUN;
18
19
DATA zip2;
20
SET zip;
21
BY State City ZipCode;
22
put _n_= City State ZipCode
23
first.city= last.city=
24
first.state= last.state=
25
first.ZipCode= last.ZipCode= ;
26
RUN;
2 Code Block
DATA STEP / PROC SORT Data
Explanation : This example illustrates that each BY variable creates temporary variables (FIRST.State, LAST.State, FIRST.City, LAST.City, FIRST.ZipCode, and LAST.ZipCode). FIRST. and LAST. variables are temporary variables automatically created by SAS, representing the start and end of each BY group. They can be referenced in the DATA step but are not part of the output dataset. The automatic variable _N_ is used as a counter for DATA step iterations.
Copied!
data zip;
input State $ City $ ZipCode Street $20-29;
datalines;
FL Miami 33133 Rice St
FL Miami 33133 Thomas Ave
FL Miami 33133 Surrey Dr
FL Miami 33133 Trade Ave
FL Miami 33146 Nervia St
FL Miami 33146 Corsica St
FL Lakeland 33801 French Ave
FL Lakeland 33809 Egret Dr
AZ Tucson 85730 Domenic Ln
AZ Tucson 85730 Gleeson Pl
;
proc sort data=zip;
by City State ZipCode;
run;
data zip2;
set zip;
1
DATA zip;
2
INPUT State $ City $ ZipCode Street $20-29;
3
DATALINES;
4
FL Miami 33133 Rice St
5
FL Miami 33133 Thomas Ave
6
FL Miami 33133 Surrey Dr
7
FL Miami 33133 Trade Ave
8
FL Miami 33146 Nervia St
9
FL Miami 33146 Corsica St
10
FL Lakeland 33801 French Ave
11
FL Lakeland 33809 Egret Dr
12
AZ Tucson 85730 Domenic Ln
13
AZ Tucson 85730 Gleeson Pl
14
;
15
PROC SORTDATA=zip;
16
BY City State ZipCode;
17
RUN;
18
19
DATA zip2;
20
SET zip;
3 Code Block
DATA STEP Data
Explanation : This example demonstrates that a change in a previous value can affect the value of FIRST.<variable>, even if the current value of the variable remains the same. The values of FIRST.<variable> and LAST.<variable> depend on the sort order and the value of the BY variable. For observation 3, the value of FIRST.Y is set to 1 because BLUEBERRY is a new value for Y, which also causes FIRST.Z to be set to 1, even though the value of Z has not changed. FIRST. and LAST. variables are temporary, automatically created by SAS, and represent the start and end of each BY group.
Copied!
data fruit;
input x $ y $ 10-18 z $ 21-29;
datalines;
apple banana coconut
apple banana coconut
apple blueberry citron
apricot blueberry citron
;
data _null_;
set fruit;
by x y z;
if _N_=1 then put 'Grouped by X Y Z';
put _N_= x= first.x= last.x= first.y= last.y= first.z= last.z= ;
run;
data _null_;
set fruit;
by y x z;
if _N_=1 then put 'Grouped by Y X Z';
put _N_= first.y= last.y= first.x= last.x= first.z= last.z= ;
run;
1
DATA fruit;
2
INPUT x $ y $ 10-18 z $ 21-29;
3
DATALINES;
4
apple banana coconut
5
apple banana coconut
6
apple blueberry citron
7
apricot blueberry citron
8
;
9
10
DATA _null_;
11
SET fruit;
12
BY x y z;
13
IF _N_=1THEN put 'Grouped by X Y Z';
14
put _N_= x= first.x= last.x= first.y= last.y= first.z= last.z= ;
15
RUN;
16
17
DATA _null_;
18
SET fruit;
19
BY y x z;
20
IF _N_=1THEN put 'Grouped by Y X Z';
21
put _N_= first.y= last.y= first.x= last.x= first.z= last.z= ;
22
RUN;
4 Code Block
DATA STEP / PROC SORT Data
Explanation : This example calculates the annual payroll by department. It uses IF-THEN statements and the values of the automatic FIRST.<variable> and LAST.<variable> variables to reset the PAYROLL value to 0 at the beginning of each BY group and to write an observation after processing the last observation of a BY group. FIRST. and LAST. variables are temporary and automatically created by SAS. The IF/THEN statement executes statements conditionally.
Copied!
data salaries;
input Department $ Name $ WageCategory $ WageRate;
datalines;
BAD Carol Salaried 20000
BAD Elizabeth Salaried 5000
BAD Linda Salaried 7000
BAD Thomas Salaried 9000
BAD Lynne Hourly 230
DDG Jason Hourly 200
DDG Paul Salaried 4000
PPD Kevin Salaried 5500
PPD Amber Hourly 150
PPD Tina Salaried 13000
STD Helen Hourly 200
STD Jim Salaried 8000
;
proc sort data=salaries out=temp;
by Department;
run;
data budget (keep=Department Payroll);
set temp;
by Department;
if WageCategory='Salaried' then YearlyWage=WageRate*12;
else if WageCategory='Hourly' then YearlyWage=WageRate*2000;
if first.Department then Payroll=0;
Payroll+YearlyWage;
if last.Department;
run;
proc print data=budget;
format Payroll dollar10.;
title 'Annual Payroll by Department';
run;
1
DATA salaries;
2
INPUT Department $ Name $ WageCategory $ WageRate;
3
DATALINES;
4
BAD Carol Salaried 20000
5
BAD Elizabeth Salaried 5000
6
BAD Linda Salaried 7000
7
BAD Thomas Salaried 9000
8
BAD Lynne Hourly 230
9
DDG Jason Hourly 200
10
DDG Paul Salaried 4000
11
PPD Kevin Salaried 5500
12
PPD Amber Hourly 150
13
PPD Tina Salaried 13000
14
STD Helen Hourly 200
15
STD Jim Salaried 8000
16
;
17
18
PROC SORTDATA=salaries out=temp;
19
BY Department;
20
RUN;
21
22
DATA budget (keep=Department Payroll);
23
SET temp;
24
BY Department;
25
IF WageCategory='Salaried'THEN YearlyWage=WageRate*12;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
« In the SAS DATA step, the automatic variables FIRST.variable and LAST.variable are the primary mechanism for high-performance data summarization. These flags allow you to detect boundaries between groups (like Departments or ZipCodes) without needing complex look-ahead logic. By identifying the exact moment a group starts or ends, you can perform conditional calculations—such as resetting counters, calculating running totals, or outputting unique summary records. »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.