BookmarkSubscribeRSS Feed
themanwith2thum
Calcite | Level 5

Hello I have a data set which has a variable ID with the client ID. I want to identify the fifth occurrence of that ID. For example if my data set looks like this I want to populate a variable called "INDICATOR" which will populate a value of 1 on the fifth occurrence. I know of the function first where I could use first.ID to do this for the first time the ID shows up, but is there a fifth function as well? Thanks in advance!

 

ID      NAME INDICATOR

001    apple

001    apple

001    apple

001    apple

001    apple    1

023    bb

023    bb

023    bb

023    bb

023    bb        1

 

5 REPLIES 5
Kurt_Bremser
Super User
data want;
set have;
by id;
if first.id
then count = 1;
else count + 1;
if count = 5 then indicator = 1;
drop count;
run;

The SUM statement

count + 1;

causes an implicit RETAIN of count.

PaigeMiller
Diamond | Level 26

There is no "fifth" function, but you can easily just count and create the indicator variable

 

Example:

 

/* UNTESTED CODE */
data want; 
    set have;
    by id;
    if first.id then count=0;
    count+1;
    if count=5 then flag=1;
    drop count;
run;
--
Paige Miller
mkeintz
PROC Star
data want;
  set have;
  by id;
  if first.id then counter=1;
  else counter+1;
  if counter=5 then put 'found 5th occurrence at ' _n_=  (_all_) (=);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

Question: is it the "fifth" occurrence or the LAST? Your example appears to be asking for a last that just happens to be "fifth".

If not you should build examples that show 1) at least 6 elements and 2) what you expect if there are only fewer than 5 for an id and indicator combination.

PeterClemmensen
Tourmaline | Level 20

Is the data always sorted or grouped by ID?

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
  • 5 replies
  • 1184 views
  • 0 likes
  • 6 in conversation