Hi,
I have data as below and want to flag youngest 3 and oldest 3 records as shown in the screenshot below.
Is there any efficient way to do this ?
Thanks.
data have;
length name $8 sex $1;
input name sex age ;
cards ;
Joyce F 5
Thomas M 6
James M 12
Jane F 3
John M 12
Louise F 4
Robert M 12
Alice F 11
Barbara F 6
Jeffrey M 8
Alfred M 1
Carol F 16
Henry M 11
Judy F 8
Janet F 7
Mary F 12
Ronald M 13
William M 5
Philip M 8
;
run;
proc sort data=have ; by age ; run;
Want as below--
data have; length name $8 sex $1; input name sex age ; cards ; Joyce F 5 Thomas M 6 James M 12 Jane F 3 John M 12 Louise F 4 Robert M 12 Alice F 11 Barbara F 6 Jeffrey M 8 Alfred M 1 Carol F 16 Henry M 11 Judy F 8 Janet F 7 Mary F 12 Ronald M 13 William M 5 Philip M 8 ; run; proc sort data=have ; by age ; run; data temp; set have end=last; by age; n+first.age; if last then call symputx('n',n); run; data want; set temp; if n in (1 2 3) then youngest3=1; if n in (%eval(&n-2) %eval(&n-1) &n) then oldest3=1; run;
PROC RANK is one option for this: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=proc&docsetTarget=n1hxon9...
data have; length name $8 sex $1; input name sex age ; cards ; Joyce F 5 Thomas M 6 James M 12 Jane F 3 John M 12 Louise F 4 Robert M 12 Alice F 11 Barbara F 6 Jeffrey M 8 Alfred M 1 Carol F 16 Henry M 11 Judy F 8 Janet F 7 Mary F 12 Ronald M 13 William M 5 Philip M 8 ; run; proc sort data=have ; by age ; run; data temp; set have end=last; by age; n+first.age; if last then call symputx('n',n); run; data want; set temp; if n in (1 2 3) then youngest3=1; if n in (%eval(&n-2) %eval(&n-1) &n) then oldest3=1; run;
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!
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.