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
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.
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;
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;
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.
Is the data always sorted or grouped by ID?
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.