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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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