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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.