proc sort data=pg2.np_acres out=sortedAcres(keep=Region ParkName State GrossAcres); by Region ParkName; run; data multiState singleState; set sortedAcres; by Region ParkName; if First.ParkName=1 and Last.ParkName=1 then output singleState; else output multiState; format GrossAcres comma15.; run;
last.<variable>
(and corresponding first.<variable>
) are two automatic temporary variables created for each BY
variable and indicate if the current row is at a group edge row; either the first or last row of a by-group level.
You can infer information about where the current observation is within the group
FIRST. LAST. where
1 at first in group
0 not at first in group
1 at last in group
0 not at last in group
1 0 group has >= 2 rows and currently at first in group
0 0 group has >= 3 rows and currently in the middle part
0 1 group has >= 2 rows and currently at last in group
1 1 group has only 1 row
Learning about the underlying premises of the implicit loop fundamental to DATA/SET
processing will make you a better SAS coder:
last.<variable>
(and corresponding first.<variable>
) are two automatic temporary variables created for each BY
variable and indicate if the current row is at a group edge row; either the first or last row of a by-group level.
You can infer information about where the current observation is within the group
FIRST. LAST. where
1 at first in group
0 not at first in group
1 at last in group
0 not at last in group
1 0 group has >= 2 rows and currently at first in group
0 0 group has >= 3 rows and currently in the middle part
0 1 group has >= 2 rows and currently at last in group
1 1 group has only 1 row
Learning about the underlying premises of the implicit loop fundamental to DATA/SET
processing will make you a better SAS coder:
Thank you for your contribution.
My concern at first is how the both is set to 1 at the same time. However, I discovered that when the value id unique, then first and last variable will be 1
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.