data temp;
do type='A','B','C' ;
do balance=1 to
5000;
id+1;
output;
end;
end;
run;
%let nobs=1000;*the number of top obs you want;
%let p_A=.5;*the percent of A in the top obs;
%let p_B=.3;
%let p_C=.2;
%let obs_A=%sysevalf(&nobs*&p_A,integer);
%let obs_B=%sysevalf(&nobs*&p_B,integer);
%let obs_C=%sysevalf(&nobs*&p_C,integer);
%put _user_;
proc sort data=temp nodupkey;
by type descending balance;
run;
data want;
set temp;
by type;
if first.type then count=0;
count+1;
select;
when(type='A' and count le &obs_A ) output;
when(type='B' and count le &obs_B ) output;
when(type='C' and count le &obs_C ) output;
otherwise;
end;
run;
data temp;
do type='A','B','C' ;
do balance=1 to 5000;
id+1;
output;
end;
end;
run;
%macro top(obs=1000,dsn=temp);
proc freq
data=&dsn;
tables type /
out=percent;
run;
data _null_;
set percent
end=last;
sum+count;
percent=percent*.01;
call symput(cats('type',_n_),type);
call symput(cats('per',_n_),percent);
if last
then call symput('count',_n_);
run;
%do i=1 %to &count;
%let obs&i=%sysevalf(&obs*&&per&i,integer);
%end;
%put _user_;
proc sort
data=&dsn nodupkey;
by type
descending balance;
run;
data want(drop=
count);
set &dsn;
by type;
if first.type then count=0;
count+1;
select;
%do
j=1 %to &count;
when(type="&&type&j" and count le &&obs&j ) output;
%end;
otherwise;
end;
run;
%mend;
%top(obs=1000,dsn=temp)
Ksharp
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.