Thursday, March 15, 2012

use PROC FORMAT to rank

The question is: after calculate the percentage, we want to count from how many obs are in 0-1%, how many in 1%-2%, ..., how many in 99%-100%. For this simple example, we can use round or floor or ceil to get the result. But here shows how to use proc format to get it.

After we get cumulative percentage, we format the percentage with put function. Take care in the proc format don't forget to add hlo, otherwise it will gives error since the first start number is missing.



data test;
do i=1 to 1000;
x=ranpoi(8,8);
output;
end;
run;

data test;
set test;
y+x;
run;

proc sql;
create table test as
select x, y, y/max(y) as pct
from test;
quit;

data test2;
do i=1 to 100;
x=i/100;
y=lag(x);
output;
end;
run;

data a;
set test2;
fmtname='fmttestf';
start=y;
end=x;
label=i;
eexcl='Y';
if _n_=1 then hlo='L';
run;

proc format cntlin=a;
select fmttestf;
run;

data final;
set test;
rank=put(pct, fmttestf.)+0;
run;

proc print data=final;
run;

proc sql;
select rank, count(1) as cnt
from final
group by rank
order by rank;
quit;

No comments:

Post a Comment