In SAS, the FIRST. and LAST. automatic variables are used within a DATA step to identify the first and last occurrences of observations within a BY group. These variables are particularly useful when working with sorted data or when you need to perform specific operations on the first or last observation of a group. Here’s how you can use FIRST. and LAST. with examples:
Syntax:
FIRST.variable
: Returns a value of 1 for the first observation in a BY group, and 0 otherwise.LAST.variable
: Returns a value of 1 for the last observation in a BY group, and 0 otherwise.
Example:
Suppose we have a dataset named sales
with the following structure:
cssCopy codeID Product Revenue
1 A 100
2 A 150
3 B 200
4 B 180
5 B 220
We want to calculate the total revenue for each product and display it only for the first and last observations of each product group.
SAS Code:
DATA sales_summary;
SET sales;
BY Product;
/* Calculate total revenue for each product */
IF FIRST.Product THEN Total_Revenue = 0; /* Initialize Total_Revenue for the first observation */
Total_Revenue + Revenue; /* Accumulate revenue for each observation */
/* Output total revenue only for the last observation of each product group */
IF LAST.Product THEN OUTPUT;
RUN;
Explanation:
- The
BY Product;
statement ensures that the data is processed in groups based on theProduct
variable. - The
IF FIRST.Product THEN Total_Revenue = 0;
statement initializes theTotal_Revenue
variable to 0 for the first observation of each product group. - The
Total_Revenue + Revenue;
statement accumulates the revenue for each observation within the product group. - The
IF LAST.Product THEN OUTPUT;
statement outputs the total revenue only for the last observation of each product group.
Output (sales_summary dataset):
Product Total_Revenue
A 250
B 600
In this example, FIRST. and LAST. are used to identify the first and last observations within each product group. The total revenue is calculated and displayed only for the last observation of each product group. This technique allows you to perform specific operations or calculations based on the position of observations within BY groups in SAS.
Leave a Reply