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--
WANT
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;
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 save with the early bird rate—just $795!
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.