BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AMIN_ALJALLAD
Calcite | Level 5
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;
1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

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:

View solution in original post

2 REPLIES 2
RichardDeVen
Barite | Level 11

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:

AMIN_ALJALLAD
Calcite | Level 5

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 841 views
  • 0 likes
  • 2 in conversation